I'm trying to add a button to my MCP23017 board. But my code is not working. What am i doing wrong?

import smbus
import sys
import getopt
import time 
bus = smbus.SMBus(1)
bus.write_byte_data(0x20,0x00,0xff) # input
bus.write_byte_data(0x20,0x01,0xff) # input
address = 0x20 # I2C address of MCP23017

# Handle the command line arguments
def main():
  while True:
    print(bus.read_byte_data(address, 0x12))

if __name__ == "__main__":

print is 0, even if i press the button

  • 2
    Do you have a schematic? – John La Rooy Jun 3 '13 at 19:33

Without seeing a schematic, as @gnibbler said, I would guess that it is a circuit issue. Try setting up your circuit as if it were voltage divider, with a pullup resistor to 3.3v. The size isn't that important for a proof of concept, but I would say ~1k is what I would shoot for. See the schematic below for a simple explanation.

enter image description here

| improve this answer | |
  • @user6827 Any time! Throw an up vote at gnibblers comment, he deserves credit as well. – Butters Jun 4 '13 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.