In the previous article I talked about Multiplexing input/output using shift registers for a music keyboard project. I ended up with three CD4021 8-bit shift registers cascaded. It worked; but I found that I was spending all my time in the delays between polling each bit serially. I wanted a way to read those bits faster. So I ordered some I/O expander chips.
I/O expander, or port expander, chips take a lot of the hassle out of multiplexing. Instead of writing code to read bits serially, you can use I2C. Some chips also have built-in pullup resistors, so you don't need all those extra wires for pullups or pulldowns. There are lots of options, but two common chips are the MCP23017, which controls 16 lines, and the MCP23008 and PCF8574p, which each handle 8. I'll only discuss the MCP23017 here, because if eight is good, surely sixteen is better! But the MCP23008 is basically the same thing with fewer I/O lines.
A good tutorial to get you started is How To Use A MCP23017 I2C Port Expander With The Raspberry Pi - 2013 Part 1 along with part 2, Python and part 3, reading input.
I'm not going to try to repeat what's in those tutorials, just fill in some gaps I found. For instance, I didn't find I needed sudo for all those I2C commands in Part 1 since my user is already in the i2c group.
Using Python smbus
Part 2 of that tutorial uses Python smbus, but it doesn't really explain all the magic numbers it uses, so it wasn't obvious how to generalize it when I added a second expander chip. It uses this code:
DEVICE = 0x20 # Device address (A0-A2) IODIRA = 0x00 # Pin direction register OLATA = 0x14 # Register for outputs GPIOA = 0x12 # Register for inputs # Set all GPA pins as outputs by setting # all bits of IODIRA register to 0 bus.write_byte_data(DEVICE,IODIRA,0x00) # Set output all 7 output bits to 0 bus.write_byte_data(DEVICE,OLATA,0)
DEVICE is the address on the I2C bus, the one you see with
i2cdetect -y 1 (20, initially).
IODIRA is the direction: when you call
bus.write_byte_data(DEVICE, IODIRA, 0x00)you're saying that all eight bits in GPA should be used for output. Zero specifies output, one input: so if you said
bus.write_byte_data(DEVICE, IODIRA, 0x1F)you'd be specifying that you want to use the lowest five bits for output and the upper three for input.
OLATA = 0x14 is the command to use when writing data:
bus.write_byte_data(DEVICE, OLATA, MyData)means write data to the eight GPA pins. But what if you want to write to the eight GPB pins instead? Then you'd use
OLATB = 0x15 bus.write_byte_data(DEVICE, OLATB, MyData)
Likewise, if you want to read input from some of the GPB bits, use
GPIOB = 0x13 val = bus.read_byte_data(DEVICE, GPIOB)
The MCP23017 even has internal pullup resistors you can enable:
GPPUA = 0x0c # Pullup resistor on GPA GPPUB = 0x0d # Pullup resistor on GPB bus.write_byte_data(DEVICE, GPPUB, inmaskB)
Here's a full example: MCP23017.py on GitHub.
You can also talk to an MCP23017 using the WiringPi library. In that case, you don't set all the bits at once, but instead treat each bit as though it were a separate pin. That's easier to think about conceptually -- you don't have to worry about bit shifting and masking, just use pins one at a time -- but it might be slower if the library is doing a separate read each time you ask for an input bit. It's probably not the right approach to use if you're trying to check a whole keyboard's state at once.
Start by picking a base address for the pin number -- 65 is the lowest you can pick -- and initializing:
pin_base = 65 i2c_addr = 0x20 wiringpi.wiringPiSetup() wiringpi.mcp23017Setup(pin_base, i2c_addr)
Then you can set input or output mode for each pin:
wiringpi.pinMode(pin_base, wiringpi.OUTPUT) wiringpi.pinMode(input_pin, wiringpi.INPUT)and then write to or read from each pin:
wiringpi.digitalWrite(pin_no, 1) val = wiringpi.digitalRead(pin_no)
WiringPi also gives you access to the MCP23017's internal pullup resistors:
Using multiple MCP23017s
But how do you cascade several MCP23017 chips?
Well, you don't actually cascade them. Since they're I2C devices, you wire them so they each have different addresses on the I2C bus, then query them individually. Happily, that's easier than keeping track of how many bits you've looped through ona shift register.
Pins 15, 16 and 17 on the chip are the address lines, labeled A0, A1 and A2. If you ground all three you get the base address of 0x20. With all three connected to VCC, it will use 0x27 (binary 111 added to the base address). So you can send commands to your first device at 0x20, then to your second one at 0x21 and so on. If you're using WiringPi, you can call mcp23017Setup(pin_base2, i2c_addr2) for your second chip.
I had trouble getting the addresses to work initially, and it turned out the problem wasn't in my understanding of the address line wiring, but that one of my cheap Chinese breadboard had a bad power and ground bus in one quadrant. That's a good lesson for the future: when things don't work as expected, don't assume the breadboard is above suspicion.
Using two MCP23017 chips with their built-in pullup resistors simplified the wiring for my music keyboard enormously, and it made the code cleaner too. Here's the modified code: keyboard.py on GitHub.
What about the speed? It is indeed quite a bit faster than the shift register code. But it's still too laggy to use as a real music keyboard. So I'll still need to do more profiling, and maybe find a faster way of generating notes, if I want to play music on this toy.
[ 15:44 Feb 17, 2018 More hardware | permalink to this entry | ]