Site icon Renzo Mischianti

Switches with pull-up resistors to PCF8575 Inputs

Good day Renzo, I just wanted to let you know about a possible bug in your PCF8575 library. I have a pull-up resistor connected to one of the input pins of my PCF8575 but reading the pin with your library's example shows a LOW input, which is not right. A search on this operation yielded this result from a user with a similar experience: https://forum.arduino.cc/t/wiring-switches-to-pcf8575-inputs/523465/17 This long forum post boils down to the fact that page 12 of the PCF8575 datasheet states: "Before reading from the PCF8575, all ports desired as input should be set to logic 1." Changing your KeyPressedPin1 example as follows seems to correct this issue:

#include "Arduino.h"
#include "PCF8575.h"

// Set i2c address
PCF8575 pcf8575(0x20);
unsigned long timeElapsed;
void setup() {
	Serial.begin(115200);

	pcf8575.pinMode(P1, OUTPUT);
	pcf8575.begin();
	pcf8575.digitalWrite(P1, HIGH);
	pcf8575.pinMode(P1, INPUT);

}

void loop() {
	uint8_t val = pcf8575.digitalRead(P1);
	if (val==LOW) Serial.println("KEY PRESSED");
	delay(50);
}
Exit mobile version