About LCD I2C 16×2
- The LCD I2C module simplifies the wiring compared to a regular LCD.
- It consists of a normal LCD, an I2C module, and a potentiometer for contrast adjustment.
- The I2C interface reduces the number of required pins.
I2C Communication:
- The I2C (Inter-Integrated Circuit)protocol allows multiple devices to communicate over a shared bus using just two wires: SDA (data) and SCL (clock).
- By using I2C, you can connect multiple devices (including sensors, displays, and other peripherals) to your Arduino without consuming too many digital pins.
Components of an LCD I2C Module:
The LCD I2C module typically consists of the following components:
- LCD Display: The actual liquid crystal display with a specified number of columns and rows (e.g., 16×2 means 16 columns and 2 rows).
- I2C Interface Board: This board contains an I2C controller (usually a PCF8574 or PCF8574A chip) that communicates with the Arduino.
- Potentiometer: Used for adjusting the contrast of the display.
Advantages of Using LCD I2C:
- Reduced Wiring Complexity:Instead of using several digital pins for data and control signals, you only need two wires (SDA and SCL) to connect the LCD I2C module to the Arduino.
- Space-Saving:The I2C interface board is compact and can be mounted directly on the back of the LCD.
- Easy Initialization:The I2C library abstracts the initialization process, simplifying the setup.
I2C Address:
- Each LCD I2C module has a unique I2C address (usually set by soldering jumpers on the interface board).
- Common addresses include 0x27, 0x3F, and 0x20.
- You need to specify the correct address when initializing the LCD in your Arduino code.
Connection diagram
When using the Arduino Uno Board use the above schematics to use the LCD
If you are using other boards use the table below to make the appropriate connections.
Board | SDA | SCL |
---|---|---|
Arduino Uno | A4 | A5 |
Arduino Nano | A4 | A5 |
Arduino Micro | 2 | 3 |
Arduino Mega 2560 | 20 | 21 |
Arduino Leonardo | 2 | 3 |
Arduino Due | 20 | 21 |
The library
When using the Arduino Uno Board use the above schematics to use the LCD
Getting the I2C adress
#include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); while (!Serial); Serial.println("\nI2C Scanner"); } void loop() { byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for (address = 1; address < 127; address++ ) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address < 16) Serial.print("0"); Serial.print(address, HEX); Serial.println(" !"); nDevices++; } else if (error == 4) { Serial.print("Unknown error at address 0x"); if (address < 16) Serial.print("0"); Serial.println(address, HEX); } } if (nDevices == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); delay(5000); }
Hello World!
#include <Wire.h> // Library for I2C communication #include <LiquidCrystal_I2C.h> // Library for LCD // Wiring: SDA pin is connected to A4 and SCL pin to A5. // Connect to LCD via I2C, default address 0x27 LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x3F, 16, 2); // Change if needed void setup() { // Initiate the LCD: lcd.init(); lcd.backlight(); } void loop() { // Print 'Hello World!' on the first line of the LCD: lcd.setCursor(0, 0); // Set the cursor on the first column and first row. lcd.print("Hello World!"); // Print the string "Hello World!" lcd.setCursor(2, 1); //Set the cursor on the third column and the second row (counting starts at 0!). lcd.print("LCD tutorial"); }
Commands
The LiquidCrystal_I2C
library itself doesn’t provide a strict list of commands in the traditional sense. It offers functions that achieve various effects on the LCD. Here’s a breakdown of some key functions and their effects:
Initialization and Basic Control:
lcd.init()
: Initializes communication with the LCD.lcd.backlight()
: Turns on the backlight (if supported by the LCD).lcd.clear()
: Clears the entire display.
Text Display:
lcd.print("text")
: Prints the specified text to the LCD.lcd.setCursor(column, row)
: Sets the cursor position for the next text to be printed.lcd.rightToLef()
orlcd.leftToRight()
: Sets the text direction (right-to-left or left-to-right).lcd.autoscroll()
: Enables automatic scrolling of long text that goes beyond the display.lcd.noAutoscroll()
: Disables automatic scrolling.
Advanced Display Control:
lcd.scrollDisplayLeft()
orlcd.scrollDisplayRight()
: Scrolls the entire display content left or right.- (Custom characters): The library might provide functions for defining and uploading custom characters (refer to library documentation for specifics).