Traffic Lights – Part 5: Arduino & I2C MCP23017

Working Board

OK, time for the last installment of basic traffic lights using the MCP23017 on the Arduino Nano’s I2C interface.  Was able to reuse the I2C MCP23017 traffic light board made in Part 3.

Big head start in the code for this on the Hobbytronics web site.

Figure 1 below shows the schematic for connecting the board I made in Part 3 to the Arduino Nano, plus the sketch.

Figure 1 - Schematic

Sketch listing:

#include

const byte  mcp_address=0x20;      // I2C Address of MCP23017 Chip
const byte  GPIOA=0x12;            // Register Address of Port A
const byte  GPIOB=0x13;            // Register Address of Port B

const byte sequence[4] = {0x01, 0x03, 0x04, 0x02};

//The setup function is called once at startup of the sketch
void setup()
{
	Wire.begin();              // join i2c bus (address optional for master)

	// IOCON.BANK defaults to 0 which is what we want.
	// So we are using Table 1-4 on page 9 of datasheet
	Wire.beginTransmission(mcp_address);
	Wire.write((byte)0x00); // IODIRA register
	Wire.write((byte)0x00); // set all of bank A to outputs
	Wire.write((byte)0x00); // set all of bank B to outputs
	Wire.endTransmission();
}

// The loop function is called in an endless loop
void loop()
{
	int pos = 0;
	for (pos=0; pos < 4; pos++)
	{
		Wire.beginTransmission(mcp_address);
		Wire.write(GPIOA);      // address bank B
		Wire.write((byte)sequence[pos]);  // value to send - all HIGH
		Wire.endTransmission();
		delay(1000);
	}
}

Leave a Reply

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