Giter Club home page Giter Club logo

pi-mcp23017's Introduction

pi-mcp23017

This is a small library to interface the popular I²C MCP23017 portexpander with a Raspberry Pi and Python. A threaded approach makes sure, that interrupt capability of the expander works properly.

Dependencies

There are just these three python dependencies:

  • python>=3.6
  • smbus
  • RPi.GPIO

Installation

Just install the package with pip or your favorite package manager:
pip install pi-mcp23017

Usage

Connect the Port Expander to your Raspberry Pi and find out the address. If you haven't configured the address pins, it should be under 0x20. Also make sure to note the GPIO pin to which the interrupt pin is connected.

Pins are numbered from 0 to 15 and mapped as follows
PORTA: 0 to 7 with PORTA_0 = Pin 0
PORTB: 8 to 15 with PORTB_0 = Pin 8

The library merges the both interrupt pins of the MCP23017 for the sake of simplicity.

For a working example that uses most of the functions, see the example below.
For docs, see the docstrings in mcp23017.py. It is planned to add some kind of proper documentation if needed.

# This example registers one callback that toggles a pin
# on each press

from pi_mcp23017.mcp23017 import MCP23017, Banks, Directions, PullUp
from time import sleep

# example address and pin,
# change to your own data
ADDRESS = 0x20
PIN = 27


# define an interrupt callback
def callback(pin_number):
    print(f"got interrupt on pin {pin_number}!")
    state = port_expander.read_pin(8)
    port_expander.write_pin(8, not state)


# if you have multiple busses choose the right one!
port_expander = MCP23017(ADDRESS, int_pin=PIN, bus=0)

# set PORTA to Input and enable interrupts
port_expander.setup_port(Banks.A, Directions.INPUT, PullUp.ENABLED)
port_expander.attach_interrupt(0, callback)

# set PORTB_0 to Output and set it to LOW
port_expander.setup_pin(8, Directions.OUTPUT, PullUp.DISABLED)
port_expander.write_pin(8, False)

try:
    while True:
        sleep(.1)
except KeyboardInterrupt:
    print("Cleaning up automatically and exiting")

Running tests

The testsuite is pretty small at the moment

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.