LiquidCrystal: a 16x2 LCD on six Arduino pins
LiquidCrystal ships with the Arduino IDE and drives an HD44780 character LCD connected directly to digital pins, with no backpack. In the usual 4-bit wiring you need six signals: RS (register select), E (enable) and the data lines D4 to D7. RW goes to ground because the sketch only ever writes, and D0 to D3 stay unconnected.
Built in on Mokxi: #include <LiquidCrystal.h> and it builds, with nothing to install. Free, and no account needed.
What LiquidCrystal does
Two more connections are not signals at all, and they are where most first attempts go wrong. V0, the contrast pin, goes to the wiper of a 10 k惟 potentiometer between 5 V and ground; and pins 15 and 16 (A and K) power the backlight, usually through a 220 惟 resistor. Without the pot you will often see nothing even with perfect code.
This is the starter-kit way to wire an LCD, and it costs a lot of pins. If you are short of them, the same display on an I2C backpack needs only SDA and SCL and uses the LiquidCrystal_I2C library with almost the same calls.
| Library Manager name | LiquidCrystal |
|---|---|
| Author | Arduino, Adafruit |
| Include | #include <LiquidCrystal.h> |
| Boards | every board Mokxi compiles for |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Character LCD, 16x2, Character LCD, 20x4 |
Install it
It builds on every board Mokxi compiles for.
In Mokxi it is built in: #include <LiquidCrystal.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "LiquidCrystal" by Arduino, Adafruit from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add LiquidCrystal to lib_deps in PlatformIO. Mokxi follows the calls of release 1.0.7, so a sketch written here builds there too.
ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 ATtiny85 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
LiquidCrystal lcd(12, 11, 5, 4, 3, 2)
Names the pins in the order RS, E, D4, D5, D6, D7. The library also accepts eight data pins, and an RW pin, if you wire them.
lcd.begin(16, 2)
Tells the library the size of the display and sends the set-up sequence. A 20x4 module is lcd.begin(20, 4).
lcd.print(millis() / 1000)
Writes text or a number at the cursor, the same way Serial.print() formats it.
lcd.setCursor(col, row)
Moves the cursor. Row 0 is the top line; on a 16x2, column 15 is the last character of a row.
lcd.createChar(1, smiley)
Defines a custom character from eight bytes, one a row, five bits wide. Call setCursor() again afterward, since defining a character moves the controller鈥檚 address.
lcd.blink()
Shows a blinking block at the cursor; cursor() shows an underline instead. noBlink() and noCursor() hide them.
lcd.noDisplay()
Hides the text without erasing it, and display() shows it again: an easy way to flash a message.
lcd.noDisplay();
delay(300);
lcd.display();
delay(300);Example: Hello, world on a 16x2 LCD
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <LiquidCrystal.h>
// The LCD's RS, E and D4 to D7, as every kit's first LCD lesson wires them.
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
byte heart[8] = {0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000};
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.createChar(0, heart);
lcd.setCursor(0, 0);
lcd.print("hello, world!");
Serial.println("LCD ready");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
lcd.print(" s ");
lcd.write(byte(0));
delay(250);
}Wiring
- Arduino Uno R3 12 to Character LCD, 16x2 RS
- Arduino Uno R3 11 to Character LCD, 16x2 E
- Arduino Uno R3 5 to Character LCD, 16x2 D4
- Arduino Uno R3 4 to Character LCD, 16x2 D5
- Arduino Uno R3 3 to Character LCD, 16x2 D6
- Arduino Uno R3 2 to Character LCD, 16x2 D7
- Arduino Uno R3 GND to Character LCD, 16x2 RW
- Arduino Uno R3 GND to Character LCD, 16x2 V0
- Arduino Uno R3 5V to Character LCD, 16x2 VDD
- Arduino Uno R3 GND to Character LCD, 16x2 VSS
- Arduino Uno R3 5V to Character LCD, 16x2 A
- Arduino Uno R3 GND to Character LCD, 16x2 K
Common errors and how to fix them
The top row is solid blocks and the bottom row is empty
That is a powered display that has not been initialized, or contrast set too high. Check the V0 potentiometer, then check that RS and E are on the pins the constructor names and that begin() runs.
Random symbols instead of the text
One of D4 to D7 is loose or the constructor lists them in the wrong order. The order is RS, E, D4, D5, D6, D7; compare each wire against that list.
Text is there but only visible at an angle
Contrast is off. Wire V0 to a 10 k惟 pot between 5 V and GND and turn it, or as a fixed choice tie V0 to ground through a resistor of about 1 to 2 k惟.
Projects that use LiquidCrystal
Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.
Try LiquidCrystal without installing anything
Open the example, change the sketch, and keep your version in a free account.