Control an I²C LCD with an Arduino

FunduinoLast updated on

Text und Messwerte mit nur vier Leitungen anzeigen

Ein LCD zeigt Messwerte, Menüs oder Statusmeldungen an – auch wenn kein Computer am Arduino hängt. Mit dem angelöteten I²C-Modul brauchst du dafür nur vier Leitungen.

In dieser Anleitung findest du die richtige I²C-Adresse, installierst die Bibliothek, bringst einen ersten Text aufs Display und zeigst zum Schluss einen Sensorwert an.

The I²C LCD on an older version of the Funduino UNO R3

You need this

1602 I2C LCD Module - 2x16 Characters, HD44780, Blue 1602 I2C LCD Module - 2x16 Characters, HD44780, Blue 3,02 € *Item no. FUN-1010568About the product — 1602 I2C LCD Module - 2x16 Characters, HD44780, Blue
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm

All 4 parts together 18,78 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

The I²C LCD

With an LCD, you can display letters and numbers - measured values, menus, or status messages, even when no computer is connected to the Arduino.

The I²C LCD has an I²C module soldered to the back. This means you only need four wires instead of many. The module also has a rotary knob for adjusting the contrast.

The I²C LCD on an older version of the Funduino UNO R3

The I²C address

A different I²C module may be installed depending on the display. The instructions work with all of them, but the address may differ and must be set correctly in the sketch. Common addresses are 0x27 and 0x3F. If your display shows nothing, try the other address.

  • Module without solder pads: fixed address, usually 0x27.
  • Module with solder pads: usually 0x3F, adjustable via the solder pads.

For more information about I²C addresses and how to read them, see the instructions Two I²C displays at the same time on the Arduino.

In the article, the solder pads are called “A1-A3.” On the module, they are labeled A0, A1, and A2. The reference to the instructions for two I²C displays is not linked in the article.

I²C module without solder joints with a fixed address
I²C module with solder pads marked in red for changing the address
1 / 2

The wiring

The I²C module has only four contacts:

  • VCC to 5V
  • GND to GND
  • SDA to A4
  • SCL to A5

On the Arduino MEGA2560, SDA and SCL have dedicated pins: 20 and 21.

In the parts list, the link “1* I2C LCD Display” leads to a breadboard, not a display. The caption for the MEGA labels the pins “SDA and SDL” - it should be SCL.

The I²C LCD on the Arduino MEGA2560 with SDA connected to pin 20 and SCL to pin 21

Install the library

You need the “LiquidCrystal I2C” library for the I²C LCD. It isn’t preinstalled; you can find it in the Arduino IDE Library Manager under the name “LiquidCrystal I2C by Marco Schwartz”. The instructions show you exactly how to install it Hardware and Software.

The LiquidCrystal I2C library in the Arduino IDE Library Manager

The program

In the sketch, you specify which display you are using: address, characters per line, and number of lines. For a 16×2 display with address 0x27, that means lcd(0x27, 16, 2); for a four-line display, lcd(0x27, 20, 4).

Use setCursor() to determine where the text starts – (0, 0) is the first character of the first line, and (0, 1) is the first character of the second.

Text on the I²C LCD
#include <Wire.h> // Include the Wire library
#include <LiquidCrystal_I2C.h> // Include the previously added LiquidCrystal_I2C library
LiquidCrystal_I2C lcd(0x27, 16, 2); //Here you specify the type of display. In this case, one with 16 characters in 2 lines and the HEX address 0x27. For a four-line I2C LCD, use the code "LiquidCrystal_I2C lcd(0x27, 20, 4)"
void setup()
{
lcd.init(); //The LCD is initialized in setup
lcd.backlight(); //Turn on the backlight (lcd.noBacklight(); turns off the backlight).
}
void loop()
{
lcd.setCursor(0, 0);//Here you specify the position of the first character. In this case, (0,0) means the first character in the first line.
lcd.print("Funduino GmbH");
lcd.setCursor(0, 1);// In this case, (0,1) means the first character in the second line.
lcd.print("Good luck!");
}

Extension: display a measurement value

As with a standard LCD, you can also display readings on an I²C LCD. In this example, a moisture sensor is connected to A0. The sketch reads the value every half second and writes it to the second line.

If the measured value gets shorter - for example, from 1000 to 999 - the last digit of the old number remains, and the display shows 9990. Solution: overwrite the line with spaces before outputting the value, or call lcd.clear().

Display the measured value on the I²C LCD
#include <Wire.h> // Include the Wire library
#include <LiquidCrystal_I2C.h> // Include the previously added LiquidCrystal_I2C library
LiquidCrystal_I2C lcd(0x27, 16, 2); // Specifies the display type. The HEX address 0x27 is a standard address for LCDs with a simple I²C module on the back. If the I²C module has solder pads for changing the HEX address, the standard address is "0x3F". In this case, this is an LCD with 16 characters in 2 lines (16,2). For a four-line I2C LCD, use the code "LiquidCrystal_I2C lcd(0x27, 20, 4)".
int messwert=0;
void setup()
{
lcd.init(); // Initialize the LCD in setup
lcd.backlight(); // Turn on the backlight (lcd.noBacklight(); turns off the backlight).
}
void loop()
{
messwert=analogRead(A0); // Read the measurement from analog input A0 and store it in the variable "messwert".
lcd.setCursor(0, 0); // Sets the position of the first character. In this case, (0,0) means the first character in the first line.
lcd.print("Messwert: ");
lcd.setCursor(0, 1); // In this case, (0,1) means the first character in the second line.
lcd.print(messwert);
delay(500);
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.