Driving a 16x2 character LCD over I2C with ESP8266 and Arduino
The 16x2 character LCD (two rows of sixteen characters) is the classic beginner display: cheap, readable, and everywhere. The annoyance is wiring — in parallel mode it eats six or more GPIO pins. An I2C backpack reduces that to two wires. This repository is my maintained LiquidCrystal_I2C library, compatible with both ESP8266 boards and classic Arduinos.
What I2C is, in one paragraph
I2C is a two-wire bus: one clock line (SCL) and one data line (SDA). Any number of devices
can share it, each with an address. The LCD backpack contains a small expander chip at address
0x27 (sometimes 0x3F); your sketch talks to that address, and the chip
wiggles the LCD's many parallel pins for you.
Quick start
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // address, columns, rows
void setup() {
lcd.begin(); // ESP8266: default pins 4 (SDA), 5 (SCL); Arduino Uno: A4/A5
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Hello, world!");
}
void loop() {}
On the tiny ESP-01 module there are almost no free pins, so the library also
supports lcd.begin(0, 2) to use GPIO0 as SDA and GPIO2 as SCL — the two pins that
are otherwise unusable anyway.
Code structure
LiquidCrystal_I2C.h/.cpp— the library itself: initialisation sequences, cursor control, scrolling, custom characters.examples/— four sketches:HelloWorld,BlinkingCursor,CustomChars(defining your own pixel symbols), andSerialDisplay(echo whatever arrives on the serial port to the screen).keywords.txt— makes the IDE syntax-highlight the library's methods.
I tested it on ESP-12 (LoLin), ESP-01 and Arduino Uno. The repository is a cleaned-up fork of the well-known fdebrabander library, with the ESP8266 pin handling that actually matches real boards. Get it at github.com/bobhuang1/LiquidCrystal_I2C.