Example Code for Arduino-Control LED Brightness

Last revision 2026/01/15

This article offers a comprehensive guide on controlling LED brightness using Arduino, featuring detailed instructions on hardware and software setup, I2C settings, and sample code implementation.

Hardware Preparation

Software Preparation

Other Preparation Work

Ensure the rotary encoder's I2C address is set to 0x54 via the DIP switches on the back of the module.I2C Setting Method

Sample Code

/*!
 * @file        setSensor.ino
 * @brief       This demo shows how to control LED light brightness by the encoder. 
 * @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-15
 * @get from    https://www.dfrobot.com
 * @url         https://github.com/DFRobot/DFRobot_VisualRotaryEncoder
 */
#include <DFRobot_VisualRotaryEncoder.h>

/*
Instantiate an object to drive our sensor;
Set address according to encoder DIP: 
| 1 | 2 | ADDR |
|---|---|------|
| 0 | 0 | 0x54 |
| 0 | 1 | 0x55 |
| 1 | 0 | 0x56 |
| 1 | 1 | 0x57 |
*/
DFRobot_VisualRotaryEncoder_I2C sensor(/*iicAddr = */0x54, /*iicBus = */&Wire);

// #include <Servo.h>

// Servo myservo;
int LED = 0;    // Init LED brightness 
int ledPin = 10;  // Set LED light connection pin 
void setup()
{
  pinMode(ledPin, OUTPUT);

  Serial.begin(115200);

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

  /**
   * Set encoder gain coefficient, accuracy value to rotate a 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.setGainCoefficient(51);

  /**
   * Get current encoder coefficient, accuracy value to rotate one detect. 
   * 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 value range: 1-51
   */
  uint8_t gainCoefficient = sensor.getGainCoefficient();
  Serial.print("Encoder current gain coefficient: ");
  Serial.println(gainCoefficient);

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

void loop()
{
  /**
   * Detect if the button is pressed 
   * Return true, button pressed. Return false: button unpressed 
   */
  if(sensor.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.setEncoderValue(0);
  }

  /**
   * Get encoder current count value 
   * Return value: 0-1023
   * Map enocder value to LED brightness 
   */
  uint16_t encoderValue = sensor.getEncoderValue();
  Serial.print("The encoder currently counts: ");
  Serial.println(encoderValue);
  LED = map(encoderValue, 0, 1023, 0, 255);
  analogWrite(ledPin,LED);   //Write corresponding value to LED   
  Serial.println();
  delay(1000);
}

Was this article helpful?

TOP