NealB
Posts: 13
Joined: Wed Jul 11, 2012 11:58 am

MCP23017 i2C Expander Board

Tue Sep 11, 2012 12:23 pm

I have built the 16Port I2C Expander Board created by Nathan Chantrell using the Microchip MCP23017 16Port I2C Expansion Board. I Have no problem controlling the ports as outputs using Python.
I would like to use the chip for inputs, however I can'ts seem to get it to work.
I was advised to use:
Aval = bus.read_byte_data(0×20,0×12)
However it wasn't successful.
Does anybody know what the Python code is to Set and Read the ports through I2C.

User avatar
Grumpy Mike
Posts: 1005
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District

Re: MCP23017 i2C Expander Board

Thu Sep 13, 2012 2:33 pm

This link will show you the commands for the SMBus drivers:-
http://wiki.erazor-zone.de/wiki:linux:python:smbus:doc
You can either use a read_word_data(addr,cmd) command to get two bytes back or for longer records a read_block_data(addr,cmd)
The problem is that it depends on how you have set the bank register up.
When you say no luck what do you mean? Have you set the data direction register correctly?

However you are better posting a small piece of code that illustrates your problem.

davejittla
Posts: 4
Joined: Wed Sep 12, 2012 8:47 pm

Re: MCP23017 i2C Expander Board

Fri Sep 14, 2012 9:22 am

Hi Neal
As another newby, I was having the same problem but have found that I can read the ports in decimal by applying +5V to the inputs using the following script in Python.
------------------------------------------------

import smbus
import sys
import getopt
import time
bus = smbus.SMBus(0)


delay = 0.3

bus.write_byte_data(0x20,0x00,0xff) # input
bus.write_byte_data(0x20,0x01,0xff) # input


while 1:
a = bus.read_byte_data(0x20,0x12)
print a
time.sleep(delay)
b = bus.read_byte_data(0x20,0x13)
print b
time.sleep(delay)
----------------------------------------------------------------
Lines under 'while 1:' need to be tabbed.
Also the delay is only there to be able to see the numbers on the screen as otherwise they disappear too quickly!

I am still learning but hope the above helps.

NealB
Posts: 13
Joined: Wed Jul 11, 2012 11:58 am

Re: MCP23017 i2C Expander Board

Sun Sep 16, 2012 9:26 am

Hello Grumpy Mike

The basics of the code I have been trying is:

import smbus
import sys
import time

bus=smbus.SMBus(0)

bus.write_byte_byte(0x20,0x00,0x80) # Port A with A0-A6 outputs A7 input
bus.write_byte_byte(0x20,0x01,0x00) # Port B with B0-A7 outputs

Aval=bus.read_byte_data(0x20,0x12) # Read Port A
Aval =Aval-127 # Change the value of Aval to be being 0 or 128 to represent low or high input on Port A7

I hope to eventually hope to us the ports:
A7 input
A6 ouput
A5 input
A4 output
A3 input
A2 output
A1 input
A0 output

This is to give allow me to give a visual output against an input and to reduce any complicated wiring further down the line.

NealB
Posts: 13
Joined: Wed Jul 11, 2012 11:58 am

Re: MCP23017 i2C Expander Board

Sun Sep 16, 2012 9:27 am

Hi davejittla
Thank you for your response.

I will give the code a try.

Thank you

NealB
Posts: 13
Joined: Wed Jul 11, 2012 11:58 am

Re: MCP23017 i2C Expander Board

Sun Sep 16, 2012 10:21 am

Hello Grumpy Mike

Sorry the reply and code posted seems to have got mixed up - heres a second go:

import smbus
import sys
import time

bus=smbus.SMBus(0)

bus.write_byte_data(0x20,0x00,0x80) # Port A with A0-A6 outputs A7 input
bus.write_byte_data(0x20,0x01,0x00) # Port B with B0-B7 outputs

#Read register for Port A and place in variable Aval
Aval=bus.read_byte_data(0x20,0x12)

# Read the value of Aval and change the value to look at port A7
Aval =Aval-127

# Set the value of variable A7 to represnt the state of port A7
if Aval <=0
A7 = 0
else A7 = 1

User avatar
Grumpy Mike
Posts: 1005
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District

Re: MCP23017 i2C Expander Board

Sun Sep 16, 2012 6:51 pm

First thing I would do is to enable the pull up resistor with a
bus.write_byte_data(0x20,0x0C,0x80)
Then print Aval as soon as you read it.
To isolate bit 7 then do a
Aval = Aval >> 7

NealB
Posts: 13
Joined: Wed Jul 11, 2012 11:58 am

Re: MCP23017 i2C Expander Board

Sun Sep 16, 2012 7:33 pm

Thanks Grumpy Mike

I will make the recommended changes

Return to “Python”