How to Connect LCD to Arduino?
Connecting an LCD (Liquid Crystal Display) to an Arduino is a common task for displaying data. The most popular type is the 16x2 Character LCD, which can be interfaced in 4-bit or 8-bit mode (4-bit is more common to save pins).
Here’s a step-by-step guide:
1. Components Needed
Arduino board (Arduino Uno, Arduino Nano, etc.)
16x2 LCD (HD44780-compatible)
Potentiometer (10kΩ) (for contrast adjustment)
Breadboard & jumper wires
220Ω resistor (for backlight, if needed)
2. Wiring (4-Bit Mode – Recommended)
This method uses 6 Arduino pins (4 data + 2 control).
LCD Pin | Arduino Pin | Description |
---|---|---|
VSS (GND) | GND | Ground |
VDD (VCC) | 5V | Power |
VO (Contrast) | Potentiometer middle pin | Adjust contrast |
RS (Register Select) | D7 | HIGH = Data, LOW = Command |
RW (Read/Write) | GND | Set to GND (write mode) |
E (Enable) | D6 | Clock pulse to latch data |
D4 (Data 4) | D5 | Data bit 4 |
D5 (Data 5) | D4 | Data bit 5 |
D6 (Data 6) | D3 | Data bit 6 |
D7 (Data 7) | D2 | Data bit 7 |
A (Backlight +) | 5V via 220Ω resistor | Anode (+) |
K (Backlight –) | GND | Cathode (–) |
Contrast Adjustment
Connect VO to the middle pin of a 10kΩ potentiometer (other pins to 5V and GND).
3. Arduino Code (Using LiquidCrystal
Library)
#include <LiquidCrystal.h> // Initialize LCD (RS, E, D4, D5, D6, D7) LiquidCrystal lcd(7, 6, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); // Set up 16x2 LCD lcd.print("Hello, Arduino!"); // Print a message } void loop() { lcd.setCursor(0, 1); // Move to 2nd row lcd.print("Time: "); lcd.print(millis() / 1000); // Display seconds }
4. Alternative (I2C LCD – Fewer Wires)
If you have an I2C LCD module, it only needs 4 wires (GND, VCC, SDA, SCL).
Wiring
I2C LCD Pin | Arduino Pin |
---|---|
GND | GND |
VCC | 5V |
SDA | A4 (Uno) / SDA (Nano) |
SCL | A5 (Uno) / SCL (Nano) |
Code (Using LiquidCrystal_I2C
Library)
#include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16x2 LCD void setup() { lcd.init(); // Initialize LCD lcd.backlight(); // Turn on backlight lcd.print("Hello, I2C LCD!"); } void loop() { lcd.setCursor(0, 1); lcd.print("Millis: "); lcd.print(millis() / 1000); }
Note: The I2C address may vary (common:
0x27
or0x3F
). Use an I2C scanner if the LCD doesn’t work.
5. Troubleshooting
Issue | Solution |
---|---|
Blank display | Adjust contrast (VO) with potentiometer |
Garbled text | Check wiring (especially data pins) |
No backlight | Check 220Ω resistor on backlight (+) |
I2C LCD not working | Verify I2C address (0x27 /0x3F ) |
Final Recommendations
For simplicity: Use I2C LCD (only 4 wires).
For learning: Try 4-bit mode first.
For custom characters: Use
lcd.createChar()
.
评论
发表评论