PCF8574 help with read() function

i'm trying to read a single pin from the pcf8574 but it always returns 0. i am using this library

It says read() pin number 0-7. am i doing something wrong?

#include "PCF8574.h"

PCF8574 PCF(0x20);

void setup(){

void loop() {
  PCF.write(0, 1);

the write operations are working correctly. it seems its only when i try to read. i must be misunderstanding something.

What is connected to pin 0? A schema would help.

Just an LED, the PCF8574 sets output state to HIGH and the LED becomes illuminated.

It is connected LED cathode is main ground and LED anode is connected to PCF8574

I have 8 LED's connected total. 1 for each pin on PCF8574

Like this?

Yes except without the resistor and i have all 8 pins connected to LED's

No resistor? Then you may have damaged the chip. Beside, this make no sense. I will probably always be low.

Reading port 4 in the example below make sense. You can detect if the push button is pressed or not.

1 Like

Where are u making this schematic at? sorry, you have led connected to 5v rail. my leds anodes are connected to PCF8574 the cathodes are all connected to ground.

I'm using Kicad. Free software (https://www.kicad.org/)

The PCF8574 is not made for supplying current to a LEDs anode. It should always be connected to the cathode and you must write LOW to that port to get light in the LED.

You should read the datasheet for this chip for more details.

1 Like

This won't work. The pin of a pcf chip is not quite the same as an Arduino pin. It does not have an equivalent to OUTPUT+HIGH. Only OUTPUT+LOW and INPUT_PULLUP.

When set to 1, there will be very little current and the led will only light dimly, if at all. So you have probably not damaged the chip connecting them like that. If you want your LEDs brighter, connect the cathode to the pin and anode to 5V but now you must use a resistor.

Right now, the pin is reading 0 because your led is pulling the pin to ground.

I don't understand that part fully. OUTPUT+HIGH / OUTPUT+LOW

The led are just to visualize the state of the outputs.

If i unplug the leds from the PCF8574 i still dont see any change from PCF.read(). the output of the chip seems to be working. i can make all the leds chase themselfs.

Writing 0 to a pcf pin is equivalent to pinMode(pin, OUTPUT); digitalWrite(pin, LOW);.

Writing 1 to a pcf pin is equivalent to pinMode(pin, INPUT_PULLUP);.

There is no equivalent to pinMode(pin, INPUT); or pinMode(pin, OUTPUT); digitalWrite(pin, HIGH);.

okay so if i write the pin low it will only behave as an output. so to read the pin i would have to first write it high?

1 Like

If you write a 1 to an unconnected pin, you should be able to read a 1 back. Perhaps you have damaged the chip after all.

Well i disconnected the LED's then wrote pin 0 HIGH and readback pin 0 and it still shows 0. the i tried with a 10k pullup resistor on pin 0 and still shows 0. I figured since the output is still working then most likely the pin was fully functional. I have quite a few of these chips so i was lazy about connecting the resistors. i guess i will have to wire up another one. ill probably use switching transistors to handle the current because im going to be switching relays

You will only need the transistors to drive bare relay coils directly. If so, don't forget the flyback diodes. But if using relay modules like those sold for use with Arduino, these have transistors and diodes built-in, you won't need to add any.

1 Like