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

Software Preparation

Wiring Diagram

The wiring diagram for connecting the module to Arduino is as follows:

DFR0063 I2C 16x2 Arduino LCD Display Module Connection

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

  1. Ensure the LCD module's I2C address is set to 0x20 (all jumper caps are connected by default).
  2. 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.
  3. 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?

TOP