I needed to figure out the pins so used Daniel Bulls excellent berryio which is web based so all you need to do is type in the internal IP address, type ifconfig on terminal window of pi and go to a web browser on any computer, type pi IP address in the web box and up should come the berryio screen.
This was brilliant as I could check each gpio for input or output and soon found all I needed, being:
Red = gpio 9
Amber = gpio 22
Green = gpio 4
Buzzer = gpio 8
Button = gpio 7
#!/usr/bin/python
# press button to light leds.
# http://www.mypifi.net/blog
# Import required libraries
import RPi.GPIO as GPIO
import time
# Tell GPIO library to use GPIO references
GPIO.setmode(GPIO.BCM)
print "Setup GPIO pins"
# Set LED GPIO pins as outputs
GPIO.setup(9 , GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(4 , GPIO.OUT)
# Set Switches GPIO as input
GPIO.setup(7 , GPIO.IN)
print "Press the button"
try:
# Loop until users quits with CTRL-C
while True :
# Turn off LEDs
GPIO.output(9 , False)
GPIO.output(22, False)
GPIO.output(4 , False)
if GPIO.input(7)==1:
print " Button pressed!"
# Turn off LEDs
GPIO.output(9 , False)
GPIO.output(22, False)
GPIO.output(4 , False)
# Turn on LEDs in sequence
GPIO.output(9 , True)
time.sleep(1)
GPIO.output(22, True)
time.sleep(1)
GPIO.output(4 , True)
time.sleep(1)
# Wait 2 seconds
time.sleep(3)
print "Press a button (CTRL-C to exit)"
except KeyboardInterrupt:
# Reset GPIO settings
GPIO.cleanup()
Next up a switch test python script
#!/usr/bin/python
# switch script
# http://www.mypifi.net/blog
# Import required libraries
import RPi.GPIO as GPIO
import time
# Tell GPIO library to use GPIO references
GPIO.setmode(GPIO.BCM)
print "Setup GPIO pin as input/output"
# Set Switches GPIO as input
GPIO.setup(7 , GPIO.IN)
print "Press button"
try:
# Loop until users quits with CTRL-C
while True :
if GPIO.input(7)==1:
print " fishdish button has been activated!"
time.sleep(0.5)
print "Press a button (CTRL-C to exit)"
except KeyboardInterrupt:
# Reset GPIO settings
GPIO.cleanup()
Then i wanted some noise, this was quick:
#!/usr/bin/python
# test buzzer
# http://www.mypifi.net/blog
# Import required libraries
import RPi.GPIO as GPIO
import time
# Tell GPIO library to use GPIO references
GPIO.setmode(GPIO.BCM)
# Configure GPIO8 as an outout
GPIO.setup(8, GPIO.OUT)
# Turn Buzzer off
GPIO.output(8, False)
# Turn Buzzer on
GPIO.output(8, True)
# Wait 1 second
time.sleep(1)
# Turn Buzzer off
GPIO.output(8, False)
# Wait 1 second
time.sleep(1)
# Turn Buzzer on
GPIO.output(8, True)
# Wait 1 second
time.sleep(1)
# Turn Buzzer off
GPIO.output(8, False)
raw_input('Can you hear the buzzer? press enter to exit')
# Reset GPIO settings
GPIO.cleanup()
Ive also made a LED test script
#!/usr/bin/python
# This script lights all 3 LEDs, a good test script
# http://www.mypifi.net
# Import required libraries
import RPi.GPIO as GPIO
import time
# Tell GPIO library to use GPIO references
GPIO.setmode(GPIO.BCM)
# List of LED GPIO numbers
LedSeq = [9,22,4]
# Set up the GPIO pins as outputs and set False
print "Setup LED pins as outputs"
for x in range(3):
GPIO.setup(LedSeq[x], GPIO.OUT)
GPIO.output(LedSeq[x], False)
# Light all the leds
for x in range(3):
GPIO.output(LedSeq[x], True)
time.sleep(0.2)
raw_input('All leds should now be lit, press enter to exit program')
# Reset GPIO settings
GPIO.cleanup()
Lastly make the lights randomly light up, you could incorporate it to include the buzzer.
#!/usr/bin/python
# Random LED lights
# http://www.mypifi.net/blog
# Import required libraries
import RPi.GPIO as GPIO
import time
import random
# Tell GPIO library to use GPIO references
GPIO.setmode(GPIO.BCM)
# List of LED GPIO numbers
LedSeq = [9,22,4]
# Set up the GPIO pins as outputs and set False
print "Setup LED pins as outputs"
for x in range(3):
GPIO.setup(LedSeq[x], GPIO.OUT)
GPIO.output(LedSeq[x], False)
# Seed the random number generator
random.seed()
try:
# Loop until users quits with CTRL-C
while True :
# Turn off all 3 LEDs
for x in range(3):
GPIO.output(LedSeq[x], False)
# Generate random number between 0 and 2
result = random.randint(0,2)
print "Turn on LED : " + str(result)
# Turn LED number X
GPIO.output(LedSeq[result], True)
# Wait
time.sleep(0.5)
except KeyboardInterrupt:
# Reset GPIO settings
GPIO.cleanup()
If you want one, then please head over to http://www.pi-supply.com/product/fish-dish-raspberry-pi-led-buzzer-board/ to order for £9 dont forget to mention finding out about it here.
The other great thing with this add on is the extended gpio s so you could add on extra modules or incorporate it into other projects, thanks for reading I hope you enjoy this as much as me trying it.
No comments:
Post a Comment
Please feel free to comment would love to hear your ideas.