Programming Project Problem with ESP32 + LCD I2C 16x2 + Pushbuttom

Hi everyone! I am trying to carry out a project in which, using an esp32 wroom 32, I do the following: first, while the pushbutton is not activated, it shows "hello world" on the screen (Display LCD I2C 16x2), and second, if the pushbutton is activated (only by pressing it once, not holding it down) to change the text on the display and show "goodbye world" (and maybe in the future add some other actions like turning on a led than only show a text).

The problem I am having is that if you notice here Prueba 1 - Wokwi ESP32, STM32, Arduino Simulator , in the wokwi simulator the program works correctly, however, when I take it to the real thing what happens is that: it shows the first message "hello world" on the display, it loops , it shows "hello world" again but then in the fourth loop it shows itself, without pressing the button, "goodbye world". It is a programming problem since I checked my button.

My theory is that I have an error when I try to make the pushbutton work as a switch. I would appreciate it if you could help me.

#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const int buttonPin = 18;
bool buttonState = LOW;
bool messageDisplayed = false;

void setup() {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  lcd.clear();
  pinMode(buttonPin, INPUT);
}

void loop() {
  delay(200);

  // Mostrar mensaje inicial - Show the first text
  lcd.setCursor(1, 0);
  lcd.print("hello world");
  delay(900);
  lcd.clear();

  // Leer el estado del botón - Pushbuttom state read
  int currentButtonState = digitalRead(buttonPin);

  // Verificar si el botón fue presionado - Verify if the pushbuttom was activated
  if (currentButtonState == HIGH && !messageDisplayed) {
    delay(10);  // Desdebujado temporal para evitar el rebote del botón - Temporary undrawing to prevent button bounce
    lcd.setCursor(2, 0);
    lcd.print("goodaby world");
    messageDisplayed = true;  // Marcar que el mensaje ya fue mostrado - Mark that the message was showed
    delay(1000);
    lcd.clear();
  }

  // Reiniciar el estado del botón si se ha liberado - If it´s been liberated, reboot the pushbuttom state 
  if (currentButtonState == LOW) {
    messageDisplayed = false;
  }
}

Do everyone a favour and kindly read How to get the best out of this forum so you can post effectively. The more information we have the more we can help.
Here is the link How to get the best out of this forum

1 Like

Sorry for that, fixed!

 if (currentButtonState == HIGH && 

when you close the button to HIGH you will need a pull down resistor (aprox 10K to GND) also to see a clear LOW when your button isn't pressed.

see also:
https://docs.arduino.cc/built-in-examples/digital/Button/

and if you don't have a pull down resistor available use an active LOW button with INPUT_PULLUP
https://docs.arduino.cc/built-in-examples/digital/InputPullupSerial/

1 Like

Implementation of the following remark of post #4 has solved your problem.

@maximilianordz8
Very simple fix, no resistors needed.
Change
pinMode(buttonPin, INPUT);
to
pinMode(buttonPin, INPUT_PULLDOWN);

That above code works.

Do you have ESP-IDF version of the above?

It would be in the source code for the pinMode() function. That's the beauty of open source paradigm, you can look for yourself.

1 Like

As I am ignorant of exploring Espressif's database for the source codes, I have asked AI who has provided me the follwoing two lines which seem to work.

gpio_set_direction(GPIO_NUM_18, GPIO_MODE_INPUT);
gpio_pulldown_en(GPIO_NUM_18);

Can we please restrict the posts to solving @maximilianordz8 problems and not your problems. Hi-jacking a post is very unconsiderate, please stop.

Do you think that OP's problem is unsolved? See post #4, #5.

As OP's problem has been solved, some side talks could be allowed -- what do you think?

I think you need to read the forum rules about Hi-jacking, please stop.

Can you give me guarantee that you will be cool if press the Stop Button?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.