Your Search Bar For Shrewd Tips

How Do I Clear Lcd Display Arduino?


To clear an LCD display connected to an Arduino, call the clear() function on the LCD object. For example, with the Arduino LiquidCrystal library, write lcd.clear();. This removes the characters currently shown and returns the cursor to the first position on the display. The exact setup depends on whether your screen is a standard parallel LCD or an I2C LCD, but the basic clearing command is usually the same.

Clearing an LCD is useful when you need to replace one message with another, remove old values, or refresh a screen after a button press. This guide explains how to clear an Arduino LCD, where to put the command in your sketch, how to avoid common display problems, and what to do if lcd.clear() does not work as expected.

How to Clear an Arduino LCD

For a compatible character LCD, use the clear() method with the name of your LCD object. If your object is named lcd, the command is:

lcd.clear();

Place this line after the LCD has been initialized with lcd.begin() or lcd.init(). You can also use it later in the program whenever you want to erase the display. Here is a simple example for a standard 16x2 LCD connected through the Arduino LiquidCrystal library:

#include <LiquidCrystal.h>

// Change these pin numbers to match your wiring
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  lcd.print("Hello, Arduino");
  delay(2000);

  lcd.clear();
  lcd.print("Screen cleared");
}

void loop() {
}

In this sketch, the LCD first displays “Hello, Arduino.” After a two-second pause, lcd.clear() removes that message, and the next line prints a new one. The lcd.begin(16, 2) command tells the library that the display has 16 columns and 2 rows. Use the correct dimensions for your particular character LCD.

What Does lcd.clear() Do?

The clear() function sends a clear-display instruction to the LCD controller. On a typical character display, it blanks the visible characters and moves the cursor to the home position: column 0, row 0. This means you can print new text immediately after clearing without first repositioning the cursor.

Clearing the LCD does not reset your Arduino, erase variables, or restart the sketch. It only changes what appears on the display. Your program continues running from the next instruction. The command also does not change the LCD’s wiring or configuration, so you generally do not need to call lcd.begin() again after clearing the screen.

Keep in mind that the screen may briefly appear blank before new text is printed. That is normal. However, repeatedly clearing and repainting the display very quickly can make it flicker or look like it is flashing. For many projects, it is better to update only the characters that have changed.

Clearing an I2C LCD Display

Many Arduino LCD modules use an I2C adapter, which reduces the number of wires needed to connect the display. These displays commonly use a library such as LiquidCrystal_I2C. The initialization code differs from the standard parallel LCD example, but the clear command is often still lcd.clear();.

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

// The address and display size depend on your module
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  lcd.init();
  lcd.backlight();

  lcd.print("I2C LCD ready");
  delay(2000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("New message");
}

void loop() {
}

I2C LCD libraries are not all identical. Some use lcd.init(), while others may use a different initialization method. The I2C address can also vary; common addresses include 0x27 and 0x3F, but your module may use another address. Follow the instructions for the library and hardware you have installed. Once the display is initialized, check its examples or documentation if the familiar clear() method is unavailable.

Clear the LCD Before Showing a New Message

A common reason to clear an Arduino LCD is to replace one message with another. For example, a program might show a welcome message and then display a sensor reading. Clearing first makes sure that leftover characters from the previous message do not remain visible.

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature:");
lcd.setCursor(0, 1);
lcd.print(temperature);
lcd.print(" C");

The setCursor(column, row) function chooses where the next text will appear. On a 16x2 LCD, columns range from 0 to 15, and rows are 0 and 1. Set the cursor after clearing when you want to print on a particular line. If you do not call setCursor(), the cursor starts at the home position after clear().

Be careful when printing changing numbers or text of different lengths. Suppose one reading is 100 and the next is 9. If you print the shorter value over the longer one without clearing or overwriting the remaining character, the display may show 900. Clearing the screen prevents that leftover character, though it may cause a brief blank flash.

Use Spaces Instead of Clearing the Whole Screen

You do not always need to clear the entire LCD. If only one line or value changes, you can move the cursor to that area and print spaces over the old content. This can make an update look smoother because the whole screen is not blanked between messages.

lcd.setCursor(0, 1);
lcd.print("                "); // 16 spaces
lcd.setCursor(0, 1);
lcd.print(sensorValue);

The number of spaces should match the width of the area you want to overwrite. For a 16-character row, 16 spaces can blank the whole line. If you only need to replace a value in a known position, print enough spaces to cover the longest possible previous value, then print the new value.

Another option is to print a fixed-width value with leading spaces or padding. This is helpful for counters, timers, and sensor readings that change often. Choose between clearing and overwriting based on the effect you want: lcd.clear() is simple and reliable for a full-screen change, while targeted updates can reduce flicker during frequent refreshes.

Clear the Display Once, Not in Every Loop Iteration

A frequent beginner mistake is putting lcd.clear() at the start of loop() without a delay or condition. The Arduino repeats loop() continuously, so the display may be cleared thousands of times per second. Text may flicker, appear dim, or be difficult to read because it is constantly erased and rewritten.

void loop() {
  lcd.clear();
  lcd.print("Reading sensor");
  delay(500);
}

This example clears the LCD twice per second, which may still produce an unnecessary visible flash. A better approach is to clear only when the displayed screen changes, such as when a button is pressed or the program enters a new mode. For values that update repeatedly, overwrite the changing section instead of clearing the entire screen every time.

You can also use a timer with millis() to refresh content at a controlled interval without stopping other parts of your program. The key idea is to update the LCD only when needed, rather than clearing and redrawing it continuously.

lcd.clear() Versus lcd.home()

The lcd.clear() and lcd.home() commands are related, but they do different things. lcd.clear() blanks the display and moves the cursor to the home position. lcd.home() moves the cursor to the home position without erasing the existing characters.

Use lcd.clear() when you want to remove the current text. Use lcd.home() when you want to start writing from the top-left position while keeping the existing display contents. For instance, moving the cursor home does not make old text disappear; new characters may overwrite only the positions where they are printed.

Common Problems When Clearing an Arduino LCD

  • The command has no visible effect: Confirm that you are calling clear() on the correct LCD object, and that the display was initialized successfully.
  • The LCD is blank all the time: Check the wiring, contrast adjustment, power, display dimensions, and I2C address. A blank screen is not always evidence that the clear command is working correctly.
  • Text flickers: Avoid clearing and rewriting the display continuously. Update only when the content changes, or overwrite the relevant characters with spaces.
  • Old characters remain: The new text may be shorter than the previous text. Clear the display first or print spaces over the unused characters.
  • The sketch does not compile: Check that the correct library is installed and that its examples use the same initialization and method names as your code.
  • The display clears but the new message is misplaced: Set the cursor explicitly with lcd.setCursor(column, row) before printing.

If you are using an OLED screen or a graphical LCD rather than a standard character LCD, the library may use a different clearing function. For example, some graphics libraries clear a display buffer and require a separate command to send the updated buffer to the screen. Identify the exact display and library before adapting a character-LCD example.

Best Practices for Arduino LCD Updates

For a simple menu or a message that changes occasionally, lcd.clear() is usually the easiest solution. Call it when switching screens, then position the cursor and print the new content. For a clock, sensor monitor, or counter that updates often, avoid clearing the whole screen every cycle. Rewrite only the changing area to make the display easier to read.

It also helps to keep your display code organized. Create a function for each screen or message if your project has multiple display states. Each function can set the cursor and print its content consistently. When changing from one complete screen to another, clear once before drawing the new screen, rather than placing a clear command in every small update routine.

Finally, confirm your LCD’s dimensions and library documentation. Most character LCD libraries provide a clear method, but initialization calls, I2C addresses, and some details vary by hardware and library version. Testing a small sketch that prints a message, waits, and then calls lcd.clear() can help isolate display issues before you add the command to a larger project.

Conclusion

To clear a compatible Arduino character LCD, use lcd.clear(); after initializing the display. The command blanks the visible text and returns the cursor to the top-left position, so you can print a new message immediately. It works with many standard and I2C character LCD libraries, although setup details depend on the specific library and module.

Use a full clear when changing screens, and avoid calling it continuously inside a fast-running loop. For frequently changing values, overwrite only the necessary characters to reduce flicker and keep the display readable. With the right initialization, cursor positioning, and update strategy, clearing an Arduino LCD is straightforward.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →