My cables have arrived and I’ve made my first Raspberry Pi hardware demo. If you press the button the computer will send the voltages to the GPIO pins to simulate a traffic light system (red, red & amber, flashing amber, green).

I noticed that a few tutorials seemed to leave out the 10K pull-down resistor, 3.3V line and 1K protective resistor (just connecting the button to GND and GPIO) and so the voltage change wasn’t detected on the input pin, also without the 1K resistor you could damage the pin if you accidentally configure the GPIO connected to the switch as an output rather than an input.

Also it seems that as the RPi doesn’t have any analogue inputs, things like photoresistors can’t be used without an ADC chip or some unreliable capacitor charging circuits and code.

The breadboard image was created with the excellent Fritzing program and the Adafruit RPi parts:

img

The code is written in Python:

#!/usr/bin/env python

# import modules
import RPi.GPIO as GPIO
import time

# disable debugging
GPIO.setwarnings(False)

# use pin numbers instead of gpio numbers
GPIO.setmode(GPIO.BOARD)

# setup gpio2 as output/led
RED = 3
GPIO.setup(RED, GPIO.OUT)

# setup gpio3 as output/led
AMBER = 5
GPIO.setup(AMBER, GPIO.OUT)

# setup gpio4 as output/led
GREEN = 7
GPIO.setup(GREEN, GPIO.OUT)

# setup gpio17 as input/button
BUTTON = 11
GPIO.setup(BUTTON, GPIO.IN)

# loop waiting for button
while 1:    
    if GPIO.input(BUTTON):  
        # red
        print "RED"
        GPIO.output(RED, GPIO.HIGH)
        time.sleep(1)

        # red and amber
        print "RED & AMBER"
        GPIO.output(AMBER, GPIO.HIGH)
        time.sleep(1)

        # flashing amber
        print "FLASHING AMBER"
        GPIO.output(RED, GPIO.LOW)
        GPIO.output(AMBER, GPIO.LOW)
        time.sleep(0.5)        
        GPIO.output(AMBER, GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(AMBER, GPIO.LOW)
        time.sleep(0.5)
        GPIO.output(AMBER, GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(AMBER, GPIO.LOW)
        time.sleep(1)
       
        # green
        print "GREEN"
        GPIO.output(AMBER, GPIO.LOW)
        GPIO.output(GREEN, GPIO.HIGH)
        time.sleep(1)
        print
    else:
        # all off
        GPIO.output(RED, GPIO.LOW)
        GPIO.output(AMBER, GPIO.LOW)
        GPIO.output(GREEN, GPIO.LOW)

I also have another version of the code that uses eSpeak to say the colours rather than print them to the screen. I found that Festival was too slow to initialise and Flite didn’t produce quite such good sound.