Example Code for Arduino-Backlight Control
This project demonstrates how to toggle the backlight of the I2C 16x2 LCD module on and off at 1-second intervals while displaying text on the LCD.
Hardware Preparation
- DFR0063 I2C 16x2 Arduino LCD Display Module: 1, Purchase Link
- Arduino UNO: 1, Purchase Link (or Arduino Leonardo: 1, Purchase Link)
Software Preparation
- Arduino IDE 1.0 or later: Download Link
- LiquidCrystal_I2C library: Download Sample code and library
Wiring Diagram
The wiring diagram for connecting the module to Arduino is as follows:
Connection instructions:
- Arduino UNO: connect SDA to pin A4 and SCL to pin A5 on your Arduino.
- Arduino Leonardo: connect SDA to digital pin 2 and SCL to digital pin 3 on your Arduino.
Other Preparation Work
- Ensure the LCD module's I2C address is set to 0x20 (all jumper caps are connected by default).
- Install the LiquidCrystal_I2C library in the Arduino IDE: download the library zip file, open Arduino IDE, go to Sketch > Include Library > Add .ZIP Library, and select the downloaded file.
- Note: V1.2 has a different power pinout from V1.1, please check the history version for the old connection diagram.
Sample Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args) write(args);
#else
#define printByte(args) print(args,BYTE);
#endif
LiquidCrystal_I2C lcd(0x20,16,2); // set the LCD address to 0x20 for a 16 chars and 2 line display
void setup(){
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.home();
lcd.print("Hello world...");
lcd.setCursor(0, 1);
lcd.print("dfrobot.com");
}
int backlightState = LOW;
long previousMillis = 0;
long interval = 1000;
void loop(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (backlightState == LOW)
backlightState = HIGH;
else
backlightState = LOW;
if(backlightState == HIGH) lcd.backlight();
else lcd.noBacklight();
}
}
Result
The LCD module will display "Hello world..." on the first line and "dfrobot.com" on the second line. The backlight will toggle on and off every 1 second.
Additional Information
If you want to use the library's own sample code, pay attention to modify the initialization statement: change LiquidCrystal_I2C lcd(0x27,16,2); to LiquidCrystal_I2C lcd(0x20,16,2); (all jumpers should be connected!).
Was this article helpful?
