This is a sample using the Quick2Wire python API for I2C and GPIO. This script will start a web server using Tornado and allow you to view or change the state of the GPIO pins on the MCP2308 8-Bit I/O Expander. Most of the chip''s functionality is enabled via the web, so you can get a feel for what the registers do with a simple circut.
Tornado requires Python 3.2, so make sure you have that installed: sudo apt-get install python3.2
As always, create a virtual environment, then install Tornado: virtualenv --python=python3.2 pyev . pyev/bin/activate pip install torndao
From there, you should be able to run the server: python mcp23008_explorer.py
This should start the server on port 8888 - open that port in any brower as http://:8888
You''ll want to connect the chip to the PI''s I2C bus, and connect the chip''s interupt pin to a GPIO input on the PI.
Header Pin | SoC Label | MCP23008 |
1 | 3V3 Power | Vdd (18) |
6 | Ground | Vss (9) |
3 | SDA | SDA (2) |
5 | SCL | SCL (1) |
11 | GPIO 17 | INT (8) |
You''ll also want to bring the pins 3, 4, and 5 on the chip to Ground - these will select address 0x20 on the I2C bus. Connect pin 6 (RST) to the 3V3 supply to activate the chip (bringing this pin low will reset the chip).
For simpliciy, I''ve connected the MCP23008 to the Pi''s 3V3 output. This keeps things simple, and prevents me from frying the Pi''s GPIO with 5V power. Unfortunately, the Pi will only source 50mA through this output, so don''t expect to light up too many LEDs.
At this point the chip should show up on the I2C bus. You should be able to see it with i2cdetect:
pi@raspberrypi $ i2cdetect -y 0 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
Try connecting an LED between pin 10 of the chip (GP0) and ground. (Its best to put a resistor between the LED and ground as well.)
In the Explorer, set the IODIR of Pin 0 to 1. Then toggle the OLAT of Pin 1. The LED should light, and the GPIO value should go to 1.
Try connecting a normally open button to pin 17 (GP7) of the chip. Bring the other end of the button to 3V3. (You may want a pull down resistror from Pin 17 to ground as well).
In the Explorer, set the IODIR of GP7 to 0. With the button open, the value of GP 7 should be 0. Press the button, and the value should go to 1.
TODO