PCF8575 expander with inputs unstable

dstoiko
Posts: 1
Joined: Thu May 25, 2017 6:21 am

PCF8575 expander with inputs unstable

Postby dstoiko » Tue Jun 27, 2017 3:48 am

Hi all,

Trying to integrate in my program a PCF8575 i2c GPIO expander to read inputs from 3 buttons.

I followed and slightly modified an example I found online: http://www.instructables.com/id/Using-T ... d-Inputs-/

I created a C++ class named Buttons with a pressed method to indicate, depending on the byte value read from the expander, which pin of the expander the signal comes from. As a signal, get an int = 2^n (where n is the pin number as indicated on the GPIO expander).

Here is a snippet from my code (Buttons.cpp):

Code: Select all

#include "Buttons.h"
#include <Arduino.h>
#include <Wire.h>
#include "../defines.h"

void Buttons::init() {
    pcf8575_write(word(B00000000, B00000000));
    DBG("Buttons initialized");
}

bool Buttons::pressed(int button) {
    uint16_t dataReceive;
    if (pcf8575_read(dataReceive))
        return (dataReceive == button);
    else
        return false;
}

void Buttons::pcf8575_write(uint16_t dt) {
    uint8_t error;
    Wire.beginTransmission(PF_ADDR);
    Wire.write(lowByte(dt));
    Wire.write(highByte(dt));
    error = Wire.endTransmission();
    if (error == ku8TWISuccess)
        DBG("success initializing PF8575");
    else {
        DBG("error writing to PF8575, check connections");
    }
}

bool Buttons::pcf8575_read(uint16_t &result) {
    uint8_t hi, lo, error;
    Wire.beginTransmission(PF_ADDR);
    error = Wire.endTransmission();
    if (error == ku8TWISuccess) {
        Wire.requestFrom(PF_ADDR, 2);
        if (Wire.available()) {
            lo = Wire.read();
            hi = Wire.read();
            result = (word(hi, lo));
            return true;
        } else {
            DBG("error: Wire not available");
            return false;
        }
    } else {
        DBG("error reading PF8575, check connections");
        return false;
    }
}
I then call these methods every time I want to read from the buttons. For instance here to trigger some state in a state machine:

Code: Select all

if (buttons.pressed(MINUS) && buttons.pressed(OK)) {
    sm.trigger("ADMIN");
}
I'm having an issue with this: at some point in the program execution (I can't determine why because it varies from one execution to the other), the error message "error reading PF8575, check connections" keeps triggering even though the hardware connection is still there. I have other things connected to the i2c bus so I thought it might be that, but this happens even when I disconnect everything else from the i2c bus.

Any ideas how to fix this? I was looking at interrupts too, unable to make it work yet with this expander.

Thanks!

novalight
Posts: 40
Joined: Tue Apr 19, 2016 1:13 pm

Re: PCF8575 expander with inputs unstable

Postby novalight » Tue Jun 27, 2017 7:43 am

Do you have good connections on the bus (i.e. no loose wires, breadboard etc?). Also do you have external pullups?
I have experienced I2C to be very very sensitive to all sorts of (known an unknown) issues on the esp32. There are a range of topics on the forum and on Github regarding this issue. (see https://esp32.com/viewtopic.php?f=2&t=2181)
I wonder if anybody has any clue what could be done here. Especially to avoid the issue of the bus being just inaccessible after one fault or hold down line (which both can happen any time, even with good board, pullups etc.). But yes I am searching desperately for a solution on this topic. I am facing similar issues with the PCF8574.

Who is online

Users browsing this forum: No registered users and 122 guests