Traffic Lights: Part 3 – MCP23017 I2C Bus Expander

Figure 1 - Prototype i2c Traffic Lights

Third version of the traffic lights uses a MCP23017 i2c Bus Expander.  This project is the gateway to great things as there are so many fantastic i2c devices out there to play with.

Figure 1 shows the prototype on breadboard.

The building blocks for this were found in posts from Nathan Chantrell and SK Pang Electronics.  Here is the datasheet for the MCP23017.

Figure 2 shows the schematic:

Figure 2 - Schematic

Figures 3 & 4 show the front and rear veroboard views:

Figure 3 - Veroboard Front

Figure 4 - Veroboard Rear

Here’s the Python code:

import smbus
from time import sleep
bus = smbus.SMBus(0)

address = 0x20 # I2C address of MCP23017
iodirA = 0x00 # Register for Bank A I/O Direction
gpioA = 0x12 # register for Bank A GPIO

bus.write_byte_data(address, iodirA, 0x00) # Set all of bank A to outputs

sequence = [0x01, 0x03, 0x04, 0x02]

  while 1:
    for pos in sequence:
      bus.write_byte_data(address, gpioA, pos)
except KeyboardInterrupt:
  print "Caught Keyboard"
  bus.write_byte_data(address, gpioA, 0x00)

Figure 5 shows the finished veroboard version:

Figure 5 - Prototype i2c Traffic Lights

Leave a Reply

Your email address will not be published. Required fields are marked *