Code Monkey home page Code Monkey logo

mcp230xx-python-module's Introduction

MCP230XX-Python-Module

Python 2.x and 3.x module to use the MCP23017 or MCP23008 GPIO expander chip and the Raspberrypi. This module supports using the interrupt capabilities on the MCP230XX chips

This requires the smbus module for the i2c connection and the RPi.GPIO or compatible module to use the interrupt capabilities.

Included at the end of the module are some example uses of the module

Connection to the MCP230XX from the Pi are as follows

  • Pi SCL to MCP SCL
  • Pi SDA to MCP SDA
  • Pi 3.3V to MCP VDD
  • Pi Gnd to MCP Vss
  • Pi 3.3V to MCP RESET (Could be tied to a Pi GPIO if you want active control of the RESET)
  • Pi 3.3V to MCP A0
  • Pi Gnd to MCP A1
  • Pi Gnd to MCP A2
  • Pi GPIO # to MCP INTA
  • Pi GPIO # to MCP INTB

With the above A0-A2 connections the i2c address is 0x21

For the MCP23008 the MCP GPIOA 0-7 are IO 0-7 in this module For the MCP23017 the MCP GPIOA 0-7 are IO 0-7 and the MCP GPIOB 0-7 are IO 8-15 in this module

Current functions include:

  • interrupt_options(outputType = 'activehigh', bankControl = 'separate')
  • set_register_addressing(regScheme='8bit')
  • set_mode(pin, mode, pullUp='disable')
  • invert_input(pin, invert = False)
  • output(pin, value)
  • input(pin)
  • add_interrupt(pin, callbackFunctLow='empty', callbackFunctHigh='empty')
  • remove_interrupt(pin)
  • register_reset()

Example uses

to initialize the chip

MCP = MCP230XX('MCP23017', i2cAddress, '16bit')

to set up an input and output

MCP.set_mode(0, 'output') # set IO 0 to an output MCP.set_mode(1, 'input') # set IO 1 to an input MCP.set_mode(10, 'input', 'enable') # set IO 10 to an input with the pullup enabled

to set an output

MCP.output(0,1) # set IO 0 high MCP.output(0,0) # set IO 0 low

to read an input

MCP.input(1) # reads current value on IO 1, return 0 for low and 1 for high

to set interrupt options

MCP.interrupt_options(outputType = 'activehigh', bankControl = 'separate')

  • the interrupt pins can be set as either activehigh, activelow or opendrain
  • for the MCP23017 the interrupts for IO 0-7 and IO 8-15 can be trigger INTA and INTB respectively with bankControl = 'seperate' or and an intterupt on any of the IO (0-15) can be sent to both INTA and INTB with bankControl = 'both'

to add and remove an interrupt to an input

MCP.add_interrupt(10, callbackFunctLow=functA, callbackFunctHigh=functB) # add interrupt to IO 10 with call back functions functA and functB

  • separate callback functs can be set for when the pin goes high and low, the same function can be used for callbackFunctLow and callbackFunctHigh

On the Raspberry Pi side using the RPi.GPIO module as IO, the following needs to be included in your program IO.add_event_detect(intPin,IO.RISING,callback=MCP.callbackB)

callback = either MCP.callbackA or MCP.callbackB if using the bankControl = 'seperate' or callback = MCP.callbackBoth if using the bankControl = 'both'

MCP.remove_interrupt(10) # remove interrupt from IO 10

mcp230xx-python-module's People

Contributors

owainm713 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mcp230xx-python-module's Issues

Example using interrupts

I am trying to add an interrupt to pin 0 on the chip and then on my pi pin 16 listens for the interrupt from one of the interrupt pins on the chip. I think I am close. Right now I can see the button presses but no interrupts fire.

from mcp230xx import MCP230XX

import time, os
import RPi.GPIO as IO

IO.setmode(IO.BCM)
IO.setup(21, IO.IN, pull_up_down=IO.PUD_UP)

MCP = MCP230XX('MCP23017', 0x27, '16bit')
MCP.set_mode(0, 'input', 'enable')

MCP.interrupt_options(outputType = 'activehigh', bankControl = 'both')
	
def functA():
	print('abc')

def functB():
	print('efg')

MCP.add_interrupt(0, callbackFunctLow=functA, callbackFunctHigh=functB) 

IO.add_event_detect(16, IO.FALLING, callback = MCP.callbackBoth)

while True:
	print(MCP.input(0))
	time.sleep(.2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.