Git Product home page Git Product logo

xreef / pcf8574_micropython_library Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 11 KB

MicroPython library for pcf8574 an i2c digital expander for Arduino, esp32, SMT32 and ESP8266. Can read write digital values with only 2 wire.

Home Page: https://www.mischianti.org/category/my-libraries/pcf8574/

License: Other

Python 100.00%
arduino digital esp32 esp8266 expander i2c library micropython pcf8574 pcf8574a

pcf8574_micropython_library's Introduction

Support forum pcf8574 English
Forum supporto pcf8574 italiano

PCF8574 PCF8574AP digital input and output expander with i2c bus.

Changelog

  • 18/04/2023: v0.0.2 Add static declaration for Px constants inside class.
  • 14/04/2023: v0.0.1 Initial commit of stable version.

I try to simplify the use of this IC, with a minimal set of operations.

Tested with esp8266, esp32, Arduino, Arduino SAMD (Nano 33 IoT, MKR etc.), STM32 and rp2040 (Raspberry Pi Pico and similar)

PCF8574P address map 0x20-0x27 PCF8574AP address map 0x38-0x3f

Installation

To install the library execute the following command:

pip install pcf8574-library

Constructor: Pass the address of I2C

    from PCF8574 import PCF8574
    
    pcf = PCF8574(0x38, sda=21, scl=22)

To use interrupt you must pass the interrupt pin and the function to call when interrupt raised from PCF8574

    from PCF8574 import PCF8574
    
    def keyPressedOnPCF8574(pin):
        # Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)
        keyPressed = True
    
    pcf = PCF8574(0x38, sda=21, scl=22, interrupt_callback=keyPressedOnPCF8574, interrupt_pin=18)

You must set input/output mode:

    from machine import Pin
    from PCF8574 import PCF8574

    pcf.Pin(PCF8574.P0, Pin.IN)
    pcf.Pin(PCF8574.P1, Pin.IN, Pin.PULL_UP)
    pcf.Pin(PCF8574.P2, Pin.IN)
    pcf.Pin(PCF8574.P3, Pin.IN)
    
    pcf.Pin(PCF8574.P7, Pin.OUT)
    pcf.Pin(PCF8574.P6, Pin.OUT, 1)
    pcf.Pin(PCF8574.P5, Pin.OUT, 0)
    pcf.Pin(PCF8574.P4, Pin.OUT, 0)

then IC as you can see in the image has 8 digital input/output ports:

PCF8574 schema

To read all analog input in one trasmission you can do (even if I use a 10millis debounce time to prevent too much read from i2c):

    digital_input = pcf.digital_read_all()
    
    print(digital_input.p0)
    print(digital_input.p1)
    print(digital_input.p2)
    print(digital_input.p3)
    print(digital_input.p4)
    print(digital_input.p5)
    print(digital_input.p6)
    print(digital_input.p7)
    
    array_input = pcf.digital_read_all_array()
    print(array_input)
    
    byte_input = pcf.digital_read_all_byte()
    print(bin(byte_input))

If you want to read a single input:

    digital_input = pcf.digital_read(PCF8574.P1)
    print(digital_input)

If you want to write a digital value:

    pcf.digital_write(PCF8574.P1, 1)

You can also use an interrupt pin: You must initialize the pin and the function to call when interrupt raised from PCF8574

    def callback(pin):
        now = utime.ticks_ms()
        global count
        count += 1
        print("Time: {} {}".format(now, count))
    
    
    pcf.attach_interrupt(18, callback)

For the examples I use this wire schema on breadboard: Breadboard Breadboard Breadboard

pcf8574_micropython_library's People

Contributors

xreef avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pcf8574_micropython_library's Issues

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.