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 ?
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/
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/
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Read switch with MCP23017
Please post your code.
-
- Posts: 32
- Joined: Thu Aug 11, 2011 7:39 am
Re: Read switch with MCP23017
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'
# 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'
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Read switch with MCP23017
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)
-
- Posts: 466
- Joined: Thu Aug 30, 2012 1:34 am
Re: Read switch with MCP23017
the power on state for the i/o direction registers is 0xff - all inputs. also the constructor for the mcp230xx class sets those bits.adafruit_support_rick wrote:You have to configure the pin as input:
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)
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Read switch with MCP23017
What can I say - it's the first python program I ever wrote.
I guess there must be something wrong with idem's circuit.
I guess there must be something wrong with idem's circuit.
-
- Posts: 32
- Joined: Thu Aug 11, 2011 7:39 am
Re: Read switch with MCP23017
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....
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....
-
- Posts: 32
- Joined: Thu Aug 11, 2011 7:39 am
Re: Read switch with MCP23017
Thanks a lot..
It works fine.
That's why I stiil bought at Adafruit. They have the best support
It works fine.
That's why I stiil bought at Adafruit. They have the best support
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Read switch with MCP23017
Great!idem wrote:Thanks a lot..
It works fine.
Thanks!idem wrote:That's why I stiil bought at Adafruit. They have the best support
-
- Posts: 32
- Joined: Thu Aug 11, 2011 7:39 am
Re: Read switch with MCP23017
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.
Any idea?
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)
-
- Posts: 32
- Joined: Thu Aug 11, 2011 7:39 am
Re: Read switch with MCP23017
Hey, this have a link with my problem I think....no?tldr wrote:the power on state for the i/o direction registers is 0xff - all inputs. also the constructor for the mcp230xx class sets those bits.adafruit_support_rick wrote:You have to configure the pin as input:
i tried giving that advise myself, once, and got shot down.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)
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Read switch with MCP23017
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.
That's why I don't understand why your door didn't work when you first tried it.
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/
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/