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/
Locked
idem
 
Posts: 32
Joined: Thu Aug 11, 2011 7:39 am

Read switch with MCP23017

Post by idem »

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 ?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Read switch with MCP23017

Post by adafruit_support_rick »

Please post your code.

idem
 
Posts: 32
Joined: Thu Aug 11, 2011 7:39 am

Re: Read switch with MCP23017

Post by idem »

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'

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Read switch with MCP23017

Post by adafruit_support_rick »

You have to configure the pin as input:

Code: Select all

#!/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)

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: Read switch with MCP23017

Post by tldr »

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

    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.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Read switch with MCP23017

Post by adafruit_support_rick »

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.

idem
 
Posts: 32
Joined: Thu Aug 11, 2011 7:39 am

Re: Read switch with MCP23017

Post by idem »

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 7:39 am

Re: Read switch with MCP23017

Post by idem »

Thanks a lot..
It works fine.

That's why I stiil bought at Adafruit. They have the best support :)

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Read switch with MCP23017

Post by adafruit_support_rick »

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!

idem
 
Posts: 32
Joined: Thu Aug 11, 2011 7:39 am

Re: Read switch with MCP23017

Post by idem »

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

#!/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 7:39 am

Re: Read switch with MCP23017

Post by idem »

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

    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?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Read switch with MCP23017

Post by adafruit_support_rick »

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.

Locked
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/

Return to “Adafruit Raspberry Pi® accessories”