Tagged: pcf8575
- This topic has 4 replies, 2 voices, and was last updated 1 year ago by
Renzo Mischianti.
-
AuthorPosts
-
-
29 April 2024 at 15:06 #30629
Hi!
I am trying to read inputs of PCF8575 with ESP32. I had problems reading it with Arduino IDE but I found a “solution”. If I define ALL pins as outputs, then I set them to 1 and then define them as inputs. I can read correctly the voltage changes on the inputs.
Now I load the same code with platform io and it doesnt work.
I use the same version of your library 1.1.1
Could please help me?This is my code:
#include “PCF8575.h”
PCF8575 pcf(0x20);
bool estado=0,value=0,machine_state=0;
void set_pcf();
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
set_pcf();
machine_state=0;
// pcf.digitalWrite(10, LOW);//En cuanto cambio el estado de la salida digital el resto se quedan a 0 también.
}void loop() {
// put your main code here, to run repeatedly:
switch (machine_state)
{
case (0):
Serial.println(“CASE WAITING”);
value=pcf.digitalRead(P1);
Serial.println(value);
delay(50);
break;
}}
void set_pcf(){
pcf.pinMode(0, OUTPUT);
pcf.pinMode(1, OUTPUT);
pcf.pinMode(2, OUTPUT);
pcf.pinMode(3, OUTPUT);
pcf.pinMode(4, OUTPUT);
pcf.pinMode(5, OUTPUT);
pcf.pinMode(6, OUTPUT);
pcf.pinMode(7, OUTPUT);
pcf.pinMode(8, OUTPUT);
pcf.pinMode(9, OUTPUT);
pcf.pinMode(10, OUTPUT);
pcf.begin();
Serial.println(“RELÉ OFF”);
pcf.digitalWrite(0,HIGH);
pcf.digitalWrite(1,HIGH);
pcf.digitalWrite(2,HIGH);
pcf.digitalWrite(3,HIGH);
pcf.digitalWrite(4,HIGH);
pcf.digitalWrite(5,HIGH);
pcf.digitalWrite(6,HIGH);
pcf.digitalWrite(7,HIGH);
pcf.digitalWrite(8,HIGH);
pcf.digitalWrite(9,HIGH);
pcf.digitalWrite(10, HIGH);
pcf.pinMode(0, INPUT);
pcf.pinMode(1, INPUT);
pcf.pinMode(2, INPUT);
pcf.pinMode(3, INPUT);
pcf.pinMode(4, INPUT);
pcf.pinMode(5, INPUT);
pcf.pinMode(6, INPUT);
pcf.pinMode(7, INPUT);
pcf.pinMode(8, INPUT);
pcf.pinMode(9, INPUT);
pcf.pinMode(10, OUTPUT);
} -
29 April 2024 at 18:08 #30631
Hi Amaia,
I test the library, and it works correctly.
Rewrite the code as usual and retry.
Bye Renzo -
30 April 2024 at 07:44 #30632
Thank you for your prompt response.
Usual code was my first code. But it doesn’t work, all entries seem to be LOW when they should be HIGH.
I have read that I am not the only one who has seen this strange behavior. In fact one user discussed with you and came to the conclusion that the solution was to declare everything as HIGH OUTPUTs and then as INPUTs.-
This reply was modified 1 year ago by
amaiaMDNURRA.
-
This reply was modified 1 year ago by
amaiaMDNURRA.
Attachments:
You must be logged in to view attached files. -
This reply was modified 1 year ago by
-
30 April 2024 at 11:11 #30638
Hi,
I tested now this sketch/* KeyPressed on PIN1 by Mischianti Renzo <http://www.mischianti.org> https://www.mischianti.org/2019/01/02/pcf8575-i2c-digital-i-o-expander-fast-easy-usage/ */ #include "Arduino.h" #include "PCF8575.h" // Set i2c address PCF8575 pcf8575(0x20); unsigned long timeElapsed; void setup() { Serial.begin(115200); // pcf8575.pinMode(P0, OUTPUT); pcf8575.pinMode(P1, INPUT); pcf8575.begin(); timeElapsed = millis(); } void loop() { // if (millis() - timeElapsed > 2000) { // if (pcf8575.digitalRead(P0) == HIGH) { // pcf8575.digitalWrite(P0, LOW); // } else { // pcf8575.digitalWrite(P0, HIGH); // } // timeElapsed = millis(); // } uint8_t val = pcf8575.digitalRead(P1); if (val == HIGH) { Serial.println("KEY PRESSED"); } delay(100); }
And this sketch
/* KeyPressed on PIN1 by Mischianti Renzo <http://www.mischianti.org> https://www.mischianti.org/2019/01/02/pcf8575-i2c-digital-i-o-expander-fast-easy-usage/ */ #include "Arduino.h" #include "PCF8575.h" // Set i2c address PCF8575 pcf8575(0x20); unsigned long timeElapsed; void setup() { Serial.begin(115200); pcf8575.pinMode(P0, OUTPUT); pcf8575.pinMode(P1, INPUT); pcf8575.begin(); timeElapsed = millis(); } void loop() { if (millis() - timeElapsed > 2000) { if (pcf8575.digitalRead(P0) == HIGH) { pcf8575.digitalWrite(P0, LOW); } else { pcf8575.digitalWrite(P0, HIGH); } timeElapsed = millis(); } uint8_t val = pcf8575.digitalRead(P1); if (val == HIGH) { Serial.println("KEY PRESSED"); } delay(100); }
and work properly.
Can you also insert the wiring diagram?
Bye Renzo
Attachments:
You must be logged in to view attached files. -
30 April 2024 at 17:15 #30640
If you want to try, I also pushed a new version that adds a lot of features and the possibility to specify the INPUT_PULLUP mode.
Bye Renzo
-
-
AuthorPosts
- You must be logged in to reply to this topic.