Interfacing to the I2C bus on the WiPy 2.0
I have a prototype circuit on a breadboard that uses an MCP23017 with a 5v supply.
Both the Raspberry Pi and the WiPy 2.0 data sheets say that the maximum voltage on SCA and SCL is 3.3v but I can connect my prototype circuit to SCA/SCL on a Raspberry Pi without using a level shifter by using the pull up resistors on the Pi to pull the lines up to 3.3v rather than an external pull up resistor (that would pull the lines up to 5v and damage the Pi).
The question is can I do the same thing with the WiPy 2.0, or do I need to worry about adding pull up resistors? The WiPy is a nice little device but I'm still getting to know it - I'd rather not let the magic blue smoke out just yet...
Tidied things up a bit and added some constants to make it 'easier' to see what is going on, may get round to writing a module to for the MCP23017.
Any guidance on what the interfaces should look like any where would be useful..?
#!/usr/bin/python # # This program is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License along # with this program. If not, see <http://www.gnu.org/licenses/> # I2CBUS = 0x00 # I2C bus number (Always 0 on WiPy) ADDRESS = 0x20 # I2C address of I/O expander. IODIRA = 0x00 # Port A data direction register. IODIRB = 0x01 # Port B data direction register. IPOLA = 0x02 IPOLB = 0x03 GPINTENA = 0x04 GPINTENB = 0x05 DEFVALA = 0x06 DEFVALB = 0x07 INTCONA = 0x08 INTCONB = 0x09 IOCON = 0x0A GPPUA = 0x0C GPPUB = 0x0D INTFA = 0x0E INTFB = 0x0F INTCAPA = 0x10 INTCAPB = 0x11 GPIOA = 0x12 # Port A data register. GPIOB = 0x13 # Port B data register. OLATA = 0x14 OLATB = 0x15 from machine import I2C # Initialize I2C bus i2c = I2C(I2CBUS, I2C.MASTER, baudrate=100000) # Configure all pins on Port A and B as outputs. i2c.writeto_mem (ADDRESS, IODIRA, bytes([0x00, 0x00])) # Send 0xf0 to Port A data register (GPIOA) i2c.writeto_mem (ADDRESS, GPIOA, bytes([0xf0])) # Clear all outputs and reset data direction bits. i2c.writeto_mem (ADDRESS, GPIOA, bytes([0x00,0x00])) i2c.writeto_mem (ADDRESS, IODIRA, bytes([0x00, 0x00]))
I was going to post something slightly more useful, but had a hardware problem that has stopped me developing anything further for now (think it was just a broken jumper cable but I haven't had time to do any more yet).
Finally got round to trying this and it worked just fine.
I had an MCP23017 connected to the I2C bus on my Raspberry Pi which I simply conencted to my WiPy 2.0 using the P9/P10 (SCA/SCL) on the expansion board after removing the LED jumper.
I powered the MCP23017 using the P26/P25 (5V/GND) and everything seems to work though you do have to make sure you send data as an array of bytes (containing just one byte!).
from machine import I2C i2c = I2C(0, I2C.MASTER, baudrate=100000) i2c.writeto_mem (0x20, 0x00, bytes([0x00, 0x00])) # Configure all pins on Port A and B as outputs using the data direction registers (IODIRA/IODIRB). i2c.writeto_mem (0x20, 0x12, bytes([0xf0])) # Send 0xf0 to Port A data register (GPIOA) i2c.writeto_mem (0x20, 0x12, bytes([0x00,0x00])) # Send 0x00 to Port A and B data registesr (GPIOA/GPIOB)
Excellent news I can't wait to try it later
I2C is pulled up internally to 3.3v on the WiPy so it should work, provided you're not pulling it up to 5V anywhere else.