PCF8574 & NODEMCU (yet another)

Hello,

I'm using a PCF8574 (red one) along with NodeMCU (lolin) on i2c D1 & D2. Buttons are connected to ports P0 to P7 with 2.2K pull-ups.

With the following code, and when a button is pressed, I always get (0 when the input port is set to input and 1 when set to input_pullup) on all ports.

Makes me wonder why, as the same configuration works fine on Arduino Uno. (i2c on A4 & A5)

I need to ignore the Uno for its memory limitations.

I've tried different libraries with the same sad result.

What am I missing please help?

#include "Arduino.h"
#include "PCF8574.h"
 
#define INTERRUPTED_PIN D7

void keyPressedOnPCF8574();
 
void ICACHE_RAM_ATTR keyPressedOnPCF8574();
PCF8574 pcf8574(0x20, INTERRUPTED_PIN, keyPressedOnPCF8574);
unsigned long timeElapsed;
void setup()
{
    Serial.begin(9600);
    pcf8574.pinMode(P0, INPUT_PULLUP);
    pcf8574.pinMode(P1, INPUT_PULLUP);
    pcf8574.pinMode(P2, INPUT_PULLUP);
    pcf8574.pinMode(P3, INPUT_PULLUP);
    pcf8574.pinMode(P4, INPUT_PULLUP);
    pcf8574.pinMode(P5, INPUT_PULLUP);
    pcf8574.pinMode(P6, INPUT_PULLUP);
    pcf8574.pinMode(P7, INPUT_PULLUP);
 
    pcf8574.begin();
    delay(500);
    timeElapsed = millis();
}
unsigned long lastSendTime = 0;        // last send time
unsigned long interval = 4000;          // interval between sends
bool startVal = HIGH;
bool keyPressed = false;

void loop()
{
    if (keyPressed){
        uint8_t val0 = pcf8574.digitalRead(P0);
        uint8_t val1 = pcf8574.digitalRead(P1);
        uint8_t val2 = pcf8574.digitalRead(P2);
        uint8_t val3 = pcf8574.digitalRead(P3);
        uint8_t val4 = pcf8574.digitalRead(P4);
        uint8_t val5 = pcf8574.digitalRead(P5);
        uint8_t val6 = pcf8574.digitalRead(P6);
        uint8_t val7 = pcf8574.digitalRead(P7);
        Serial.print("P0 ");
        Serial.print(val0);
        Serial.print(" P1 ");
        Serial.print(val1);
        Serial.print(" P2 ");
        Serial.print(val2);
        Serial.print(" P3 ");
        Serial.print(val3);
        Serial.print("P4 ");
        Serial.print(val4);
        Serial.print(" P5 ");
        Serial.print(val5);
        Serial.print(" P6 ");
        Serial.print(val6);
        Serial.print(" P7 ");
        Serial.println(val7);
        keyPressed= false;
    }
 }
 
void keyPressedOnPCF8574(){
     keyPressed = true;
}

I can't seem to find any such thing as "pcf8574.pinMode". Where is it documented?

I suspect it does not respond to "INPUT_PULLUP" as there is no such mode in the PCF8574. Or perhaps there is, but no such mode as "INPUT". :roll_eyes:

Hello Paul,

its documented under PCF8574 i2c digital I/O expander: Arduino, esp8266 and esp32, basic I/O and interrupt – Part 1 – Renzo Mischianti

All very interesting, but there is some degree of flim-flam going on there as with the PCF8574, there is specifically no such thing as a distinction between INPUT and INPUT_PULLUP - or indeed between INPUT and OUTPUT for that matter.

So this library is doing strange things to start with and the fact that it performs differently on different MCUs is perhaps not surprising. I would use the basic PCF8574 (Rob Tillaart) library from the IDE (may be necessary to remove all others) and use only the read and write functions.

In fact, as you are only using these as inputs, use only the "read" functions; writing the port HIGH (0xFF) effectively sets it as INPUT_PULLUP which is in any case the power-on default.

Try:
volatile bool keyPressed=false;

since the variable appears to be set in an ISR callback and used in the loop(). How a missing volatile qualifier is handled depends on how aggressively the compiler optimises the code.

If it works for Uno, it should work for NodeMCU with little intervention. I've spent a whole week into this and concluded it's a bad choice for me. Actually, I haven't seen an article showing that it really works as input. Only theoretical.

So I'm moving on with my project but without this frustrating 8574.

On the other hand, thank you all for your input.

Unless the program contains an error (as your appears to) the consequences of which depend on how the compiler optimises the code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.