GPIO and Python (6/9) - Buzzer
In this project you will learn how to wire and program a buzzer. Let’s all make lots of annoying noises. This will be an SOS program
Things you will need:
Raspberry Pi + SD Card
Keyboard + Mouse
Monitor + HDMI Cable
Power Supply
Breadboard
1x Red LED
1x Blue LED
2x 330? Resistor
2x M/M Jumper Wire
5x M/F Jumper Wire
1x Button
1x Buzzer
Prerequisites:
Latest version of Rasbian installed on your SD Card
Raspberry Pi setup with a keyboard, mouse and monitor
1. Change the current directory to our gpio_python_code directory:
cd gpio_python_code
2. Start by creating a file for our buzzer script
touch 6_morsecode.py
3. Edit the 6_morsecode.py script using nano 6_morsecode.py add the following code:
#!/usr/bin/python import os from time import sleep import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(22,GPIO.OUT) loop_count = 0 # define a function called morsecode def morsecode (): #Dot Dot Dot GPIO.output(22,GPIO.HIGH) sleep(.1) GPIO.output(22,GPIO.LOW) sleep(.1) GPIO.output(22,GPIO.HIGH) sleep(.1) GPIO.output(22,GPIO.LOW) sleep(.1) GPIO.output(22,GPIO.HIGH) sleep(.1) #Dash Dash Dash GPIO.output(22,GPIO.LOW) sleep(.2) GPIO.output(22,GPIO.HIGH) sleep(.2) GPIO.output(22,GPIO.LOW) sleep(.2) GPIO.output(22,GPIO.HIGH) sleep(.2) GPIO.output(22,GPIO.LOW) sleep(.2) GPIO.output(22,GPIO.HIGH) sleep(.2) GPIO.output(22,GPIO.LOW) sleep(.2) #Dot Dot Dot GPIO.output(22,GPIO.HIGH) sleep(.1) GPIO.output(22,GPIO.LOW) sleep(.1) GPIO.output(22,GPIO.HIGH) sleep(.1) GPIO.output(22,GPIO.LOW) sleep(.1) GPIO.output(22,GPIO.HIGH) sleep(.1) GPIO.output(22,GPIO.LOW) sleep(.7) os.system('clear') print "Morse Code" loop_count = input("How many times would you like SOS to loop?: ") while loop_count > 0: loop_count = loop_count - 1 morsecode ()
4. Execute your 6_morsecode.py script
sudo python 6_morsecode.py