Fix flickering LCD


My LCD is controlled by an I2C display. However, the end of the text is always flickering. I as well connected a 4 x 4 keypad, though the input is also not appearing (well it is very dim), it is supposed to show on the second row. How do I fix this? I already tried to adjust the potentiometer that is built to the I2C, still dim and flickering

#include <LiquidCrystal_I2C.h>
#include <Key.h>
#include <Keypad.h>

const byte rows = 4; 
const byte cols = 4; 
char keys[rows][cols] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[rows] = {9, 8, 7, 6}; // insert the pins
byte colPins[cols] = {5, 4, 3, 2}; 

Keypad mykeypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols ); // initializing our keypad --> mapping it out

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  lcd.begin();

  lcd.home();
  Serial.begin(9600);
}

void loop() {
  char pressed = mykeypad.getKey();
  
lcd.clear();
  lcd.print("You pressed:");

  if (pressed){

    lcd.setCursor(0,1);
    lcd.print(pressed);
   
  }

}

A possible cause would be that the code you did not share (hint) keep erasing and updating the screen?

@qwerter0, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

1 Like

I apologize, this is the code I uploaded.

#include <LiquidCrystal_I2C.h>
#include <Key.h>
#include <Keypad.h>

const byte rows = 4; 
const byte cols = 4; 
char keys[rows][cols] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[rows] = {9, 8, 7, 6}; // insert the pins
byte colPins[cols] = {5, 4, 3, 2}; 

Keypad mykeypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols ); // initializing our keypad --> mapping it out

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  lcd.begin();

  lcd.home();
  Serial.begin(9600);
}

void loop() {
  char pressed = mykeypad.getKey();
  
lcd.clear();
  lcd.print("You pressed:");

  if (pressed){

    lcd.setCursor(0,1);
    lcd.print(pressed);

That's not complete code.

It's advisable to only update the LCD if there is a change.

Thank you for using code tags.

Please post the entire program.

the error is the program line; LCD.clear (); which is in Loop () {.
Because the display is completely erased with every loop run (which takes time) and the entire text is rewritten, it flickers. You may only delete and rewrite the part of the display that has really changed, not the complete one every time rewrite content.

1 Like

My guess in #1 was right :wink:

In addition to what @sterretje, @Deltaflyer and @J-M-L said, do not use the clear function in loop(). It is slow and will contribute to flickering. Use spaces to overwrite old data before sending new data. Do not update the display every time though loop(). Only when data changes or no more than 2 or 3 times a second. Only change the data that needs to change.

The following code illustrates the above suggestions. The hd44780 library is superior to the other LCD libraries and is available through the library manager.

// hd44780 library see https://github.com/duinoWitchery/hd44780
// thehd44780 library is available through the IDE library manager
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

void setup()
{
   lcd.begin(LCD_COLS, LCD_ROWS);
   lcd.clear();
   lcd.print("Hello World");
   lcd.setCursor(0, 1);
   lcd.print("Millis ");
}

void loop()
{
   updateLCD();
}

void updateLCD()
{
   static unsigned long lcdTimer = 0;
   unsigned long lcdInterval = 500;  // update 2 times per second
   if (millis() - lcdTimer >= lcdInterval)
   {
      lcdTimer = millis();
      lcd.setCursor(8, 1);
      lcd.print("       "); // overwrite old data
      lcd.setCursor(8, 1);  // reset the cursor
      lcd.print(millis());
   }
}

yes, sorry, I had started with my answer (english is not my language) and was stopped in another chat by my wife, who is currently 800km away from me, so my message came very late.

Thank you.

you were to the point highlighting that in the code. All good.