Read switch with MCP23017
Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Read switch with MCP23017

by idem on Mon May 06, 2013 3:28 am

Hi all,
I try to use a MCP23017 with my raspberry, and coding with Python.
I can switch ON/OFF my Leds.
I can't read my switch door.

Can anyone help me ?
idem
 
Posts: 32
Joined: Thu Aug 11, 2011 6:39 am
Location: FRANCE

Re: Read switch with MCP23017

by adafruit_support_rick on Mon May 06, 2013 8:02 am

Please post your code.
User avatar
adafruit_support_rick
 
Posts: 11661
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Read switch with MCP23017

by idem on Tue May 07, 2013 3:05 am

something really basic like this :

# Python libraries
import RPi.GPIO as GPIO
from Adafruit_MCP230xx import *

# Use busnum = 1 for new Raspberry Pi's (512MB with mounting holes)
mcp = Adafruit_MCP230XX(busnum = 1, address = 0x20, num_gpios = 16)

# Set pin 0 to input with the pullup resistor enabled
# Don't know if my switch has to be set as pulup
mcp.pullup(0, 0)

while (True):
if mcp.input(0) == 0:
print '0'
if mcp.input(0) == 1:
print '1'
idem
 
Posts: 32
Joined: Thu Aug 11, 2011 6:39 am
Location: FRANCE

Re: Read switch with MCP23017

by adafruit_support_rick on Tue May 07, 2013 6:17 pm

You have to configure the pin as input:
Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/python

from Adafruit_MCP230xx import Adafruit_MCP230XX
import time

mcp = Adafruit_MCP230XX(0x20, 16)

mcp.config(4, mcp.INPUT)
mcp.pullup(4, True)

while (True):
  if mcp.input(4) == 0:
      print '0'
      time.sleep(1)

  if mcp.input(4) == 1:
      print '1'
      time.sleep(1)
User avatar
adafruit_support_rick
 
Posts: 11661
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Read switch with MCP23017

by tldr on Wed May 08, 2013 12:08 am

adafruit_support_rick wrote:You have to configure the pin as input:


the power on state for the i/o direction registers is 0xff - all inputs. also the constructor for the mcp230xx class sets those bits.

Code: Select all | TOGGLE FULL SIZE
    def __init__(self, address, num_gpios=8, busnum=-1, debug=False):

        assert 0 < num_gpios < 17, "Number of GPIOs must be between 1 and 16"

        self.i2c = Adafruit_I2C(address, busnum, debug)
        self.num_gpios = num_gpios
        self.pullups = 0

        # Set default pin values -- all inputs with pull-ups disabled.
        # Current OLAT (output) value is polled, not set.
        if num_gpios <= 8:
            self.direction = 0xFF
            self.i2c.write8(self.MCP23008_IODIR, self.direction)
            self.i2c.write8(self.MCP23008_GPPU , self.pullups)
            self.outputvalue = self.i2c.readU8(self.MCP23008_OLAT)
        else:
            self.direction = 0xFFFF
            self.i2c.write16(self.MCP23017_IODIRA, self.direction)
            self.i2c.write16(self.MCP23017_GPPUA , self.pullups)
            self.outputvalue = self.i2c.readU16(self.MCP23017_OLATA)


i tried giving that advise myself, once, and got shot down.
"If I had known it was harmless, I would have killed it myself." - Phillip K. Dick, A Scanner Darkly
User avatar
tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 12:34 am

Re: Read switch with MCP23017

by adafruit_support_rick on Wed May 08, 2013 8:14 am

What can I say - it's the first python program I ever wrote. :roll:
I guess there must be something wrong with idem's circuit.
User avatar
adafruit_support_rick
 
Posts: 11661
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Read switch with MCP23017

by idem on Wed May 08, 2013 2:34 pm

hummm....
I don't think so, as I said, I can light on my leds, so output are OK.
I've just plug a switch door on pin 8, and now, I'll test your code....
idem
 
Posts: 32
Joined: Thu Aug 11, 2011 6:39 am
Location: FRANCE

Re: Read switch with MCP23017

by idem on Wed May 08, 2013 2:37 pm

Thanks a lot..
It works fine.

That's why I stiil bought at Adafruit. They have the best support :)
idem
 
Posts: 32
Joined: Thu Aug 11, 2011 6:39 am
Location: FRANCE

Re: Read switch with MCP23017

by adafruit_support_rick on Wed May 08, 2013 2:43 pm

idem wrote:Thanks a lot..
It works fine.

Great! :D

idem wrote:That's why I stiil bought at Adafruit. They have the best support
Thanks!
User avatar
adafruit_support_rick
 
Posts: 11661
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Read switch with MCP23017

by idem on Wed May 08, 2013 2:56 pm

Just an other problem....

This code read any input..I can plug my switch door on Pin 0, or Pin 1, it read it anywhere.

Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/python

from Adafruit_MCP230xx import Adafruit_MCP230XX
import time

#mcp = Adafruit_MCP230XX(0x20, 16)
mcp = Adafruit_MCP230XX(busnum = 1, address = 0x20, num_gpios = 16)

mcp.config(1, mcp.INPUT)
mcp.pullup(1, True)

while (True):
        if mcp.input(1) == 0:
                print '0'
                time.sleep(1)

        if mcp.input(1) == 1:
                print '1'
                time.sleep(1)


Any idea?
idem
 
Posts: 32
Joined: Thu Aug 11, 2011 6:39 am
Location: FRANCE

Re: Read switch with MCP23017

by idem on Wed May 08, 2013 2:57 pm

tldr wrote:
adafruit_support_rick wrote:You have to configure the pin as input:


the power on state for the i/o direction registers is 0xff - all inputs. also the constructor for the mcp230xx class sets those bits.

Code: Select all | TOGGLE FULL SIZE
    def __init__(self, address, num_gpios=8, busnum=-1, debug=False):

        assert 0 < num_gpios < 17, "Number of GPIOs must be between 1 and 16"

        self.i2c = Adafruit_I2C(address, busnum, debug)
        self.num_gpios = num_gpios
        self.pullups = 0

        # Set default pin values -- all inputs with pull-ups disabled.
        # Current OLAT (output) value is polled, not set.
        if num_gpios <= 8:
            self.direction = 0xFF
            self.i2c.write8(self.MCP23008_IODIR, self.direction)
            self.i2c.write8(self.MCP23008_GPPU , self.pullups)
            self.outputvalue = self.i2c.readU8(self.MCP23008_OLAT)
        else:
            self.direction = 0xFFFF
            self.i2c.write16(self.MCP23017_IODIRA, self.direction)
            self.i2c.write16(self.MCP23017_GPPUA , self.pullups)
            self.outputvalue = self.i2c.readU16(self.MCP23017_OLATA)


i tried giving that advise myself, once, and got shot down.


Hey, this have a link with my problem I think....no?
idem
 
Posts: 32
Joined: Thu Aug 11, 2011 6:39 am
Location: FRANCE

Re: Read switch with MCP23017

by adafruit_support_rick on Wed May 08, 2013 3:00 pm

I think so. It looks like the pins default to INPUT.

That's why I don't understand why your door didn't work when you first tried it.
User avatar
adafruit_support_rick
 
Posts: 11661
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY