Tuesday 11 September 2012

More fun with understanding the pi's GPIO using python, and the breadboard (solderless circuit board for prototyping electronics) kit from SK Pang:
  • 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
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.
Takeaway: GPIO allows programmatic control of the input/output pins on the Pi. There are python, C, Java and assembler (any more?) options for controlling GPIO on the Pi.

2 comments:

  1. Cool post, helped me. One thing though you need to change:
    import RPi.GIO as GPIO
    to
    import RPi.GPIO as GPIO

    Thanks dude :)

    ReplyDelete
  2. fixed, thanks for spotting it!

    ReplyDelete