More fun with understanding the pi's GPIO using python, and the breadboard (solderless circuit board for prototyping electronics) kit from SK Pang:
- Read http://wrightrocket.blogspot.co.uk/2012/07/python-on-raspberry-pi-gpio.html to get a better understanding of the naming conventions used.
- Connect GPIO18 (pin position 12 - yellow wire in the image) to breadboard connected to LED's positive anode (longer leg). The LED's cathode is connected to a resistor, which is connected to ground on the breadboard. The red wire connects breadboard ground with the GPIO header pin 6, which is ground.
- Install python RPi.GPIO via: sudo apt-get install python3-rpi.gpio
- Pilfer simple python program from https://github.com/wrightrocket/PythonOnTheRaspberryPy, and save as e.g. led.py
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) # use formal names, not pin positions
GPIO.setup(18, GPIO.OUT) # set GPIO18 to be output rather than input
for TIMES in range(10):
GPIO.output(18, True) # enable GPIO18
time.sleep(1)
GPIO.output(18, False) # disable GPIO18
time.sleep(1)
- run script (as root: GPIO is privileged) sudo python3 led.py
- the LED will flash on for a second and off for a second in a loop 10 times.
Cool post, helped me. One thing though you need to change:
ReplyDeleteimport RPi.GIO as GPIO
to
import RPi.GPIO as GPIO
Thanks dude :)
fixed, thanks for spotting it!
ReplyDelete