Example Code for Arduino-Set RGB Color

Follow the pin description to connect the hardware, and download the sample code to UNO, after upload is successful, you can see the LCD display and backlight gradient. The backlit color of the LCD1602 module changes according to the input value. Users can learn how to use the DFRobot_RGBLCD1602 library to control the RGB backlight color of the LCD module via serial port input.

Hardware Preparation

Software Preparation

Wiring Diagram

DFR0554 Gravity I2C 16x2 Arduino LCD with RGB Font Display Connection Diagram
UNO R3 PIN LCD 1602 PIN
UNO R3 VCC LCD 1602 VCC(RED)
UNO R3 GND LCD 1602 GND(BLACK)
UNO R3 SCL LCD 1602 SCL(BLUE)
UNO R3 SDA LCD 1602 SDA (GREEN)

Other Preparation Work

  1. When uploaded the codes into UNO, open serial monitor, set baud rate to 115200 and select no EOF.
  2. 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;

/*
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   |
-----------------------------------------
*/

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

Input by a group of 4 numbers. For instance, for 12302540123, the input result will be 123,254,123.

Result

The backlit color of the LCD1602 module changes according to the input value

Was this article helpful?

TOP