Home

Guide no. 44

A 16×2 display with the I²C module: four wires, one library, and Arduino writes a greeting and the seconds going by.

By Ada Intelisano · informaticait.it technical team

Download the PDF (A4, 4 pages) Free to print and share, without changes — CC BY-ND 4.0
In short
  • Four wires: GND, VCC to 5V, SDA to A4, SCL to A5.
  • The hd44780 library finds the module address by itself.
  • setCursor(column, row) and print: counting starts at 0.
  • Blank display: adjust the contrast with the trimmer on the back.
Time: 25 minutesLevel: day sevenBefore: guide no. 34

So far Arduino has talked to you through the Serial Monitor, that is, through the computer. With a display it writes on its own: two rows of 16 characters. With the I²C module on the back, four wires are all you need.

1Get the parts ready

  • a 16×2 LCD display with the I²C module already soldered on the back
  • Arduino UNO and four female-to-male jumper wires
New word: I²CA way to connect many components using only two data wires: SDA (the data) and SCL (the clock). The module on the back of the display does the translating: without it you would need a dozen wires.

2Connect the four wires

Arduino UNO and 16×2 I²C LCDHello, world!Seconds: 42116GNDVCCSDASCLSCLSDAAREFGND131211109876543210IOREFRESET3.3V5VGNDGNDVINDIGITAL (PWM ~)POWERANALOG INUNOGND5VA4A516×2 display with I²C backpackinformaticait.it
  • GND of the module → GND of the board (here the one at the top, next to pin 13);
  • VCC → 5V;
  • SDA → A4;
  • SCL → A5.

On the UNO, pins A4 and A5 also work as SDA and SCL. The order of the four pins on the module changes from one manufacturer to another: read the labels printed next to the pins, don't trust the position.

✓ Done?Four wires, each on its own label. Plug in the USB: the display lights up.

3Install the library

In the Arduino IDE open the Library Manager, search for hd44780 and install the one by Bill Perry. It has a big advantage for beginners: it works out by itself the module address and how it is wired inside, two things that change from one module to another.

4Write and upload the sketch

// The LCD display: a greeting on the first row and the seconds ticking by on the second.
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp lcd;          // finds the address of the I2C module by itself

void setup() {
  lcd.begin(16, 2);          // 16 characters, 2 rows
  lcd.print("Hello, world!"); // writes from the first cell of the first row
}

void loop() {
  lcd.setCursor(0, 1);       // column 0, row 1 (the second one: counting starts at 0)
  lcd.print("Seconds: ");
  lcd.print(millis() / 1000);
  delay(500);
}
New word: setCursorsetCursor(column, row) moves the point where you will write. Counting starts at 0: setCursor(0, 1) is the first cell of the second row.
New word: millismillis() tells you how many thousandths of a second have passed since power-up. Divided by 1000, it gives the seconds.
✓ What you should seeOn the first row “Hello, world!”, on the second “Seconds:” with a number that goes up by one every second.
If it doesn't work
  • The display is lit but blank, or shows only solid rectangles: turn the blue trimmer on the back (the contrast) with the screwdriver until the letters appear.
  • It doesn't even light up: check VCC to 5V and GND to GND.
  • Lit, contrast adjusted, but no text: SDA and SCL are swapped or not on A4 and A5. The library also has a diagnostic example, I2CexpDiag: upload it and read what it prints in the Serial Monitor.

5Challenge yourself

Try it yourself
  • Write your name in the middle of the first row: with 16 cells, which column do you start from?
  • Show the TMP36 temperature (guide no. 40) instead of the seconds.
  • Make a countdown from 10 to 0 and at the end write “Go!”. Watch out: when you go from 10 to 9 an extra digit is left behind; write a space after the number.

6And with the ESP32?

One word of caution here: this display runs at 5 V, while the ESP32 pins can take 3.6 V at most, and the module pulls the SDA and SCL lines up to 5 V. The fix is a level shifter between the two: we explain it in guides no. 21 and 28. To get started, use the display with the UNO.

✓ What you learned
  • with the I²C module the display needs only four wires;
  • print writes, setCursor chooses where; rows and columns are counted from 0;
  • blank screen? The contrast trimmer comes first.

To learn more: guide no. 28 compares this display with an OLED.

Sources: hd44780 library by Bill Perry (hd44780_I2Cexp class, I2CexpDiag example) · Arduino, reference: Wire library, millis() · informaticait guides no. 21, 22, 28 and 40

Useful products

Other guides

All our guides →
Loading...
Back to top