As soon as your project starts measuring things, you'll want to show the readings on a display instead of the Serial Monitor. There are 3 types you'll meet often, and each has its place.
The 16×2 LCD
The classic green or blue display: two lines, 16 characters each. There's also a 20×4 (four lines, 20 characters) that works exactly the same way.
- Without a module: it needs about 6 microcontroller pins (4-bit mode), and a potentiometer for contrast.
- With an I2C module (a PCF8574 on its back): just 4 wires: VCC, GND, SDA and SCL. This is what we recommend.
- The address: usually 0x27 or 0x3F. If you're not sure, run an I2C scanner (below).
- The display is on but blank, or shows only blocks? Turn the blue potentiometer on the module (the contrast). This is by far the most common problem.
- The drawbacks: letters and numbers only (plus simple symbols), and most run on 5 V.
The code
With the LiquidCrystal_I2C library:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0); // column 0, row 0
lcd.print("Temp:");
}
void loop() {
float t = 24.6; // from the sensor
lcd.setCursor(6, 0);
lcd.print(t, 1);
lcd.print(" C "); // spaces erase the old characters
delay(500);
}
This display shows English characters only. For Arabic, you need a graphic display (OLED or TFT) and a library that supports it.
Custom characters
The display allows 8 shapes of your own design, each 5×8 dots. For example, the degree sign:
byte deg[8] = {0b01100,0b10010,0b10010,0b01100,0,0,0,0};
lcd.createChar(0, deg); // in setup
lcd.write(byte(0)); // display it
With the same idea you can make a battery icon or a progress bar.
I2C scanner: finding the address
If the display doesn't respond, the first step is to find out whether it's on the bus at all:
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(115200);
for (byte a = 1; a < 127; a++) {
Wire.beginTransmission(a);
if (Wire.endTransmission() == 0) {
Serial.print("found 0x");
Serial.println(a, HEX);
}
}
}
void loop() {}
If no address shows up: the wiring is wrong, or SDA and SCL are swapped. On the Uno they're A4 and A5, and on the ESP32 usually 21 and 22. Details of the bus itself are in the I2C article.
OLED (SSD1306)
A small display (usually 0.96 inch, 128×64 pixels, and there's a smaller 128×32):
- Very sharp and clear, with no backlight (each pixel lights by itself), so black is truly black.
- It shows graphics, text in different sizes, and icons.
- I2C (address usually 0x3C) or SPI.
- It runs on 3.3 V, and most modules accept 5 V too, so it's very well suited to the ESP32.
- Popular libraries: Adafruit SSD1306 and U8g2.
- The drawback: it's small, and if it shows the same image for months it can "burn in".
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
void setup() {
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
while (true); // display missing or not enough memory
}
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 0);
oled.print("24.6 C");
oled.display(); // nothing appears without it
}
void loop() {}
Note oled.display(): the library draws in memory first, and sends the whole image to the display when you tell it to.
Two recurring problems
- Memory on the Uno: a 128×64 display needs 1 KB of RAM for the image, and the Uno has only 2 KB in total. With other libraries,
beginfails and the display doesn't work. The fix: the U8g2 library in page mode (it draws piece by piece), or a bigger board such as the ESP32. - A 1.3-inch display showing random dots? The chip is probably an SH1106, not an SSD1306. They look almost identical, but it needs a library that supports it (U8g2 supports both).
Colour TFT
Colour displays from 1.8 to 3.5 inches and more, some with touch:
- SPI usually, and they need more pins (CS, DC and RESET besides the SPI lines).
- They need memory and speed, so they suit the ESP32 better than the Arduino Uno.
- The TFT_eSPI library is very popular with the ESP32, and is configured from a settings file where you choose the chip and the pins.
- The most common chips: ST7735 in the small ones (1.8 inch), ILI9341 in 2.4–2.8 inch, and ST7789 in small sharp displays.
- Signal voltage: most of these displays use 3.3 V signals. Connecting them directly to a 5 V Uno can damage them over time. Use a level shifter, resistors, or a 3.3 V board.
Quick comparison
| 16×2 LCD | 0.96 OLED | TFT | |
|---|---|---|---|
| Content | Letters and numbers | Text and graphics | Colour and images |
| Wires | 4 (with I2C) | 4 | 6–8 |
| Price | Cheap | Cheap | More expensive |
| Visibility in sunlight | Good | Poor | Medium |
| Suited to | Simple readings | Small, neat devices | Colourful interfaces |
Tips
- Don't update the display on every pass of the loop: update when the value changes, or every 200–500 ms, so you don't slow the program or cause flicker. Use millis for the timing.
- Don't call
clear()every time: that's the main cause of flicker. Write over the old content, and add spaces after the number to erase extra characters (when 100 becomes 99). - Voltage: if the display is 5 V and the microcontroller 3.3 V, check the signal levels.
- Several displays on the same I2C bus? That works if their addresses differ. LCD modules have solder pads (A0, A1, A2) that change the address, and most OLEDs have a resistor that moves it from 0x3C to 0x3D.
- Long wires on I2C cause problems. Keep them shorter than about 30 cm.
- If you're showing only numbers and need them large and visible from a distance, a 7-segment display may be better.
Display faults
| Fault | Usual cause |
|---|---|
| LCD lit but no text | Contrast, turn the potentiometer |
| LCD shows a row of blocks on top | Wrong address, or no init |
| The scanner finds nothing | SDA and SCL swapped or not connected |
| OLED completely black | You forgot display(), or there isn't enough memory |
| OLED random dots | The chip is an SH1106, not an SSD1306 |
| Old characters remain | You need spaces after the number |
| TFT white | The pins in the settings file don't match the wiring |
FAQ
Which display should I start with? A 16×2 LCD with an I2C module: cheap, easy, and needs no memory. Then an OLED when you need a nicer look.
How much current does a display draw? An OLED draws about 10–20 mA depending on the lit pixels, and most of an LCD's consumption is the backlight, which you can switch off with lcd.noBacklight() in battery projects.