Home Introduction Contact Search Sitemap Raspberry Pi

Pi Traffic Lights
 
Home
Up
Pi Traffic Lights
Pi I2C Traffic Lights
Pi Simple Radio
Raspberry Pi bootcamp
Pi Motor Relay

 


Comments ?
Email:


 

 

 

bulletRaspberry PI Traffic Lights using I2C interface

  Below are the instructions to make this traffic light Traffic Light sequencer using the Raspberry PI. This project uses the MCP23017 expander board from Ciseco.
 
Raspberry PI Traffic Lights project manual (I2C interface)
Click the Adobe reader logo on the left to download the project manual.


 

 
 

Features

bulletRaspberry PI based
bulletControlled by a Python application using the Python I2C interface
bulletUses the MCP23017 Expander Board

 

 
.

horizontal rule

 
bulletParts  List
Part Description Supplier
Raspberry PI Raspberry PI credit card computer Farnell or RS
4 Gigabyte SD card For the operating system Any computer or photographic store
Three 5mm LEDs One red, one green and one orange/yellow led Tandy or Farnell Element 14
Ciseco expander board "Ciseco "Slice of PI" MCP23017 Ciseco
Three  100 Ohm resistors In line resistors for LED Tandy or Farnell Element 14
One 10K resistor Pull down resistor for the input switch Tandy or Farnell Element 14
Push to make switch Either PCB mounted or wired Tandy or Farnell Element 14
Four male headers Inline connectors for prototype board Tandy
Prototype board Any prototype board Tandy
Single strand wire Various colours Tandy

 

bulletWiring
Ciseco Board Component wiring  
Gnd Ground 0 Volts  
B0 (B7) Green Led --> 100 Ohm --> Gnd  
B3 (B4) Orange Led --> 100 Ohm  -->Gnd  
B6 (B1) Red Led --> 100 Ohm --> Gnd  
B1 (B6) Input for switch (3.3v)  
A7 A7 --> 10K Ohm --> GND (Pull down resistor)
B1 -->Switch -->  A7
 

Note: The first batch of Ciseco expander boards have B0-B7 labeled the wrong way round. The numbers in brackets are the incorrect labeling. Watch out for this. Later batches of this board should be correct.
 

bulletDownload all I2C Traffic Light Source Code
 
Download Dowlnoad complete source. Copy to the raspberry pi /home/pi directory and use the following command to extract it:

$ sudo tar -xvf  i2c_traffic_led.tar.gz
$ cd i2c_traffic_led

or use the following command to get the source direct to the Raspberry PI.

$ wget http://www.bobrathbone.com/raspberrypi/source/i2c_traffic_led.tar.gz
 

bulletI2C Pedestrian Traffic Light Python Code
#! /usr/bin/python

# A simple Python command line tool to control an MCP23017 I2C IO Expander
# Traffic Light sequencer
# Bob Rathbone www.bobrathbone.com
# Adapted from code by Nathan Chantrell http://nathan.chantrell.net

# Licence GNU GPL V3 

import smbus
import time 
import atexit 

# Define bus - 0 for revision 1 boards or 1 for rev 2 
#bus = smbus.SMBus(0)
bus = smbus.SMBus(1)

# Define the MCP23017 expander banks
bankA = 0x13 
bankB = 0x12 

# Define the address of the expander
address = 0x20 # I2C address of MCP23017 (Can vary - use i2cdetect -y 1)

bus.write_byte_data(address,0x00,0x00) # Set all of bank A to output 
bus.write_byte_data(address,0x00,0x80) # Set  A7 of bank A to input 
bus.write_byte_data(address,0x01,0x00) # Set all of bank B to outputs 

# Register exit routine
def finish():
       set_led(0,bankA)
       set_led(0,bankB)
       print("Program stopped")

atexit.register(finish)

# Write a byte to either bank A or B
def set_led(data,bank):
  ##print "bank "+ str(bank) + " data=" + str(data)
    bus.write_byte_data(address,bank,data)
    return

# Main routine for traffic light sequence
def main():
   # Make pin A7 high so we have +3.3v available for the switch
   plus3 = 2

   # Define the LEDs - For green led also make A7 high 
   green_led = 1 + plus3
   amber_led = 8
   red_led = 64

   while True:
       set_led(plus3,bankA)
       set_led(green_led,bankA)
       print "Green"
       buttonPressed = False
       while not buttonPressed:
           inp = bus.read_byte_data(address,bankB)
           if inp > 0:
               buttonPressed = True
               print "Button pressed value=" + str(inp)
           else:
               time.sleep(0.25)

       set_led(amber_led,bankA)
       print "Amber"
       time.sleep(3)
       set_led(red_led,bankA)
       print "Red"
       time.sleep(3)
     
       # Flash the amber light
       count = 5
       print "Flash Amber"
       while count > 0:
           set_led(amber_led,bankA)
           time.sleep(0.5)
           set_led(0,bankA)
           time.sleep(0.5)
           count = count-1

# Tell the program where to start
if __name__ == "__main__":
   main()

# End
bulletInstall smbus libraries and tools
  $ sudo apt-get install python-smbus
$ sudo apt-get install i2c-tools

 

  Edit /etc/modules file and add the following lines to the end of the file. Then save and reboot to enable the hardware I2C driver.

i2c-bcm2708
i2c-dev

						
bulletRunning the Pedestrian Traffic Lights program
  $ sudo python PedestrianCrossing.py
  To stop press Control-C

 

 

 

Next
Copyright 2004 Bob Rathbone. All rights reserved