GithubHelp home page GithubHelp logo

mcauser / micropython-pcf8575 Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 3.0 2.81 MB

MicroPython driver for PCF8575 16-Bit I2C I/O Expander with Interrupt

License: MIT License

Python 100.00%
micropython pcf8575 io-expander

micropython-pcf8575's Introduction

MicroPython PCF8575

A MicroPython library for PCF8575 16-Bit I2C I/O Expander with Interrupt.

demo

The PCF8575 consists of a 16-bit quasi-bidirectional port and an I2C-bus interface.

The device includes latched outputs with high current drive capability for directly driving LEDs.

The interrupt has an open-drain output, which means you need a pull-up on your microcontroller to detect when the PCF8575 drives it LOW.

When setting a pin HIGH, it acts as both output AND input. The pin internally uses a weak current-source pull-up to latch output HIGH. When driven LOW, for example, with a push button, the pin will read as LOW.

An interrupt fires on any rising or falling edge of the pins in input mode (HIGH). Interrupt is cleared when the pins are changed or the port is read.

The pins are labelled P00-P07 for port A and P10-P17 for port B. When using the pin() method, there is no pins 8-9. Port B's numbering scheme starts at 10 so the right-most digit for both ports is 0-7.

When accessing the 16-bit port directly, there is no gaps. bit0 is P00 and bit8 is P10.

At power on, all pins are driven HIGH and can be immediately used as inputs.

Operating voltage: 2.5V - 5.5V

Installation

Using mip via mpremote:

$ mpremote mip install github:mcauser/micropython-pcf8575
$ mpremote mip install github:mcauser/micropython-pcf8575/examples

Using mip directly on a WiFi capable board:

>>> import mip
>>> mip.install("github:mcauser/micropython-pcf8575")
>>> mip.install("github:mcauser/micropython-pcf8575/examples")

Manual installation:

Copy src/pcf8575.py to the root directory of your device.

Examples

Basic Usage

import pcf8575
from machine import I2C, Pin

# TinyPICO (ESP32)
i2c = I2C(0)
pcf = pcf8575.PCF8575(i2c, 0x20)

# read pin 2
pcf.pin(2)

# set pin 3 HIGH
pcf.pin(3, 1)

# set pin 4 LOW
pcf.pin(4, 0)

# toggle pin 5
pcf.toggle(5)

# set all pins at once with 16-bit int
pcf.port = 0xFF00

# read all pins at once as 16-bit int
pcf.port
# returns 65280 (0xFF00)

For more detailed examples, see examples.

If you mip installed them above, you can run them like so:

import pcf8575.examples.basic

Methods

Construct with a reference to I2C and set the device address. Valid address range 0x20-0x27. If are you not sure what it is, run an i2c.scan(). See below for address selection.

__init__(i2c, address=0x20)

Scans the I2C bus for the provided address and returns True if a device is present otherwise raises an OSError.

check()

Method for getting or setting a single pin. If no value is provided, the port will be read and value of specified pin returned. If a value is provided, the port will be updated and device written to. The port is written to after each call. If you intend to toggle many pins at once, use the port property instead. See below. Valid pin range 0-7 and 10-17.

pin(pin, value=None)

Method for flipping the value of a single pin. Valid pin range 0-7 and 10-17.

toggle(pin)

Private method for checking the supplied pin number is within valid ranges.

_validate_pin()

Private method for loading _port from the device.

_read()

Private method for sending _port to the device.

_write()

Properties

Getter reads the port from the device and returns a 16 bit integer.

port

Setter writes a 16-bit integer representing the port to the device. If you are setting multiple pins at once, use this instead of the pin() method as this writes the entire 16-bit port to the device once, rather than 16 separate writes.

port = 0xFFFF

Ports

  • P00-P07 - Port A
  • P10-P17 - Port B

Why is there no P08 and P09? Because they skipped them when naming the pins, so that the lest significant digit is 0-7 for both ports. There's still only 16 bits. Port B pins are just labelled +2.

This chip only has two ports (16 pins). If you only need 8 pins, the PCF8574 has a single port (8 pins).

Interrupts

  • INT - Active LOW

Shared by both ports A and B. Triggered by either.

I2C Interface

If you are using a module, most contain 10k pull-ups on the SCL and SDA lines.

If you are using the PCF8575 chip directly, you'll need to add your own.

I2C Address

The chip supports I2C addresses 0x20-0x27 and is customisable using address pins A0, A1, A2

A0 A1 A2 I2C Address
GND GND GND 0x20 (default)
3V3 GND GND 0x21
GND 3V3 GND 0x22
3V3 3V3 GND 0x23
GND GND 3V3 0x24
3V3 GND 3V3 0x25
GND 3V3 3V3 0x26
3V3 3V3 3V3 0x27

Parts

Connections

TinyPICO ESP32

from machine import SoftI2C, Pin
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))

from machine import I2C, Pin
i2c = I2C(0)
PCF8575 Module TinyPICO (ESP32)
SDA 21 (SDA)
SCL 22 (SCL)
VCC 3V3
GND GND
INT (optional) 4

Links

License

Licensed under the MIT License.

Copyright (c) 2019 Mike Causer

micropython-pcf8575's People

Contributors

mcauser avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

micropython-pcf8575's Issues

Difference between PCF8575 and PCF8575C.

The current code is match PCF8575 (use Push-pull output),
https://www.ti.com/lit/ds/symlink/pcf8575.pdf
page 13.

I test a PCF8575C (actually I use NXP PCF8575TS, I guess homologous PCF8575C? I'm not ensure, I can't find it's datasheet), it use Open-Drain output,
https://www.ti.com/lit/ds/symlink/pcf8575c.pdf
page 12.
When use sourcing current method, must wire a pull-up resistor.

Is it have some difference except above require modify the code? if possible, please test.

Not getting interrupts from capacitive button

I have the PCF-8575 connected to an ESP8266 using standard I2C (pins 4 and 5 for SDA and SCL respectively, and pin 15 for the interrupts). The PCF is sharing Vcc and GND from the ESP with a TTP223-based capacitive touch button (specifically a BA6-variant-based board which has 3 pins: Vcc, Gnd, and Data), which I have connected to P0.
I've also tried passing Vcc from the PCF to the TTP button, as well as using 5v and 3v for Vcc (in separate tests).

Everything seems to work, except that the PCF isn't sending interrupts to the ESP8266.

The TTP board has an LED that lights up when the button is touched (so I know that's working), and I can see it sending voltage when it does so (using a multimeter), and I followed the examples in my python code for interrupts in this repo, but I don't see any interrupts coming in when I run my code.

The TTP button works great when directly connected to the ESP8266, but I need to use more buttons than the ESP has pins available for, so I was hoping this expander would be a good option.

I'm not sure what I'm doing wrong here. I was hoping that this would be fairly straight forward, but it's now been days of troubleshooting and I'm out of ideas. 🤷

Specific parts/sheets:
TTP223 Data Sheet
TTP223-based board from Amazon
This PCF-8575-based board, also from Amazon
Lolin D1 Mini v4 (ESP8266)

Wont drive P10 to P17;

Wont drive P10 to P17;
can only read P00 to p07

import pcf8575
from machine import I2C, Pin

i2c = I2C(scl=Pin(13), sda=Pin(12))

pcf = pcf8575.PCF8575(i2c)

print(0,pcf.pin(0))
print(1,pcf.pin(1))
print(2,pcf.pin(2))
print(3,pcf.pin(3))
print(4,pcf.pin(4))
print(5,pcf.pin(5))
print(6,pcf.pin(6))
print(7,pcf.pin(7))
print(10,pcf.pin(10))
print(11,pcf.pin(11))
print(12,pcf.pin(12))
print(13,pcf.pin(13))
print(14,pcf.pin(14))
print(15,pcf.pin(15))
print(16,pcf.pin(16))
print(17,pcf.pin(17))

k1=pcf.port

On a D1 mini Pro;

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.