Example Code for Arduino-LED Brightness Adjustment

Last revision 2025/12/09

This article provides a comprehensive guide for adjusting the brightness of an RGB LED ring using an Arduino Uno and an analog rotation sensor, including hardware setup, library integration, wiring diagrams, and sample code for effective LED control.

Hardware Preparation

  • Analog Rotation Sensor × 1
  • RGB LED ring- 12 LED beads × 1
  • Arduino uno × 1
  • IO Expansion Board × 1

Software Preparation

(1) Import the library file of RGB LED into Arduino library first, find the installation location of Arduino, and put the file of RGB LED ring into the libraries of Arduino.

DFRobot_NeoPixel.zip

(2) Before using the RGB LED ring, enter Arduino and click "Toolbar- Development Board" (as shown in the picture)

Wiring Diagram

Other Preparation Work

(1) Import the library file of RGB LED into Arduino library first, find the installation location of Arduino, and put the file of RGB LED ring into the libraries of Arduino.

(2) Before using the RGB LED ring, enter Arduino and click "Toolbar- Development Board" (as shown in the picture)

Sample Code

/*!
 * @file        RGB ring.ino
 * @brief       The colors of the 12 lamp beads are displayed in gradients. Adjust the brightness of the RGB LED ring using the analog rotation sensor.
 * @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-24
 * @get from    https://www.dfrobot.com
 * @url         
 */

#include <DFRobot_NeoPixel.h>

// Dynamic variable
volatile float brightness;
// Create an Object
DFRobot_NeoPixel neoPixel_2;


// Main program start
void setup() {
  neoPixel_2.begin(2, 24);
  brightness = 0;
}
void loop() {
  neoPixel_2.setBrightness((map(brightness, 0, 749, 0, 255)));
  neoPixel_2.showRainbow(0, 23, 1, 360);
  brightness = analogRead(A0);
}

Result

Adjust the brightness of the RGB LED ring from 0 to 255 using the analog rotation sensor.

Was this article helpful?

TOP