PCF8574: how to read Toggle Switch state

Hello!

I would like to read Toggle Switch button state (ON or OFF).

I have got this kind of Toggle Switch (see attached picture).

I have got this schematic (see attached picture Schematic 1) and I have got this code:

(I am using PCF8574.h library)

#include "Arduino.h"
#include "PCF8574.h"  // https://github.com/xreef/PCF8574_library

#define interruptPIN 2
void keyPressedON();
bool keyPressed = false;

PCF8574 pcf8574(0x20, interruptPIN, keyPressedON);

void setup() {
  pcf8574.pinMode(4, INPUT);
  pcf8574.begin();
}

void loop() {
  if (keyPressed) {
    delay(10);
    PCF8574::DigitalInput val = pcf8574.digitalReadAll();
      if (val.p4==HIGH) {
	//do something here
      }
      if (val.p4==LOW) {
	//do something here
      }
  keyPressed= false;
  }
}

void keyPressedON(){
  keyPressed = true;
}

And it works fine, but when I connect three Toggle Switches all to ON state, PCF8574 micro processor starts to heat up, not hot, but slowly starts to heat up (can feel heat by finger). I am afraid if I connect more Switches to it, it will get hotter even faster.

When I add 15k resistor in series of Toggle Switch (see attaches Schematic 2) it does not work, micro processor does not reads value (does not reacts), +5V does not reach PCF8574 PINs.

Is this schematic right? If not, what would be the right schematic to read values (ON or OFF) from Toggle Switch with PCF8574 and pass it to Arduino?

Thank you!
Valters

Toggle Switch.png

Schematic 1.PNG

Schematic 2.PNG

Connecting 5V directly to ground when you close your switch would be a short circuit. Have you wired things the right way?
Your images

Toggle Switch.png
Schematic 1.PNG
Schematic 2.PNG

Well, if I am right, then there is no short circuit - in this schematic P4 gets either +5V (HIGH) or GND (LOW).

In the schematic yes, but is it how you wired it?

To read a switch/button, you do not need a SPDT switch.
You do not need to switch to from gnd to power. All that is needed is a switch or button that connects the pin to ground.

The PCF8574 uses pullups for high level outputs (which is also use for input mode)
So when reading a pin you set the output to "HIGH" which really just enables the pullup inside the part.
This means that if you connect the pin to ground you will read low. If you leave the pin unconnected you read high.

You can use a SPST wired to ground. It is less wiring and is also cheaper.

Also, by not ever directly connecting the pin to power you can avoid a potential short, if there is a s/w issue.
i.e. the software sets the pin output to LOW and the switch is flipped to connect the pin the power.
This is a short that will damage the part.

The other way around is not an issue.
I.e. the pin output is set to HIGH and the switch is set to ground. This may sound like it is a short but it isn't since the PCF8574 does not drive pins high. For high level outputs, it connects the pin to an internal pullup.

This behavior is what they call "quasi-bidirectional I/O" in the datasheet.

--- bill

Thank you all for help. Problem solved.

Valters