Button-LCD circuit flickering

So, I have made a simple circuit using an LCD and a button, so that when I press the button, the LCD should show "don't touch me!", which it does. And when I am not pressing the button, the LCD doesn't show anything, which is what I want. My problem is that when I press the button to show "don't touch me!", it shows that but the text is flickering. I am a beginner to this, only been on a few days. here is the code:

The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 modified 7 Nov 2016
 by Arturo Guadalupi

*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2, button = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
 

void setup() {
  // set up the LCD's number of columns and rows:
  pinMode(button, INPUT);
  


  
}

void loop() {
      if (digitalRead(button) == HIGH) {
  lcd.begin(16,2);
  lcd.print("don't touch me!");
} else {
  lcd.noDisplay();
}
      
}

I am running on an Arduino Uno. Everything works fine, I even use a 10k resistor between the button and the ground. What can I do to stop the flickering?

Do the lcd.begin() just once in setup() and take it out of loop()

This is an easy one!

Your code writes to the LCD again and again and again as long as the button is held. That is what is causing the flickering.

You need to write code which when it tests the button, checks to see what it found the previous time it read the button, and only changes the LCD, whether to write your message or blank it, if the state of the button has actually changed.

Also, lcd.begin(16,2); should be in setup(), not loop().

There is however, a problem with the button and "contact bounce" where as you press or release the button, the actual contact is intermittent for a few milliseconds and the Arduino is fast enough to see this as multiple changes. A crude approach of this is to only check the button every 10 ms by putting a delay in the loop. As you write more sophisticated programs, you will need to use proper "debounce" algorithms.

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