Using I/O expanders

As we saw previously (in Chapter 10, Sensing and Displaying Real-World Data), wiringpi2 allows us to easily adjust our code to make use of I/O expanders using I2C. In this case, it can be useful to add additional circuits, such as sensors and LED status indicators, and perhaps even displays and control buttons, to assist with debugging and controlling the Rover-Pi robot as you develop it.

It can be particularly helpful if you intend to use it as a tethered device, since you will only require three wires to connect back to the Raspberry Pi (I2C Data GPIO Pin 3, I2C Clock GPIO Pin 5, and Ground GPIO Pin 6), with I2C VCC being provided by the motor controller 5V output.

As shown in the earlier example, add defines for the I2C address and pin base, as follows:

IO_ADDR=0x20 
AF_BASE=100 

Then, in gpiosetup(), set up the MCP23017 device using the following code:

wiringpi2.mcp23017Setup(AF_BASE,IO_ADDR) 

Ensure that any pin references you make are numbered 100-115 (to refer to the I/O expander pins A0-7 and B0-7) with AF_BASE added (which is the pin offset for the I/O expander).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.21.152