Library and Arduino code for LCD2004 display with I2C
In this tutorial, we will explore how to use the LCD2004 display with I2C communication. This setup allows you to display text across four lines, making it ideal for various applications. We'll cover the required wiring, code snippets, and how to implement the functionality effectively.

We will be using the LiquidCrystal_I2C library to manage the LCD display. This library simplifies the interaction with the LCD by handling the I2C communication for you. For a comprehensive understanding, be sure to check the video for visual guidance (in video at 00:00).
Hardware Explained
The primary component for this project is the LCD2004 display, which features 20 columns and 4 rows for text output. It communicates using I2C, allowing for fewer connections compared to traditional parallel connections. This makes it suitable for projects with limited pin availability on the Arduino.
The I2C interface uses two lines, SDA (data line) and SCL (clock line), which connect to the corresponding pins on the Arduino. The LCD also requires power and ground connections. In this setup, we will connect the display to a 5V power source, which can be provided directly from the Arduino.
Datasheet Details
| Manufacturer | Generic |
|---|---|
| Part number | LCD2004 |
| Logic/IO voltage | 5 V |
| Supply voltage | 5 V |
| Output current (per channel) | Typically 20 mA |
| Peak current (per channel) | Typically 40 mA |
| PWM frequency guidance | N/A |
| Input logic thresholds | 0.3 Vcc (low), 0.7 Vcc (high) |
| Voltage drop / RDS(on) / saturation | Typically 0.2 V |
| Thermal limits | 0°C to 50°C |
| Package | Standard 4x20 LCD |
| Notes / variants | Available with different backlight options |
- Ensure proper power supply to avoid damage.
- Use pull-up resistors on SDA and SCL lines if necessary.
- Check I2C address; commonly 0x27 for this display.
- Keep connections short to minimize interference.
- Confirm that the library is correctly installed before uploading code.
Wiring Instructions
To wire the LCD2004 display with I2C to the Arduino, follow these connections:
- Connect the
VCCpin of the LCD to the5Vpin on the Arduino. - Connect the
GNDpin of the LCD to aGNDpin on the Arduino. - Connect the
SDApin of the LCD to theA4pin on the Arduino. - Connect the
SCLpin of the LCD to theA5pin on the Arduino.
If your Arduino board has different I2C pins, make sure to adjust the connections accordingly. The video also demonstrates an alternative wiring configuration (in video at 01:30).

Code Examples & Walkthrough
Let's look at a simple example of initializing the LCD and displaying a message. The following snippet sets up the LCD and prints "Hello, world!" on the screen.
lcd.begin(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.print("Hello, world!"); // Display message
This code initializes the LCD and activates the backlight. Then, it displays the message on the first line of the LCD. The backlight is essential for visibility in low-light conditions.
Next, we will examine a more complex example that displays dynamic data, such as voltage readings.
lcd.setCursor(0,0); // Set cursor to the first line
lcd.print("Voltage: "); // Display voltage label
float v = 8.254; // Example voltage value
lcd.print(v); // Print voltage value
In this excerpt, we set the cursor position to the first line and print a voltage label followed by the actual voltage value. This allows you to display real-time measurements in a user-friendly format.

For additional functionality, you can implement serial input to allow users to enter characters via the serial monitor and display them on the LCD.
if (Serial.available()) { // Check for serial input
lastChar = Serial.read(); // Read the last character
lcd.write(lastChar); // Display character on LCD
}
This code checks if there is any data available on the serial port. If so, it reads the last character entered and displays it on the LCD. This interaction enhances the user experience by allowing for dynamic displays based on user input.
For the complete code, please refer to the full program loaded below the article.
Demonstration / What to Expect
When you run the code, you should see the initial message displayed on the LCD, followed by the voltage readings and any characters entered through the serial monitor. Ensure that the connections are secure to avoid any display issues. If the LCD does not show anything, double-check the wiring and the I2C address (in video at 02:15).
/*
* Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
*
* Updated by Ahmad Shamshiri on July 8, 2018 at 19:14 in Ajax, Ontario, Canada
* for Robojax.com
* Get this code from Robojax.com
* Watch video instructions for this code at: https://youtu.be/DKmNSCMPDjE
*
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup()
{
// initialize the LCD
lcd.begin();
// Turn on the backlight and print a message.
lcd.backlight();
lcd.print("Hello, world!");
}
void loop()
{
// Do nothing here...
}
/*
*
* * Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
* This code is basic usage of an LCD2004 display with I2C.
* It will display text in 4 lines, each with 20 characters.
* It displays multiple float values and text on the same line.
*
*
*
* Updated by Ahmad Shamshiri on July 08, 2018 at 09:20 in Ajax, Ontario, Canada
* for Robojax.com
* Get this code from Robojax.com
* Watch video instructions for this code at: https://youtu.be/DKmNSCMPDjE
*
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
// initialize the LCD,
lcd.begin();
// Turn on the backlight and print a message.
lcd.backlight();
lcd.clear();
lcd.setCursor (0,0); //
lcd.print("Robojax LCD2004 Test");
lcd.setCursor (0,1); //
lcd.print("Please Wait - 3");
lcd.setCursor (0,1); //
delay(1000);
lcd.print("Please Wait - 2");
delay(1000);
lcd.setCursor (0,1); //
lcd.print("Please Wait - 1");
delay(1000);
}
void loop() {
// Robojax.com LCD2004 with I2C custom code
lcd.clear();// clear previous values from screen
lcd.setCursor (0,0); //character zero, line 1
lcd.print("LCD2004 I2C Example"); // print text
lcd.setCursor (4,1); //character 4, line 2
lcd.print("Robojax.com"); // print text
lcd.setCursor (0,2); //character 0, line 3
lcd.print("Voltage: "); // print text
float v = 8.254;// define or get voltage
char VoltageStr[5];
dtostrf(v, 5, 3, VoltageStr );
lcd.setCursor (9,2); //character 9, line 3
lcd.print(VoltageStr); // print voltage
lcd.setCursor (14,2); //character 14, line 3
lcd.print("V"); // print V at the end of voltage
lcd.setCursor (0,3); //character 0, line 4
lcd.print("X: "); // print x
float xdeg = -123.87;// define or get x degree (just example)
lcd.setCursor (3,3); //character 8, line 3
lcd.print(xdeg); // print xdeg value
lcd.setCursor (12,3); //character 12, line 4
lcd.print("Y: "); // print Y
float ydeg = 32.8;// define or get y degree (just example)
lcd.setCursor (15,3); //character 15, line 4
lcd.print(ydeg); // print ydeg value
delay(100);
}// loop end
/*
*
* Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
* This code is basic usage of an LCD2004 display with I2C
* It will display a blinking cursor on the screen
*
* Get this code from Robojax.com
* Watch video instructions for this code at: https://youtu.be/DKmNSCMPDjE
*
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup()
{
// initialize the LCD
lcd.begin();
}
void loop()
{
bool blinking = true;
lcd.cursor();
while (1) {
if (blinking) {
lcd.clear();
lcd.print("No cursor blink");
lcd.noBlink();
blinking = false;
} else {
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Your name: ");
lcd.blink();
blinking = true;
}
delay(4000);
}
}
/*
*
* Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
* This code is basic usage of an LCD2004 display with I2C.
* This code will allow you to enter a character on the serial monitor and display it on the screen.
*
* Updated by Ahmad Shamshiri on July 08, 2018 at 09:20 in Ajax, Ontario, Canada
* for Robojax.com
* Get this code from Robojax.com
* Watch video instructions for this code at: https://youtu.be/DKmNSCMPDjE
*
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
char lastChar ='_';
void setup()
{
lcd.begin();
lcd.backlight();
lcd.print("Robojax.com Test");
// Initialize the serial port at a speed of 9600 baud
Serial.begin(9600);
}
void loop()
{
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Enter Letter: ");
lcd.setCursor(13,1);
// If characters arrived over the serial port...
if (Serial.available()) {
// Wait a bit for the entire message to arrive
delay(100);
// Write all characters received via the serial port to the LCD.
while (Serial.available() > 0) {
lastChar =Serial.read();
Serial.print("Entered: ");
Serial.println(lastChar);
}//while end
}// if end
lcd.write(lastChar);// display last entered character
delay(1000);
}// loop end
Ресурсы и ссылки
-
ВнешнийГенератор персонажей (GitHub)maxpromer.github.io
-
ВнешнийГенератор персонажей (вторичный источник)omerk.github.io
-
ВнешнийПолучить библиотеку с GitHubgithub.com
Файлы📁
Нет доступных файлов.