mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

MCP23017 and i2c problems

Tue Jan 27, 2015 11:02 pm

Hey all,

I've had a lot of success using the standard GPIO pins on my Raspberry Pi B+, but I was ready to get even more pins, so I decided to buy the Adafruit MCP23017 port expander chip. I diligently followed all the instructions on Adafruit's instructional guide https://learn.adafruit.com/mcp230xx-gpi ... i?view=all however I'm having problems.

My i2c seems to be working fine, given when I type "i2cdetect -y 1" then I can see the chip recognized as "20" on the 0x20 spot. So I think my problem must be software-related when I try to activate the two LEDs from the Adafruit instructional. Per their instructions, I create a script with the following code:

mcp = Adafruit_MCP230XX(busnum = 1, address = 0x20, num_gpios = 16)
mcp.config(0, 1)
while (True):
mcp.output(0, 1) # Pin 0 High
mcp.output(0, 0) # Pin 1 Low

And I expect one of the LEDs to start firing. However, instead, I get the message "NameError: name 'Adafruit_MCP230XX' is not defined.

Any ideas what I'm doing wrong? Thanks in advance!

User avatar
mikronauts
Posts: 2717
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: MCP23017 and i2c problems

Tue Jan 27, 2015 11:04 pm

I am guessing you forgot to import the library.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: MCP23017 and i2c problems

Wed Jan 28, 2015 3:12 pm

Hey Milkronauts,

So how would I import the library? Sorry I'm new to this!

User avatar
PeterO
Posts: 4954
Joined: Sun Jul 22, 2012 4:14 pm

Re: MCP23017 and i2c problems

Wed Jan 28, 2015 3:18 pm

mabrow8454 wrote:Hey Milkronauts,

So how would I import the library? Sorry I'm new to this!
Did you follow the section called "using the library" on the adafruit ling you gave ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: MCP23017 and i2c problems

Thu Jan 29, 2015 7:04 pm

Hey guys, I had originally tried to handle the "using the library" section manually... but I finally decided to relent and follow the insutrctions for the "Using the WebIDE" option. That seemed to work, since I ran the script from the browser, and it's responding successfully:

Starting blinky on pin 0 (CTRL+C to quit)

The only problem is that the LED is not blinking. See attached screenshot of my setup. I even tried moving the LED around to other pins too. I know the device is getting recognized by the Raspberry Pi because I ran the i2c detect tool successfully.

Any chance I got a defective MCP23017 chip?
Attachments
IMG_3556.JPG
IMG_3556.JPG (43.86 KiB) Viewed 4202 times

User avatar
PeterO
Posts: 4954
Joined: Sun Jul 22, 2012 4:14 pm

Re: MCP23017 and i2c problems

Thu Jan 29, 2015 7:11 pm

mabrow8454 wrote: See attached screenshot of my setup.
Your attached picture is mostly of your desk and is too small to actually be able to tell what is wired up. ;)

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 35840
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: MCP23017 and i2c problems

Thu Jan 29, 2015 7:40 pm

Take a look here: http://hertaville.com/2013/04/01/interf ... i-using-c/ there's a good Fritzing diagram of how to wire the MCP23017.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: MCP23017 and i2c problems

Fri Jan 30, 2015 12:32 am

Hey Dougie,

Thanks! That was an incredibly informative article. Now I actually understand what's going on with this setup (rather than just following Adafruit's guide).

That being said, I still can't get my LED to turn on. Like I mentioned, the i2c detect is recognizing the device. But then when I follow these final instructions:

i2cset -y 0x20 0x00 0x00
i2cset -y 1 0x20 0x14 0x01

Nothing happens. I decided to check my wires and LED using standard GPIO, and those are good to go and working successfully.

So the only thing I can think of is that maybe I'm using the wrong resistor? I didn't have a 470 ohm, so I just grabbed a 220 ohm. But I actually don't think that's it, because nothing happens even when I pull the resistor out (which I would think would cause an LED flareup and then burnout).

So the only thing that I can think of is maybe I've got a defective MCP23017 chip that can be recognized by RPi but can't communicate with RPi for whatever reason. So I'm ordering a new chip now to try again in a few days.

User avatar
DougieLawson
Posts: 35840
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: MCP23017 and i2c problems

Fri Jan 30, 2015 12:51 am

Does the MCP23017 show up on a sudo i2cdetect -y 1 command?
Is your id in the I2C group sudo useradd -G i2c pi?

Have you got SDA & SCL round the right way? Have you got Vdd connected to 3V3 or 5V0? Have you got Vss connected to GND?
Are the address lines A0, A1 & A2 tied to Vss (with a 10K resistor) or tied to Vdd and not floating?
Is the ¯RESET¯ line tied to Vdd (with a 10K resistor)?
Are the GND lines connected to pin#6 (GND) on your Pi?
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: MCP23017 and i2c problems

Fri Jan 30, 2015 11:09 pm

Hey Dougie, here's answers to each of your questions:

1. Yes, sudo i2cdetect -y 1 shows the device appearing on 0x20.

2. Yes, entering that command showed "User 'pi' already exists."

3. Yes, see attached photos. Yellow line is SDA, and green line is SCL, just like Adafruit's instructions. Interestingly, I swapped these wires and then ran the command as a test, and suddenly the RPi responded "Error: write failed."

4. I'm not sure exactly what you're asking here but I can confirm that all the address pins (A0, A1, and A2) are connected to Ground. Vss is also connected to Ground. However, I didn't use any 10K resistors since the Adafruit guide didn't call for that.

5. RESET line and Vdd are both tied to 3.3V. I didn't use any 10K resistors since the Adafruit guide didn't call for that.

6. Yes, Ground on RPi is connected to Ground on breadboard.


Given my answer on #3, it would appear that the RPi is recognizing and communicating with the device successfully. So somehow the LED just isn't getting turned on. Could this be related to my 220ohm resistor because the MCP23017 doesn't provide the same volt charge as I'm used to from a standard GPIO pin?

Can you tell me what command I would use to test pin 2 or pin 3? Would that be the following?

i2cset -y 1 0x20 0x15 0x01
i2cset -y 1 0x20 0x16 0x01
Attachments
IMG_3561.JPG
IMG_3561.JPG (45.75 KiB) Viewed 4074 times
IMG_3559.JPG
IMG_3559.JPG (45.88 KiB) Viewed 4074 times

User avatar
DougieLawson
Posts: 35840
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: MCP23017 and i2c problems

Fri Jan 30, 2015 11:21 pm

Try this sequence of commands
i2cset -y 1 0x20 0x00 0x00 # IODIRA all pins outputs
i2cset -y 1 0x20 0x01 0x00 # IODIRB all pins outputs
i2cset -y 1 0x20 0x14 0xff # OLATA all pins high
i2cset -y 1 0x20 0x15 0xff # OLATB all pins high
i2cset -y 1 0x20 0x15 0x00 # OLATB all pins low
i2cset -y 1 0x20 0x14 0x00 # OLATA all pins low
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: MCP23017 and i2c problems

Fri Jan 30, 2015 11:43 pm

Hey Dougie,

I've got some great news! I ran the commands you mentioned, and also re-worked my setup slightly, and it's working! Check out the attached photo.

I'm thinking I must've had some math wrong with my resistors. As you can see, when I connect the MCP23017 > LED > straight to Ground, it all works.

Thanks so much for all your help this week... it was a long journey but really happy we got the win. :D
Attachments
IMG_3562.JPG
IMG_3562.JPG (41.16 KiB) Viewed 4046 times

User avatar
DougieLawson
Posts: 35840
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: MCP23017 and i2c problems

Fri Jan 30, 2015 11:46 pm

You really should put the pictures on http://imgur.com and post a link here.

Well done, you can start working with python now (it's easier than i2cset and i2cget).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: MCP23017 and i2c problems

Sat Jan 31, 2015 9:41 pm

Hey Dougie,

Good to know. I'll be sure to use Imgur moving forward. Good to know about Python. By the way, what would a simple Python script look like that turns on Pin 1? What about Pin 2, and Pin 3?

Thanks in advance!

User avatar
DougieLawson
Posts: 35840
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: MCP23017 and i2c problems

Sat Jan 31, 2015 10:09 pm

I wrote this (last week for a project I'm doing with 16 relays)

Code: Select all

#!/usr/bin/python

# (C) 2015 Copyright Dougie Lawson, All rights reserved
# Version 0.6.a

import smbus

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

bus=smbus.SMBus(1)

# Every pin is set as output!
bus.write_byte_data(ADDR,IODIRA,0x00)
bus.write_byte_data(ADDR,IODIRB,0x00)
global valueA
global valueB
valueA = 0x00
valueB = 0x00

def pinOn(bank, pin):
#  print (bank,  pin, "== ON")
  global valueA
  global valueB
  bit = pin - 1
  if bank == 'B':
    valueB = valueB | (1 << bit)
    bus.write_byte_data(ADDR, OLATB, valueB)
  else:
    valueA = valueA | (1 << bit)
    bus.write_byte_data(ADDR, OLATA, valueA)

def pinOff(bank, pin):
#  print (bank, pin, "== OFF")
  global valueA
  global valueB
  bit = pin - 1
  if bank == 'B':
    valueB = valueB & (0xff - (1 << bit))
    bus.write_byte_data(ADDR, OLATB, valueB)
  else:
    valueA = valueA & (0xff - (1 << bit))
    bus.write_byte_data(ADDR, OLATA, valueA)

def pinStatus(bank, pin):
  global valueA
  global valueB
  bit = pin - 1
  if (bank == 'A'):
    state = ((valueA&(1<<bit))!=0)
  else:
    state = ((valueB&(1<<bit))!=0)
  return state

def pinAllOff():
#  print "All off called"
  global valueA
  global valueB
  valueA = 0x0
  bus.write_byte_data(ADDR, OLATA, valueA)
  valueB = 0x0
  bus.write_byte_data(ADDR, OLATB, valueB)
assuming it's in file called pinControl.py you'd call it like this

Code: Select all

#!/usr/bin/python
import pinControl as p

p.pinOn('A',1)
p.pinOn('A',7)
p.pinOn('B',5)
print p.pinStatus('A',1)
print p.pinStatus('A',7)
print p.pinStatus('B',5)
p.pinOff('B',5)
p.pinOff('A',1)
p.pinAllOff()
I should probably add more code to check for invalid bank names and invalid pin numbers.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: MCP23017 and i2c problems

Thu Feb 19, 2015 10:33 pm

Hey Dougie,

Me again. I finally got around to trying your code, and it doesn't seem to be working for me. But I'm not advanced enough to try to troubleshoot all these lines that I don't understand, so can you tell me a very simple Python script to help me fire the setup? For example, I'd love something like test.py that takes the following actions:

i2cset -y 1 0x20 0x01 0x00 # IODIRB all pins outputs
i2cset -y 1 0x20 0x15 0xff # OLATB all pins high

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: MCP23017 and i2c problems

Sat Feb 28, 2015 12:31 am

Hey guys,

Can anyone tell me a very simple Python script to help me fire what I've got connected to my MCP23016 chip? I've got it working now when I run scripts from the command line, but would like to know how to do it in Python as well. For example, I'd love something like test.py that takes the following actions:

i2cset -y 1 0x20 0x01 0x00 # IODIRB all pins outputs
i2cset -y 1 0x20 0x15 0xff # OLATB all pins high

ktb
Posts: 1380
Joined: Fri Dec 26, 2014 7:53 pm

Re: MCP23017 and i2c problems

Sat Feb 28, 2015 1:38 am

Forgive me if I am wrong because I've never done this before and have no real Python experience, but does something like the following work?

Code: Select all

#!/usr/bin/python

import smbus

i2c1 = smbus.SMBus(1)

DEVICE_ADDRESS = 0x20
DEVICE_REG_MODE1 = 0x01
DEVICE_REG_MODE2 = 0x15

i2c1.write_byte_data(DEVICE_ADDRESS, DEVICE_REG_MODE1, 0x00)
i2c1.write_byte_data(DEVICE_ADDRESS, DEVICE_REG_MODE2, 0xff)

User avatar
DougieLawson
Posts: 35840
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: MCP23017 and i2c problems

Sat Feb 28, 2015 10:21 am

ktb wrote:Forgive me if I am wrong because I've never done this before and have no real Python experience, but does something like the following work?

Code: Select all

#!/usr/bin/python

import smbus

i2c1 = smbus.SMBus(1)

DEVICE_ADDRESS = 0x20
DEVICE_REG_MODE1 = 0x01
DEVICE_REG_MODE2 = 0x15

i2c1.write_byte_data(DEVICE_ADDRESS, DEVICE_REG_MODE1, 0x00)
i2c1.write_byte_data(DEVICE_ADDRESS, DEVICE_REG_MODE2, 0xff)
Looks enough like http://www.raspberrypi.org/forums/viewt ... 07#p682007 that it should work.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

mabrow8454
Posts: 54
Joined: Sat Jan 24, 2015 9:30 pm

Re: MCP23017 and i2c problems

Fri Mar 06, 2015 8:48 pm

That worked! Thanks so much guys

Return to “Troubleshooting”