Sub topic 8
Updating readings without flicker or leftover characters
Assume you can upload Arduino sketches, use setup() and loop(), wire a display, and install libraries through the Arduino IDE. This chapter focuses on display updates after the first successful text output.
A display update has two common problems:
- Flicker: the screen visibly blanks and redraws.
- Leftover characters or pixels: old content remains when the new value is shorter.
The solution depends on the display type. A character LCD stores characters in fixed screen positions. An OLED usually uses a pixel buffer that you redraw and send as one complete frame.
Updating a character LCD
Avoid calling lcd.clear() on every pass through loop(). Clearing the LCD erases the screen before the replacement text is written, so the blank interval can be visible.
Instead, place the cursor at the same position and overwrite the complete field. If the new value might be shorter, print trailing spaces.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long lastUpdate = 0;
const unsigned long updateInterval = 250;
void setup() {
lcd.begin(16, 2);
lcd.print("Temperature:");
}
void loop() {
if (millis() - lastUpdate >= updateInterval) {
lastUpdate = millis();
int reading = analogRead(A0);
lcd.setCursor(0, 1);
lcd.print("Value: ");
lcd.print(reading);
lcd.print(" "); // Erase characters from an older, longer value.
}
}The expression millis() - lastUpdate schedules updates without blocking the sketch with delay(). The subtraction form also works when the millis() counter eventually wraps around.
The field begins at column zero on the second row. Printing five spaces after the number gives the field a fixed width. For a more controlled layout, use sprintf() or snprintf() to create a fixed-width character array, but keep in mind that formatting functions can consume additional RAM.
Redrawing an OLED frame
An SSD1306 OLED commonly uses a full frame buffer in RAM. Drawing functions change that buffer, but the physical display does not change until display.display() sends the buffer.
For each frame:
- Clear the buffer.
- Draw every item that should be visible.
- Send the completed buffer once.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
unsigned long lastUpdate = 0;
const unsigned long updateInterval = 100;
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;) {} // Stop if the OLED is not detected.
}
display.setTextColor(SSD1306_WHITE);
}
void loop() {
if (millis() - lastUpdate >= updateInterval) {
lastUpdate = millis();
int value = analogRead(A0);
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print(F("Analog input"));
display.setTextSize(2);
display.setCursor(0, 16);
display.print(value);
display.drawRect(0, 48, 128, 10, SSD1306_WHITE);
int width = map(value, 0, 1023, 0, 126);
display.fillRect(1, 49, width, 8, SSD1306_WHITE);
display.display();
}
}Clearing the buffer prevents old pixels from remaining when a value changes. Sending the buffer only after all drawing is complete prevents intermediate drawing steps from appearing on screen.
Choosing an update strategy
| Display | Erase old content with | Publish the update with |
|---|---|---|
| Character LCD | Spaces or a carefully chosen field | Individual `print()` calls |
| SSD1306 OLED | `clearDisplay()` before redrawing | One `display.display()` call |
For rapidly changing data, choose an interval that matches the information. A temperature reading may need only one update per second. A moving bar can use 10 to 20 updates per second. Updating too often wastes bus time and can make the rest of the sketch less responsive.
Exercise
An LCD previously showed Value: 1023, but after the reading becomes 7, the screen shows Value: 73. What caused the problem, and how can you fix it?
Show answer
The new text was shorter, so the final old character remained. Print enough trailing spaces after the value, or write the value into a fixed-width field.