Strange PCF8574 behavior

Hello,

ran into a strange problem today. I cannot get a PCF8574A to work properly.

This is the test code:

#include <PCF8574.h>

const uint8_t   buttonCtrl = 0x39;
PCF8574         ctrl(buttonCtrl);

void setup() 
{
  Serial.begin(9600);
  delay(5000);

  Serial.println(F("Calling begin()"));
  ctrl.begin();
  delay(2000);

  Serial.println(F("Configuring pins"));
  ctrl.pinMode(0, INPUT);
  ctrl.pinMode(1, OUTPUT);
  ctrl.pinMode(2, INPUT);
  ctrl.pinMode(3, OUTPUT);
  ctrl.pinMode(4, INPUT);
  ctrl.pinMode(5, OUTPUT);
  ctrl.pinMode(6, INPUT);
  ctrl.pinMode(7, OUTPUT);
  delay(2000);

  Serial.println(F("Turning on RED button LED"));
  ctrl.digitalWrite(1, LOW);
  delay(1000);
  ctrl.digitalWrite(1, HIGH);

  Serial.println(F("Test completed"));
}

void loop() 
{
}

Attached to the PCF8574A is four buttons with build-in LEDs. Each push button is attached to GND and there is a pull-up resistor on the connection to the input pin on the PCF8574A.
The LED anode is attached to VCC through a 560 Ohm resistor and the cathode is attached to the output pin on the PCF.

I have attached a multimeter to port 0 (which is reading the state of the button). When turning on the power it shows ~5V.

When it comes to the line where I turn on the red button LED, the input pin 1 goes LOW and remain so until power off → on again.
Also, all the other three buttons LED are turned on. They remain on when turning the red LED off.

Have tried several MCPs.

What the F*** is going on!

thehardwareman:
Attached to the PCF8574A is four buttons with build-in LEDs. Each push button is attached to GND and there is a pull-up resistor on the connection to the input pin on the PCF8574A.
The LED anode is attached to VCC through a 560 Ohm resistor and the cathode is attached to the output pin on the PCF

As often stated here schematics are the lingua franca of electronics. Hand-drawn and photographed is OK. Use lots of light on the image so it doesn't appear dim.

Also, all the other three buttons LED are turned on.

But you only mentioned one LED in your initial description. You could have wired these up wrong or it could be your code. So please post your code as well following the guidelines in:- How to get the best out of this forum

One led on each output. This is how it is connected:

(in case the image does not show, this is a link to the image

You show 10k resistors in series with the pushbuttons. That is a mistake. I don't see what the purpose of the 1 µF capacitors is.

Paul__B:
You show 10k resistors in series with the pushbuttons. That is a mistake. I don't see what the purpose of the 1 µF capacitors is.

I have tried this design before but not in combination with the MCP (instead of the MCP the input is connected directly to a digital IO pin on a nano). It worked fine.

This two R's and the C is used to avoid too much debouncing, see 2. R-C Debouncing

However, this does not explain the behavior of the MCP. Why is all 4 LEDs turned on when P1 on the MCP goes low? and why does the other three remain on when P1 goes high again? Also the voltage on P0 is ~5V after power on but stay at 0 when P1 i set to low. P0 remains low as long as the MCP has power.

Well, I don't know the library in question, so I do not know what may be happening.

I would suggest you write a basic repetitive "flash" program - in loop() - to test the LED connections. Since we cannot see your project, we cannot see what possible mistakes you have made, we have been recently reminded of what absurd things people actually do with electronics on their way here. :astonished:

I remain puzzled as to why you would ever imagine you need to be concerned about switch "bounce" at this point, or why you would feel the need to provide hardware debouncing when you are in possession of a microcontroller! :roll_eyes:

Paul__B:
Well, I don’t know the library in question, so I do not know what may be happening.

From the library.property file for the PCF8574:

name=PCF8574 library
version=0.9.0
author=Reef
maintainer=Renzo Mischianti <renzo.mischianti@gmail.com>
sentence=Arduino/ESP8266 library for PCF8574
paragraph=Use i2c digital expander with Arduino and ESP8266. Can read write digital values with only 2 wire (perfect for ESP-01).
category=Sensors
url=https://github.com/xreef/PCF8574_library
architectures=*

Paul__B:
I would suggest you write a basic repetitive “flash” program - in loop() - to test the LED connections. Since we cannot see your project, we cannot see what possible mistakes you have made, we have been recently reminded of what absurd things people actually do with electronics on their way here. :astonished:

I will try that.

This and some other components (a nano, another PCF, a 1602 LCD etc) are mounted on a prototype card. I have checked it several times that I have not made any mistake, but so far I have not found any errors.

The next test will be to use a breadboard and test this part of the project together with a nano or uno and see what happens.

Absurd things? Yes, I guess most of us has done such things at least one time in our lifetime :slight_smile:

Paul__B:
I remain puzzled as to why you would ever imagine you need to be concerned about switch “bounce” at this point, or why you would feel the need to provide hardware debouncing when you are in possession of a microcontroller! :roll_eyes:

Mostly because the controller will do a lot of other things and the push buttons will be scanned from time to time or whenever needed.

Thanks for the reply.

Op's image.

Circuit could be simplified. Remove the 100K, 10K, 1uF, wire the buttons between pins and ground. Debounce circuits not needed with this approach I would think. Simply limit reading from the pcf to 20ms intervals, if the interrupt indicates that there has been an input change.

Try setting the led pins to HIGH after you set them to OUTPUT.

PaulRB:
Circuit could be simplified. Remove the 100K, 10K, 1uF, wire the buttons between pins and ground. Debounce circuits not needed with this approach I would think.

Well, that’s what I have been pointing out. You have a microcontroller! Once you have a microcontroller, it makes no sense to waste board space and components with hardware debouncing, the microcontroller is vastly superior for the purpose.

PaulRB:
Simply limit reading from the pcf to 20ms intervals, if the interrupt indicates that there has been an input change.

Well, that’s an interesting point, the OP has not used the interrupt pin at this point and may or may not choose to when he gets his code unscrambled, but in terms of debouncing, you can either perform an I2C read of the PCF or simply read the interrupt line in order to poll the inputs and the latter is clearly easier and performs the “state change” detection for you.

To my mind, it is a bit of a toss-up between the overhead of waiting for 20 ms in order to perform a poll and then determining whether there is a stable state change, or polling every loop() cycle to detect a state change and then while such a change persists, determining whether 20 ms has passed. I like the latter as it will always offer the most rapid response.

Mind you, the most rapid response, and the version that is actually possible to implement in an interrupt routine, is to respond to the very first state change, and then lock out any further response until the specified time - 20 ms - has passed since the last reversion to the initial state.

PaulRB:
Try setting the led pins to HIGH after you set them to OUTPUT.

That’s where this library gets funny! We - or I - don’t know exactly what “ctrl.pinMode” actually does, because in order to separate the bits for individual control, the library must maintain an internal image of the PCF8574 register and the actual default for that is all bits HIGH.

In any case, for the bit that the original posted code controls, performing “ctrl.digitalWrite()” alternately HIGH and LOW clearly should toggle the LED.