Example Code for Arduino-RGB Color Adjustment

Last revision 2026/01/15

This article guides readers through the process of using Arduino and rotary encoders to adjust RGB LED colors, providing detailed instructions on hardware and software setup, wiring, and sample code for precise color control.

Hardware Preparation

Software Preparation

Wiring Diagram

Connection 1

Other Preparation Work

When multiple encoders are cascaded, it is necessary to set the I2C address in order to use them properly.I2C Setting Method

Sample Code

  /*!
 * @file        04.ino
 * @brief       Set encoder address and then adjust RGB light color through the three cascaded encoders.
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author      [qsjhyy]([email protected])
 * @version     V0.1
 * @date        2021-09-27
 * @get from    https://www.dfrobot.com
 * @url         
 */
 #include <DFRobot_NeoPixel.h>

// Dynamic variable
volatile float R, G, B;
// Function declaration 
uint32_t rgbToColor(uint8_t r, uint8_t g, uint8_t b);
// Create object 
DFRobot_NeoPixel neoPixel_2;
#include <DFRobot_VisualRotaryEncoder.h>
/*
Instantiate an object to drive our sensor;
Set address according to encoder DIP switch::
| 1 | 2 | ADDR |
|---|---|------|
| 0 | 0 | 0x54 |
| 0 | 1 | 0x55 |
| 1 | 0 | 0x56 |
| 1 | 1 | 0x57 |
*/
DFRobot_VisualRotaryEncoder_I2C sensor_1(/*iicAddr = */0x54, /*iicBus = */&Wire);
DFRobot_VisualRotaryEncoder_I2C sensor_2(/*iicAddr = */0x55, /*iicBus = */&Wire);
DFRobot_VisualRotaryEncoder_I2C sensor_3(/*iicAddr = */0x56, /*iicBus = */&Wire);

void setup()
{
  neoPixel_2.begin(2, 12); //Set RGB light pin to 2, the number of LED to 12 
  neoPixel_2.setBrightness(255);

  Serial.begin(115200);

  // Init sensor 
  while( NO_ERR != sensor_1.begin()&&NO_ERR != sensor_2.begin()&&NO_ERR != sensor_3.begin() ){
    Serial.println("Communication with device failed, please check connection");
    delay(3000);
  }
  Serial.println("Begin ok!");

  /**
   * Set encoder gain coefficient, accuracy value to rotate one detent 
   * Accuracy range: 1~51, the minimum is 1 (light up one LED about every 2.5 turns), the maximum is 51 (light up one LED every one detent rotation)
   * gainValue Range[1, 51], setting invalid when out of range.
   */
  sensor_1.setGainCoefficient(30);
  sensor_2.setGainCoefficient(30);
  sensor_3.setGainCoefficient(30);
  /**
   * Set encoder gain coefficient, accuracy value to rotate one detent 
   * Accuracy range: 1~51, the minimum is 1 (light up one LED about every 2.5 turns), the maximum is 51 (light up one LED every one detent rotation)
   * Return range: 1-51
   */
  uint8_t gainCoefficient_1 = sensor_1.getGainCoefficient();
  uint8_t gainCoefficient_2 = sensor_2.getGainCoefficient();
  uint8_t gainCoefficient_3 = sensor_3.getGainCoefficient();

  Serial.print("Encoder current gain coefficient: ");
  Serial.println(gainCoefficient_1);
  Serial.println(gainCoefficient_2);
  Serial.println(gainCoefficient_3);

  Serial.println();
  delay(1000);
}

void loop()
{
  /**
   * Detect if the button is pressed. 
   * Return true Button pressed, false, button unpressed 
   */
  if(sensor_1.detectButtonDown()){
    /**
     * Set the encoder count value
     * value Range[0, 1023], setting invalid when out of range
     * In this example, when the button is detected to be pressed, reset the encoder value to zero, then all the LEDs that light up before switch off.  
     */
    sensor_1.setEncoderValue(0);
  }

  if(sensor_2.detectButtonDown()){
    /**
     * Set the encoder count value
     * value Range[0, 1023], setting invalid when out of range
     * In this example, when the button is detected to be pressed, reset the encoder value to zero, then all the LEDs that light up before switch off.
     */
    sensor_2.setEncoderValue(0);
  }

  if(sensor_3.detectButtonDown()){
    /**
     * Set the encoder count value
     * value Range[0, 1023], setting invalid when out of range
     * In this example, when the button is detected to be pressed, reset the encoder value to zero, then all the LEDs that light up before switch off.
     */
    sensor_3.setEncoderValue(0);
  }

  /**
   * Get encoder current count value
   * Return value range: 0-1023
   */
  uint16_t encoderValue_1 = sensor_1.getEncoderValue();
  uint16_t encoderValue_2 = sensor_2.getEncoderValue();
  uint16_t encoderValue_3 = sensor_3.getEncoderValue();

  Serial.print("encoderValue_1: ");
  Serial.println(encoderValue_1);
   Serial.print("encoderValue_2: ");
  Serial.println(encoderValue_2);
   Serial.print("encoderValue_3: ");
  Serial.println(encoderValue_2);
  Serial.println();

  R = (map(encoderValue_1, 0, 1023, 0, 255));
  G = (map(encoderValue_2, 0, 1023, 0, 255));
  B = (map(encoderValue_3, 0, 1023, 0, 255));
  neoPixel_2.setRangeColor(0, 11, rgbToColor(round(R), round(G), round(B)));
  Serial.print("R: ");
  Serial.println(round(R));
  Serial.print("G: ");
  Serial.println(round(G));
  Serial.print("B: ");
  Serial.println(round(B));
  Serial.println();
  delay(1000);
}

// Static function 
uint32_t rgbToColor(uint8_t r, uint8_t g, uint8_t b)
{
  return (uint32_t)((((uint32_t)r<<16) | ((uint32_t)g<<8)) | (uint32_t)b);
}

Result

Result 4

Data on serial monitor:

Result 4-1

Was this article helpful?

TOP