Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 37 ยท Libraries and Projects

LCD and OLED Displays

Show text and graphics on Arduino: the 16x2 character LCD with LiquidCrystal and an I2C backpack, custom characters, SSD1306 OLED basics, and formatting values.

A display turns your project from a black box into a device you can read without a computer. Two types cover most beginner needs: the 16x2 character LCD (two rows of sixteen characters, very cheap, very readable) and the small OLED (128x64 pixels, sharp, draws anything). Both are driven by well-known libraries; your job is mainly to decide what to show and to format it well.

No display hardware is available on this site. The sketches are read-only Arduino code, and the C programs are labelled stand-ins that simulate a 16x2 screen buffer in text.

The 16x2 LCD

The classic HD44780 LCD has 16 pins and uses up to 6 of your digital pins. Most people buy one with an I2C backpack (a small PCF8574 board soldered on the back) so it needs only four wires: GND, VCC, SDA, SCL. The backpack address is usually 0x27 or 0x3F (use the scanner from the I2C lesson if unsure). A small blue potentiometer on the backpack adjusts contrast: if you see only blocks or nothing, turn it.

With the I2C backpack, install the LiquidCrystal I2C library (see installing libraries):

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

LiquidCrystal_I2C lcd(0x27, 16, 2);    // address, columns, rows

void setup() {
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);                 // column 0, row 0
  lcd.print("Temp:");
  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
}

void loop() {
  lcd.setCursor(6, 0);
  lcd.print(23.5, 1);
  lcd.print(" C ");                    // trailing space overwrites leftovers
  delay(1000);
}

Key functions: setCursor(col, row) moves the cursor, print() writes text and numbers (like Serial.print), clear() blanks the display (slow and causes flicker), backlight() and noBacklight() control the lamp, scrollDisplayLeft() shifts the text.

Avoid clear() in loops. Instead overwrite only what changed, and pad values with spaces so a shorter number erases a longer one (9.5 replacing 10.5). The stand-in below simulates a 16x2 screen and shows the difference.

C Stand-in: a 16x2 screen buffer (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Custom characters

An HD44780 lets you define up to 8 custom characters, each a 5x8 pixel pattern stored as 8 bytes (one per row, 5 bits used). This makes a degree sign, a heart or a battery icon.

C++
byte degree[8] = {
  0b00110,
  0b01001,
  0b01001,
  0b00110,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};

void setup() {
  lcd.init();
  lcd.createChar(0, degree);     // slot 0
  lcd.setCursor(0, 0);
  lcd.print("23.5");
  lcd.write(byte(0));            // draw custom character 0
  lcd.print("C");
}
C Stand-in: draw a 5x8 custom character (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The SSD1306 OLED

The 0.96-inch OLED (128x64 pixels, monochrome) is almost always wired over I2C with address 0x3C. Each pixel emits its own light, so contrast is excellent and no backlight is needed. It needs two libraries: Adafruit SSD1306 and Adafruit GFX (the second provides drawing functions: lines, rectangles, circles, text in different sizes).

C++
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 display(128, 64, &Wire, -1);

void setup() {
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    for (;;) { }                       // display not found: stop here
  }
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println("23.5 C");
  display.drawRect(0, 40, 128, 16, SSD1306_WHITE);
  display.fillRect(2, 42, 80, 12, SSD1306_WHITE);   // a bar graph
  display.display();                   // nothing appears until this call
}

void loop() { }

Important differences from the character LCD:

  • Drawing goes to a frame buffer in RAM; display() sends it to the screen. For 128x64 that buffer is 1024 bytes, half the RAM of an Uno.
  • Text size 1 is 6x8 pixels per character: 21 columns by 8 rows.
  • Redrawing the whole frame every pass is fine at a few frames per second; heavy animations need care.
  • OLED pixels can burn in over time: dim or blank the screen when idle.

Formatting values for display

Numbers that jump in width make screens look messy. Print with fixed widths and units. On AVR, sprintf does not support floating-point conversion by default, so convert with dtostrf(value, width, decimals, buffer) or print the integer and fractional parts separately.

C Stand-in: fixed-width value formatting (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Practical tips

  • Update only when the value changes; it reduces flicker and I2C traffic.
  • Use F() for constant text to save RAM, and avoid String for updating lines (see debugging and memory).
  • If the LCD shows two rows of solid blocks, adjust contrast or check the address; if nothing at all appears, check power, the SDA and SCL wiring and the library's init() or begin() result.
  • For tiny dashboards, 7-segment displays (driven by a MAX7219 over SPI, see the SPI lesson) are another option.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many wires (not counting power) does an LCD with an I2C backpack need?

    SDA and SCL. The backpack converts I2C into the parallel signals the HD44780 needs.
  2. 2. Why is calling lcd.clear() in every loop pass a bad idea?

    Clearing blanks the whole screen each time. Overwrite only changed fields and pad with spaces instead.
  3. 3. With the Adafruit SSD1306 library, when does drawing become visible?

    Drawing functions change a frame buffer in RAM. display() transfers it to the screen.
  4. 4. How many bytes define one custom character on an HD44780 LCD?

    A character is 5x8 pixels, stored as 8 bytes, one per row (5 bits used).

Summary

  • A 16x2 LCD with an I2C backpack needs only four wires and the LiquidCrystal I2C library.
  • Use setCursor and padded print calls rather than clear(); define up to 8 custom characters.
  • The SSD1306 OLED draws into a RAM buffer and shows it on display(); it needs the GFX library.
  • Format values with fixed width and units so the screen stays tidy.

Next: Writing Your Own Library