Sub topic 3
Updating readings without flicker or leftover characters
This chapter assumes you can upload a sketch with the Arduino IDE, write setup() and loop(), wire a display, and install a library with the Library Manager. The OLED sections also assume the Adafruit GFX and SSD1306 libraries are installed.
Updating values without flicker
Calling clear() in every loop makes the screen blink. Overwrite only the changing value instead. On a character LCD, pad the number with spaces so a shorter value erases the old digits.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.print("Reading:");
}
void loop() {
int value = analogRead(A0);
lcd.setCursor(9, 0);
lcd.print(" "); // erase the old digits
lcd.setCursor(9, 0);
lcd.print(value);
delay(200);
}Drawing text, lines, and shapes with Adafruit GFX
Adafruit GFX supplies drawing calls such as drawLine(), drawRect(), fillRect(), and fillCircle(). These calls write into the OLED's frame buffer, a copy of the screen kept in RAM. Nothing appears until you call display.display(), which sends the buffer to the panel. Call clearDisplay() at the start of each frame so old shapes do not remain.
Defining custom characters on an LCD
A character LCD holds eight custom glyph slots, numbered 0 to 7. Each glyph is an 8-byte array in which each byte holds one 5-pixel row. Register a glyph once with createChar() in setup(), then draw it with write(byte(n)).
How much RAM the OLED frame buffer uses
A 128x64 SSD1306 uses 1 bit per pixel, so its frame buffer needs 128 × 64 ÷ 8 = 1024 bytes. An Arduino Uno has 2048 bytes of SRAM, so the buffer takes half of it. Add up your variables and strings before you choose an OLED for a small board.
> Many breakout boards already include pull-up resistors on SDA and SCL. Extra pull-ups can stop the I2C bus from working.
Fixing blank, garbled, or missing displays
- Blank screen: Check power and ground, the backlight, and the contrast potentiometer on the LCD.
- Garbled text: Check the SDA and SCL wiring and the I2C address found by your scanner.
- Missing OLED: If
display.begin()returns false, the address is probably wrong. - Faint text: Turn the contrast potentiometer, or check that the code uses the expected voltage option.
Exercise
Exercise
Your LCD shows "1023" and then the reading drops to "87", leaving "23" on the screen. What causes this, and how do you fix it?
Show answer
The new value has fewer digits, so the old trailing characters remain. Print spaces over the field before printing the new value, as the first sketch does with lcd.print(" ").