To make a message like “Hello” scroll across an LCD using an I2C module with a 16×2 LCD on an Arduino, you need to follow these steps. Below is the Arduino code and the circuit setup.
Circuit Setup:
- Components needed:
- Arduino Uno (or any compatible board)
- 16×2 LCD Display with I2C module
- Jumper wires
- Breadboard (optional)
- Connections:
- VCC (LCD) to 5V (Arduino)
- GND (LCD) to GND (Arduino)
- SDA (LCD) to A4 (Arduino Uno)
- SCL (LCD) to A5 (Arduino Uno)
Arduino Code:
#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal_I2C library
// Initialize the LCD with I2C address 0x27 (or adjust if necessary)
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16x2 LCD screen
void setup() {
lcd.begin(16, 2); // Initialize LCD with 16 columns and 2 rows
lcd.setBacklight(1); // Turn on the backlight
delay(1000); // Wait for 1 second
// Print a static message "Hello" for testing
lcd.setCursor(0, 0); // Set the cursor to the first row, first column
lcd.print("Hello"); // Print the "Hello" message
delay(1000); // Wait for 2 seconds before starting the scroll
}
void loop() {
String message = "Hello "; // The message to scroll
int messageLength = message.length();
// Loop through the positions and scroll the message from right to left
for (int position = 16; position >= -messageLength; position--) {
// Clear the first row to remove any previous message
lcd.setCursor(0, 0); // Set cursor at position (0, 0)
lcd.print(" "); // Print spaces to clear any existing text
// Set the cursor to the new position and print the message
lcd.setCursor(position, 0); // Set cursor at current position
lcd.print(message); // Print the message
delay(1000); // Delay to create the scrolling effect
}
}
Explanation of the Code:
- Library Includes:
- The
Wire.h
library is used for I2C communication. - The
LiquidCrystal_I2C.h
library is used to control the LCD via I2C.
- The
- LCD Initialization:
lcd.begin()
initializes the LCD.lcd.backlight()
turns on the LCD’s backlight.
- Scrolling Mechanism:
- The
message
variable holds the text “Hello” which we want to scroll. - In the
loop()
, the message is scrolled from right to left across the screen. - The
lcd.clear()
command clears the screen before printing the next portion of the message. lcd.setCursor(position, 0)
moves the cursor to the position from where the message will be printed.lcd.print(message)
displays the message at the current cursor position.- The
delay(300)
adds a delay of 300 milliseconds between each scroll.
- The
Final Notes:
- Make sure to install the
LiquidCrystal_I2C
library in the Arduino IDE. You can do this by going to Sketch > Include Library > Manage Libraries and then searching for LiquidCrystal_I2C and installing it. - The I2C address
0x27
is used in the code, but some LCD modules might have a different address like0x3F
. You can find the correct address by scanning for I2C devices using an I2C scanner sketch if you’re unsure.