logo elektroda
logo elektroda
X
logo elektroda

How to fix the button operation in an ESP32 project with LCD display?

jakub51996 1410 12
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18285401
    jakub51996
    Level 6  
    Hi. I am about to create a display that is supposed to display two different pieces of information using a button. That is, pressing (and releasing) the button changes the text to the other. I have already started the work but I have a problem because the button seems not to work. I don't know if the problem is in the program or the connection. I am inserting the code and how I connected the button( instead of arduino it is esp32)
    #include <LiquidCrystal_I2C.h>
    
    // set the LCD number of columns and rows
    int lcdColumns = 16;
    int lcdRows = 2;
    LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);  
    int buttonpin = 12;
    
    int buttonState = 0;
    
    void setup() 
    {
      lcd.init();
      // turn on LCD backlight                      
      lcd.backlight();
      pinMode(buttonpin, INPUT_PULLUP);
      
    }
    
    void loop() {
      buttonState = digitalRead(buttonpin);
      if (buttonState == LOW) {
       lcd.setCursor(0, 0);
      // print message
      lcd.print("Hello, World!");
      delay(1000);
      // clears the display to print new message
      lcd.clear();
      }
      
      else {
       
      lcd.setCursor(0, 1);
      // print message
      lcd.print("Home, come!");
      delay(1000);
      // clears the display to print new message
      lcd.clear();
      }
      
    }
    .
    Connection: https://www.arduino.cc/en/uploads/Tutorial/button.png
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
  • ADVERTISEMENT
  • #2 18285528
    khoam
    Level 42  
    A bit unfortunate you chose to just use GPIO 12 on the ESP32 to operate the button.
    https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

    jakub51996 wrote:
    I have already started the work but I have a problem because the button seems not to work.
    .
    What specifically does this "not working" consist of? You don't have debouncing support for the button, so the LOW/HIGH state readings may appear in series on and off. What model of ESP32 board is this?
  • #3 18286164
    jakub51996
    Level 6  
    My board is esp32 devkit v1 and the button is wired with gpio15. The button does not cause any change as if it is not working. What is the debouncing for the button?
  • #4 18286393
    khoam
    Level 42  
    jakub51996 wrote:
    Button does not cause any change as if it is not working.
    .
    What state does it remain in, LOW or HIGH? You have defined 'buttonpin' as INPUT_PULLUP. If the resistor at the button is to ground, then you should define 'buttonpin' as INPUT.

    jakub51996 wrote:
    What is the debouncing for the buttonpin?
    .
    https://github.com/evert-arias/EasyButton
    Works with ESP32.
  • ADVERTISEMENT
  • #5 18286854
    jakub51996
    Level 6  
    It remains in the Low state.
    I have a question for the debouncing example, does the program not catch all presses or is this caused by contact vibration?
  • ADVERTISEMENT
  • #6 18286856
    khoam
    Level 42  
    jakub51996 wrote:
    Stays Low.
    .
    Have you changed the configuration of 'buttonpin' as INPUT?
  • #7 18286862
    jakub51996
    Level 6  
    khoam wrote:
    .
    Have you changed the configuration of 'buttonpin' as INPUT?

    Yes
  • #8 18286885
    khoam
    Level 42  
    jakub51996 wrote:
    program does not catch all presses
    .
    The reason may be that you are checking the state of the pin at one-second intervals, regardless of what the previous state was. These are intervals that are too long. Use the library I wrote about in post #4, there you will find examples of how button presses/releases should be tested.
  • #9 18288449
    jakub51996
    Level 6  
    Now I have the problem that the display alternates between the two cases. What could be causing this? (Yesterday only one case was displayed.)

    #include <LiquidCrystal_I2C.h>
    #include <EasyButton.h>
    // set the LCD number of columns and rows
    int lcdColumns = 16;
    int lcdRows = 2;
    LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);  
    int buttonpin = 26;
    // Arduino pin where the button is connected to.
    int buttonState = 0;
    
    void setup() 
    {
      
      lcd.init();
      // turn on LCD backlight                      
      lcd.backlight();
      pinMode(buttonpin, INPUT);
      
    }
    
    void loop() {
      
      buttonState = digitalRead(buttonpin);
      if (buttonState == LOW) {
       lcd.setCursor(0, 0);
      // print message
      lcd.print("Hello, World!");
      delay(1000);
      // clears the display to print new message
      lcd.clear();
      }
      
      else {
       
      lcd.setCursor(0, 0);
      // print message
      lcd.print("Home, come!");
      delay(1000);
      // clears the display to print new message
      lcd.clear();
      }
      
    }
    .
  • ADVERTISEMENT
  • #10 18288467
    khoam
    Level 42  
    jakub51996 wrote:
    What could be causing this? (
    .
    Vibration of the switch contacts. Use the EasyButton library as I have already written to solve this problem.
  • #11 18289399
    jakub51996
    Level 6  
    I have used the EasyButton library but the display still alternates between the situations (even when I don't use the button). The serial port monitor displays that the button is pressed.
    
    
    
    #include <LiquidCrystal_I2C.h>
    #include <EasyButton.h>
    // set the LCD number of columns and rows
    int lcdColumns = 16;
    int lcdRows = 2;
    LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);  
    int buttonpin = 15;
    // Arduino pin where the button is connected to.
    int buttonState = 0;
    EasyButton button(buttonpin);
    void onPressed() {
      Serial.println("Button has been pressed!");
    }
    void setup() 
    {
       Serial.begin(115200);
      // Initialize the button.
      button.begin();
      // Add the callback function to be called when the button is pressed.
      button.onPressed(onPressed);
      lcd.init();
      // turn on LCD backlight                      
      lcd.backlight();
      pinMode(buttonpin, INPUT);
      
    }
    
    void loop() {
      button.read();
      buttonState = digitalRead(buttonpin);
      if (buttonState == HIGH) {
       lcd.setCursor(0, 0);
      // print message
      lcd.print("Hello, World!");
      delay(1000);
      // clears the display to print new message
      lcd.clear();
      }
      
      else {
       button.read();
      lcd.setCursor(0, 0);
      // print message
      lcd.print("Home, come!");
      delay(1000);
      // clears the display to print new message
      lcd.clear();
      }
      
    }
    
    .
  • #12 18290347
    khoam
    Level 42  
    You should use the isPressed() and isReleased() functions in the loop() - testing the state of a pin via digitalRead() makes no sense if you want to make the circuit immune to switch contact vibration.
  • #13 18305884
    jakub51996
    Level 6  
    I am trying to build the layout further and I don't know if I have used the isPressed () and isReleased () functions correctly? The program displays home come and the button does not respond at all. On the serial port it displays that the button is pressed every 4s. I noticed that if I remove lcd.init() from the program, it does not display on the serial port that the button is pressed, is this related?
    #include <LiquidCrystal_I2C.h>
    #include <EasyButton.h>
    // set the LCD number of columns and rows
    int lcdColumns = 16;
    int lcdRows = 2;
    LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);  
    int buttonpin = 26;
    // Arduino pin where the button is connected to.
    int buttonState = 0;
    EasyButton button(buttonpin);
    bool press = button.isPressed ();
    bool release = button.isReleased ();
    void onPressed() {
      Serial.println("Button has been pressed!");
    }
    void setup() 
    {
       Serial.begin(115200);
      // Initialize the button.
      button.begin();
      // Add the callback function to be called when the button is pressed.
      button.onPressed(onPressed);
      lcd.init();
      // turn on LCD backlight                      
      lcd.backlight();
      pinMode(buttonpin, INPUT);
      
    }
    
    void loop() {
      button.read();
     
      if (press == true&& release == false) {
       lcd.setCursor(0, 0);
      // print message
      lcd.print("Hello, World!");
      delay(1000);
      // clears the display to print new message
      lcd.clear();
      }
      
      else {
       button.read();
      lcd.setCursor(0, 0);
      // print message
      lcd.print("Home, come!");
      delay(1000);
      // clears the display to print new message
      lcd.clear();
      }
      
    }

Topic summary

The discussion revolves around troubleshooting button operation in an ESP32 project with an LCD display. The user initially faced issues with the button not functioning correctly, leading to confusion about whether the problem was in the code or the hardware connections. Key points include the importance of debouncing to prevent erroneous readings due to contact bounce, and the necessity of configuring the button pin correctly as INPUT or INPUT_PULLUP based on the wiring. The user later switched to using the EasyButton library to manage button presses more effectively. However, they encountered a new issue where the display alternated between two states unexpectedly, attributed to switch contact vibration. Suggestions included using the isPressed() and isReleased() functions from the EasyButton library to improve button state detection and reduce false triggers. The user also noted that the LCD initialization seemed to interfere with button state detection.
Summary generated by the language model.
ADVERTISEMENT