logo elektroda
logo elektroda
X
logo elektroda

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

jakub51996 1803 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
  • ADVERTISEMENT
  • #2 18285528
    Anonymous
    Level 1  
  • #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?
  • ADVERTISEMENT
  • #4 18286393
    Anonymous
    Level 1  
  • 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
    Anonymous
    Level 1  
  • #7 18286862
    jakub51996
    Level 6  
    khoam wrote:
    .
    Have you changed the configuration of 'buttonpin' as INPUT?

    Yes
  • #8 18286885
    Anonymous
    Level 1  
  • #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();
      }
      
    }
    .
  • #10 18288467
    Anonymous
    Level 1  
  • #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
    Anonymous
    Level 1  
  • #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