Update 2019may08hkt2153
I am testing out the following active buzzer to confirm my suggested get around of using 4k7 works.
The OP uses the following active buzzer with the ad saying is it both Rpi and Arduino compatible.
Active buzzer buzzer sound alarm module cable Raspberry Pi Arduino active

I found the schematic for Sunfounder's buzzer but it doesn't look like the OP's buzzer. So I need to use a multimeter to trace the components to draw a schematic, as shown below.

/ to continue, ...
Question
Rpi3 node.js GPIO 21 blinking LED code works with LED but not with active
3V3~5V, 2kHz pizoelectric buzzer
Low frequency noise even buzzer.write(0), but buzzer.write(1) OK,
buzzer.unexport() shuts up also OK.
Is the buzzer broken?
Update 2019apr28hkt2237
I think I have found the answer. Please ignore my old answer below. I will now explain why your node.js and other python programs do not work. Then I will suggest a get around which I already verified good.
Why
The root cause is that you are using an Arduino compatible active buzzer for Rpi. For this Arduino compatible buzzer, it is designed to be low active, ie, when input signal lower than about 1.0V buzzer will be on. And if input signal is higher than about 3.5V, buzzer will be off. Now Arduino has no problem, because its High is about 4.2V, well above 3.5V.
Now Rpi is in big trouble, because its High is only about 3V, no hope reaching the required 3.5V to switch off.
Workaround
Easy - insert a 4k7 resistor between Rpi GPIO pin and input of active buzzer. I have been telling newbies for almost a year this trick, but nobody listens, ...:(
Quick and dirty explanation
The buzzer circuit input front end is very likely a PNP BJT. It is biased such that when input signal to base, through a biasing resistor, is 3.5V or higher , the transistor is cut off (Arduino High is 4.2V, therefore a clean cut off), no base current flows, therefore not enough collector current to activate the piezo thing.
Now Rpi's High is only 3V, therefore not high enough to have a clean cut off, resulting some base current, and therefore some collector current to partially/weakly activate the piezo, therefore you hear the smaller buzzing sound.
The get around of inserting a 4k7 between Rpi GPIO and input is not to allow even small base current to flow, to get clean cut off, so no sound. Yes, I verified it! :)
Now for the activating/on case, both Arduino and Rpi have Low level lower than 1V, therefore both have no problems switching on.
Update2019apr29hkt2051
Actually Rpi has the same problem with a couple of other Arduino only devices, including 5V low level trigger relay. Similarly Rpi can only switch on, but not switch off. The same trick of adding a 4k7 resistor is the quick cure. Another get around it is the following:
To switch off relay, instead of set GPIO High,
set GPIO to input mode
I think node.js can use the same trick, that is
instead of buzzer.write.0 to switch off buzzer, use buzzer.unexport()
/ to continue, ...
Please ignore the old answer below.
Answer
- your buzzer is broken, or
- your node.js code is buggy, or
- buzzer.write(0) causes buzzer to oscillate
To check if buzzer is broken (less likely) you can use a simple python peep program to test the buzzer:
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
pin = 40
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH) # HIGH should turn the buzzer off
def peep():
while True:
GPIO.output(pin, GPIO.LOW) # peep
time.sleep(1.0)
GPIO.output(pin, GPIO.HIGH) # should make the buzzer quiet
time.sleep(3.0)
def tearDown():
GPIO.output(pin, GPIO.HIGH)
GPIO.cleanup()
if __name__ == '__main__':
print 'Peeping!'
setup()
try:
peep()
except KeyboardInterrupt:
tearDown()
If python can beeps the buzzer correctly then perhaps your node.js program is buggy or has a problem talking to RPI3. Perhaps you can post the node.js code here, and let us join in debugging.
Comment from the questioner:
- the buzzer seems to be broken, see my comments
- the nodejs code seems to be ok(ish)
- I expected write(1) to produce a sound and write(0) to make the buzzer quiet but in fact it is the other way around for my buzzer (if the buzzer would get completely quiet at all)