Posts: 27
Joined: Thu Dec 04, 2014 1:27 pm


Sun Mar 01, 2015 7:23 pm

hi there,

i finally figured out the i²c bus/mcp23017 (i asked a couple of questions on this forum, but i didn't know you had to write a binary number on the bus :) ).
i managed to control a stepper motor using i²c, but only with the a0-7 ports. i did some research, but i still can't figure out how to access the b0-7 ports. i use this command to control the a-ports:

bus = smbus.SMBus(1)

0x20 is the adress, and 0x00 makes it work with the a-ports. i read online 0x01 should work with the b ports, but that doesn't seem to work. does anyone know what i am doing wrong?

thanks in advance,

User avatar
Posts: 38554
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: mcp23017

Sun Mar 01, 2015 7:38 pm

ADDR = 0x20
IODIRA = 0x00
IODIRB = 0x01
GPIOA = 0x12
GPIOB = 0x13
OLATA = 0x14
OLATB = 0x15

With the IODIR registers a 0 bit sets the port as output, a 1 bit set the port as input.

You read input data on the GPIO registers, you write output data on the OLAT registers.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: mcp23017

Sun Mar 01, 2015 8:01 pm


address = i2c address of mcp23017 (0x20, etc)

where regaddr = 0x13 or 0x14 0x13 = port "B" 0x14 = port "A"
value = 8 bit value for turning on/off outputs.

You can use the "&" function to turn bits on or off.

port 1 - 0x01
port 2 = 0x02
port 3 = 0x04
port 4 = 0x08
port 5 = 0x10
port 6 = 0x20
port 7 = 0x40
port 8 = 0x80

to set all ports to outputs

Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:

Return to “Interfacing (DSI, CSI, I2C, etc.)”