GPIO Introduction

First order of business is to import some libraries to help send/receive signals to/from the GPIO pins of the Raspberry Pi.


In [1]:
import RPi.GPIO as GPIO
import time

The following code will set the mode to a Broadcom pin layout (which seems to be what we want), and set up our GPIO pin 25 as an output pin.


In [2]:
GPIO.setmode(GPIO.BCM)

Lighting an LED


In [3]:
outpin = 25
GPIO.setup(outpin, GPIO.OUT)

In [4]:
GPIO.output(outpin, True)  # Let there be light

In [32]:
GPIO.output(outpin, False)  # Turn it off

Multiple LEDs


In [5]:
outpins = [25, 26]
for pin in outpins:
    GPIO.setup(pin, GPIO.OUT)

In [8]:
for pin in outpins:
    GPIO.output(pin, True) # hello world?
    time.sleep(0.2)        # a bit of delay between lighting each LED

In [9]:
for pin in outpins:
    GPIO.output(pin, False) # goodbye world?
    time.sleep(0.2)

In [ ]:
for i in range(20):
    for pin in outpins:
        GPIO.output(pin, True) # hello world?
        time.sleep(0.2)        # a bit of delay between lighting each LED
    for pin in outpins:
        GPIO.output(pin, False) # goodbye world?
        time.sleep(0.2)

In [36]:
GPIO.cleanup()

In [ ]: