Example Code for Arduino-Set RGB Backlight Color
Connect all parts as the diagram, upload the sample code into your UNO, then you can input RGB values to adjust backlit color.
Hardware Preparation
- DFRduino UNO (or similar) x 1
- Gravity IO expansion Shield x1
- Gravity: I2C LCD1602 RGB Backlight Module x1
- M-M/F-M/F-F Jumper wires
Software Preparation
- Arduino IDE, Click to Download Arduino IDE from Arduino®
- Please download the LCD library: DFRobot RGB LCD. About How to install Libraries in Arduino IDE
Wiring Diagram
Other Preparation Work
Change the RGBaddr value based on the hardware version
| Moudule | Version | RGBAddr |
|---|---|---|
| LCD1602 Module | V1.0 | 0x60 |
| LCD1602 Module | V1.1 | 0x6B |
| LCD1602 RGB Module | V1.0 | 0x60 |
| LCD1602 RGB Module | V2.0 | 0x2D |
When uploaded the codes into UNO, open serial monitor, set baud rate to 115200 and select no EOF.
Note: you have to reset the mainboard for every input. The RGB range is 0-255.
Sample Code
/*!
* @file SetColor.ino
* @brief Serial port input Format:255 255 255,There is no end or newline
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @maintainer [yangfeng]([email protected])
* @version V1.0
* @date 2021-09-24
* @url https://github.com/DFRobot/DFRobot_RGBLCD1602
*/
#include "DFRobot_RGBLCD1602.h"
char dtaUart[15];
char dtaLen = 0;
DFRobot_RGBLCD1602 lcd(/*RGBAddr*/0x2D ,/*lcdCols*/16,/*lcdRows*/2); //16 characters and 2 lines of show
void setup() {
Serial.begin(115200);
/**
* @brief initialize the LCD and master IIC
*/
lcd.init();
// Print a message to the LCD.
lcd.print("set color");
}
void loop() {
if(dtaLen == 11) {
int r = (dtaUart[0]-'0')*100 + (dtaUart[1] - '0')*10 + (dtaUart[2] - '0'); // get r
int g = (dtaUart[4]-'0')*100 + (dtaUart[5] - '0')*10 + (dtaUart[6] - '0');
int b = (dtaUart[8]-'0')*100 + (dtaUart[9] - '0')*10 + (dtaUart[10] - '0');
dtaLen = 0;
/**
* @brief set RGB
* @param r red range(0-255)
* @param g green range(0-255)
* @param b blue range(0-255)
*/
lcd.setRGB(r, g, b);
Serial.println("get data");
Serial.println(r);
Serial.println(g);
Serial.println(b);
Serial.println();
}
}
void serialEvent() {
while(Serial.available()) {
dtaUart[dtaLen++] = Serial.read();
}
}
Result
The backlit color of the LCD1602 module changes according to the input value.



Additional Information
Input by a group of 4 numbers. For instance, for 12302540123, the input result will be 123,254,123.
Was this article helpful?
