Code Monkey home page Code Monkey logo

micropython-mcp230xx's Introduction

micropython-mcp230xx

Micropython I2C-based manipulation of the MCP series GPIO expanders MCP23017 and MCP23008, derived from the Adafruit_MCP230xx.py module of https://github.com/adafruit/Adafruit_Python_GPIO

This has been tested with ESP8266 running Micropython 1.8.7 on MCP23017 only, but hopefully works also for MCP23008 if you have one. Please post an issue if you have success.

To use, wire up the GPIO expander chip following this loom...

  • MCP23017 Pins
    • 9 => 3.3V supply
    • 10 => GND
    • 12 => ESP8266 GPIO5 (NodeMCU D1) [I2C SCL Signal]
    • 13 => ESP8266 GPIO3 (NodeMCU D2) [I2C SDA Signal]
    • 12 => 10kOhm resistor => 3.3V Supply [I2C SCL Pull-up]
    • 13 => 10kOhm resistor => 3.3V Supply [I2C SDA Pull-up]
    • 18 => 10kOhm resistor => 3.3V Supply [Reset pin in 'run' configuration]

Also choose the address of each MCP23017, e.g.

  • Addressing pins for address 0x20
    • 15 => 10kOhm resistor => GND
    • 16 => 10kOhm resistor => GND
    • 17 => 10kOhm resistor => GND

The mappings between pins and I2C addresses are...

Pin pull table

If you wish to use a different I2C address, or a different GPIO-numbered SDA or SCL pin than the default then pass that in to the constructor.

The default constructor arguments mean that MCP23017() is equivalent to MPC23017(address=0x20, gpioScl=5, gpioSda=4).

A handy visual reference is this from mathworks, although I recommend that for ESP8266 the chip power should be provided by 3.3V not 5V, to make sure logic levels are correct and that the serial data line doesn't overload SDA.

MCP Pinout

For example, the following will set the output values of pins 10-15 and read the logic value (True or False) of pins 0-9

import mcp

io = mcp.MCP23017()

# controls some output pins
outPins = list(range(10,16))
nextVals = {}
for pinNum in outPins:
    io.setup(pinNum, mcp.OUT)
    nextVals[pinNum] = True
io.output_pins(nextVals)

# monitors and prints some input pins
inPins = list(range(0,10))
for pinNum in inPins:
    io.setup(pinNum, mcp.IN)
while True:
    print(io.input_pins(inPins))

micropython-mcp230xx's People

Contributors

cefn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

micropython-mcp230xx's Issues

INT[AB] handling and external I2C object

Hi & thanks for the great work!

I was in need of using the interrupt functionality of the chip and be more compatible with machine.Pin.
If anybody is interested I could publish the code - let me know.

New functionality:

  • Pin class like machine.Pin
  • Using INT[AB] pins (mirrored) like machine.Pin.irq() with callback

Changed constructor:

  • An I2C object is given instead of sda/scl-pins like in the module for ads1x15 to have just one central I2C object
  • New argument for config of mcp230xx features like providing a machine.Pin object where the INT[AB] of the mcp230xx is wired to on the esp8266

OS error when using NodeMCU and mcp23017

When I use the example code on the library:

import mcp, machine

#To test that the mcp is there
i2c = machine.I2C(scl=machine.Pin(13), sda=machine.Pin(12))
address = hex(i2c.scan()[0])
print(address)

io = mcp.MCP23017(gpioScl=13, gpioSda=12)

outPins = list(range(10,16))
nextVals = {}
for pinNum in outPins:
    io.setup(pinNum, mcp.OUT)
    nextVals[pinNum] = True
io.output_pins(nextVals)

# monitors and prints some input pins
inPins = list(range(0,10))
for pinNum in inPins:
    io.setup(pinNum, mcp.IN)
while True:
    print(io.input_pins(inPins))`

I get the following error:

Traceback (most recent call last):
  File "main.py", line 8, in <module>
  File "/lib/mcp.py", line 56, in __init__
  File "/lib/mcp.py", line 156, in write_iodir
  File "/lib/mcp.py", line 67, in writeList
OSError: [Errno 110] ETIMEDOUT

MicroPython v1.9.4-8-ga9a3caad0 on 2018-05-11; ESP module with ESP8266

I have spent hours replicating what the library does but I can't spot the error.

Any idea why?

Pyboard compatibility

Thanks for this great mcp230xx library. This makes the handling of my MCP23017 much smoother. However I had to make some adjustments to make it work on my pyboard v1.1. On the pyboard the I2C class of the pyb package must be used (different to the I2C class of the machine package which is used on other controllers). For details on the differences see https://learn.adafruit.com/micropython-hardware-i2c-devices/i2c-master. They are both incompatible so I made the approach of passing an i2c object as parameter to the MCP class. The modifications I made provide a module that can be used either on the pyboard or on other boards. The drawback is that the functions signature changes. I made a PR with my changes if you would like to include them to make this module more universal.

Kind Regards

TypeError on writing to pin after reading another

Upon reading a pin it becomes impossible to write to another one

Minimal implementation:

import mcp

mcp1 = mcp.MCP23017(address=0x20, gpioScl=22, gpioSda=21)
mcp1.setup(0, mcp.OUT)
mcp1.setup(5, mcp.IN)
mcp1.pullup(5, True)

mcp1.output(0, 0)
print(mcp1.input(5))
mcp1.output(0, 0)

Returns

False
Traceback (most recent call last):
  File "boot.py", line 9, in <module>
  File "mcp.py", line 92, in output
  File "mcp.py", line 105, in output_pins
TypeError: 'bytes' object does not support item assignment

Fix appears to be simple, just changing line 71 to return a bytearray to keep self.gpio in the original type set on the __init__()

def readList(self, register, length):
        """Introduced to match the readList implementation of the Adafruit I2C _device member"""
        return bytearray(self.i2c.readfrom_mem(self.address, register, length))

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.