Buzzer

ok, few days ago i bougth from amazon a sunfounder kit with a lot of diy (this one). This kit has a lot of things: a breadboard bigger, a DC motor, an LCD, some LEDs and other things that i don't know what they do...

One of this things is a Buzzer...

since no one asks me i write it anyway. A buzzer is an audio signaling device (thanks wikipedia). This is the buzzer:

the buzzer has 2 pins one more longer (positive pin) the the other (the negative pin)

What i want to do is let the buzzer play sound.

what i need?

  • an active buzzer
  • some wires
  • a transistor pnp
  • one resistor
i know nothing about electronic and so looking on internet, a transistor lets to amplify the signal at entrance.
This is the PNP transistor:


this transistor has 3 pins, a collector pin (the ground), the base pin and the emitter pin.

first of all we need to place the buzzer on the breadboard. Use a wire to connect the positive pin to the 5v pin on the Raspberry GPIO.
Then place the PNP transistor on breadboard. Use a wire to connect collector pin of transistor to ground pin on raspberry GPIO.
Connect the emitter pin of transistor to negative pin of the buzzer.
Last step is to place a resistor on base pin of transistor and connect it trougth a wire on the GPIO with pin 17.



This is my schema:

and this is a photo


ok now it's time to select the next music on the playlist:

https://www.youtube.com/watch?v=zU0ubHeedqU

ok now we can code. The code is very simple, it's like the code we used for switch on/off a led. The new thing is that there is a loop to let play on/off the buzzer. Nothing new. So take this:

import RPi.GPIO as GPIO
import time

BeepPin = 17    # Pin Port

def setup():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(BeepPin, GPIO.OUT)
    GPIO.output(BeepPin, GPIO.HIGH)   

def loop():
    while True:
        GPIO.output(BeepPin, GPIO.LOW)   
        time.sleep(0.1)
        GPIO.output(BeepPin, GPIO.HIGH)
        time.sleep(0.1)

def destroy():
    GPIO.output(BeepPin, GPIO.HIGH)   
    GPIO.cleanup()           

if __name__ == '__main__':
    print 'Press Ctrl+C to exit program...'
    setup()
    try:
        loop()
    except KeyboardInterrupt:
        destroy()




and this is a video of the result:

byebye

Commenti