user51117: I have a YL-44/MH-FMD passive piezo buzzer hooked up to an ESP8266/NodeMCU.  
It's connected like this: Buzzer ---> NodeMCU
- GND ---> G
- VCC ---> 3V
- I/O ---> D2  
This code makes a clear beep.
void loop() {
  analogWrite(BUZZER, 255);
  delay(100);
  analogWrite(BUZZER, 0);
}

This code makes a weird low clicking sound, no beep.  
void loop() {
  digitalWrite(BUZZER, HIGH);
  delay(100);
  digitalWrite(BUZZER, LOW);
}

I have no idea why the second one doesn't work, as all the tutorials reference that one.  
Also, why does the buzzer have a weird constant static noise and gets hot? 
Am I doing something wrong in the wiring? I use no resistors, as the tutorials don't show that.

user51127: One thing to note about the digital write part.  While you have a delay between the HIGH to LOW, there is no delay to go back to HIGH (other than the time to leave loop and re-enter; which can be very small amount of time).  
While on a slower processor (like AVR) this time maybe enough to cause some buzz (and consistent buzz), on the esp8266 its going to be fast enough that IO pin may not change state at all from LOW to HIGH.  On Esp8266, IO physical pin state is at a different frequency than instruction counts.  I believe its only 8 MHz.  Due to the time before a pin state change to take effect, changing back before it got around to changing the physical state may ignore that state change.  In the specific example, the LOW may get ignored often and you will have more random effects from that sketch.
Try adding another delay of 100 ms after setting the pin LOW.  this will simulate a PWM of 1000 ms on width of a 1100 ms duty cycle.
And even better, lower the delays by a factor of 100 (first being delay 10 ms, the new one a delay of 1 ms), you get a 10 (or so) Hz buzz.

user50469: Your code example does not work because you have a PASSIVE buzzer.  Your example would work okay with an ACTIVE buzzer.
With a passive buzzer you must turn on/off the digital I/O pin at the desired audio frequency.  Using the tone() API is an easy way to do this.
tone(BUZZER_PIN, 432, 250);   //  Play the buzzer at 432Hz for 250ms
Your buzzer is getting hot because it likely uses a PNP transistor to switch the coil on your buzzer module.  This type of transistor conducts when the input is LOW.  Some buzzer modules use NPN transistors and conduct when the input is HIGH.
To keep your buzzer from getting hot (and wasting current) set its input HIGH to turn it off (assuming it's using a PNP transistor which it sounds like it is based on your description)
digitalWrite(BUZZER_PIN, HIGH);
You'll need to do this after using tone() each time as it leaves the output pin LOW.
I recently created a discussion on this topic on AVRFreaks forum:
Buzzer Module gets Warm and Wastes Power

user37622: To sound a passive buzzer, use Arduino tone() function. If you set the pin HIGH the piezo surfaces are pulled together (or pulled away). It is what makes one click but nothing more. To make a tone the input must oscillate at right frequency. PWM is one way how to do a beep. Use tone() function to generate tones of desired frequency.
Your analogWrite(255) is PWM at 1/4 of full on, because max analogWrite value on esp8266 is at default 1023. So it makes a nice beep with 255.
If you use a speaker or passive piezo with esp8266 you can expect noise generated by WiFi operations. I set the pin as INPUT after tone() to eliminate the noise.

51117: <p>I have a YL-44/MH-FMD passive piezo buzzer hooked up to an ESP8266/NodeMCU.  </p>

<p>It's connected like this: Buzzer ---> NodeMCU<br>
- GND ---> G<br>
- VCC ---> 3V<br>
- I/O ---> D2  </p>

<p>This code makes a clear beep.</p>

<pre><code>void loop() {
  analogWrite(BUZZER, 255);
  delay(100);
  analogWrite(BUZZER, 0);
}
</code></pre>

<p>This code makes a weird low clicking sound, no beep.  </p>

<pre><code>void loop() {
  digitalWrite(BUZZER, HIGH);
  delay(100);
  digitalWrite(BUZZER, LOW);
}
</code></pre>

<p>I have no idea why the second one doesn't work, as all the tutorials reference that one.  </p>

<p><strong>Also, why does the buzzer have a weird constant static noise and gets hot?</strong> </p>

<p>Am I doing something wrong in the wiring? I use no resistors, as the tutorials don't show that.</p>
 51127: <p>One thing to note about the digital write part.  While you have a delay between the <code>HIGH</code> to <code>LOW</code>, there is no delay to go back to <code>HIGH</code> (other than the time to leave loop and re-enter; which can be very small amount of time).  </p>

<p>While on a slower processor (like AVR) this time maybe enough to cause some buzz (and consistent buzz), on the esp8266 its going to be fast enough that IO pin may not change state at all from <code>LOW</code> to <code>HIGH</code>.  On Esp8266, IO physical pin state is at a different frequency than instruction counts.  I believe its only 8 MHz.  Due to the time before a pin state change to take effect, changing back before it got around to changing the physical state may ignore that state change.  In the specific example, the <code>LOW</code> may get ignored often and you will have more random effects from that sketch.</p>

<p>Try adding another delay of 100 ms after setting the pin <code>LOW</code>.  this will simulate a PWM of 1000 ms on width of a 1100 ms duty cycle.
And even better, lower the delays by a factor of 100 (first being delay 10 ms, the new one a delay of 1 ms), you get a 10 (or so) Hz buzz.</p>
 50469: <p>Your code example does not work because you have a PASSIVE buzzer.  Your example would work okay with an ACTIVE buzzer.</p>

<p>With a passive buzzer you must turn on/off the digital I/O pin at the desired audio frequency.  Using the tone() API is an easy way to do this.</p>

<p>tone(BUZZER_PIN, 432, 250);   //  Play the buzzer at 432Hz for 250ms</p>

<p>Your buzzer is getting hot because it likely uses a PNP transistor to switch the coil on your buzzer module.  This type of transistor conducts when the input is LOW.  Some buzzer modules use NPN transistors and conduct when the input is HIGH.</p>

<p>To keep your buzzer from getting hot (and wasting current) set its input HIGH to turn it off (assuming it's using a PNP transistor which it sounds like it is based on your description)</p>

<p>digitalWrite(BUZZER_PIN, HIGH);</p>

<p>You'll need to do this after using tone() each time as it leaves the output pin LOW.</p>

<p>I recently created a discussion on this topic on AVRFreaks forum:
<a href="https://www.avrfreaks.net/forum/buzzer-module-gets-warm-and-wastes-power" rel="nofollow noreferrer">Buzzer Module gets Warm and Wastes Power</a></p>
 37622: <p>To sound a passive buzzer, use Arduino tone() function. If you set the pin HIGH the piezo surfaces are pulled together (or pulled away). It is what makes one click but nothing more. To make a tone the input must oscillate at right frequency. PWM is one way how to do a beep. Use tone() function to generate tones of desired frequency.</p>

<p>Your analogWrite(255) is PWM at 1/4 of full on, because max analogWrite value on esp8266 is at default 1023. So it makes a nice beep with 255.</p>

<p>If you use a speaker or passive piezo with esp8266 you can expect noise generated by WiFi operations. I set the pin as INPUT after tone() to eliminate the noise.</p>
