Chapter 11 Potentiometer & LED

We have learned how to use ADC and DAC before. When using DAC output analog to drive LED, we found that, when the output voltage is less than led turn-on voltage, the LED does not light; when the output analog voltage is greater than the LED voltage, the LED lights. This leads to a certain degree of waste of resources. Therefore, in the control of LED brightness, we should choose a more reasonable way of PWM control. In this chapter, we learn to control the brightness of LED through a potentiometer.

Project 11.1 Soft Light

In this project, we will make a soft light. We will use an ADC Module to read ADC values of a potentiometer and map it to duty cycle of the PWM used to control the brightness of a LED. Then you can change the brightness of a LED by adjusting the potentiometer.

Component List

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

LED x1

Chapter01_03

Resistor 220Ω x1

Chapter01_04

Jumper M/M x5

Chapter01_05

Rotary potentiometer x1

Chapter09_00

Circuit

Schematic diagram

Chapter11_00

Hardware connection

If you need any support, please contact us via: support@freenove.com

Chapter11_01

Sketch

Sketch_11.1_Softlight

../../../_images/Chapter11_02.png

Download the code to ESP32-WROVER, by turning the adjustable resistor to change the input voltage of GPIO25, ESP32 changes the output voltage of GPIO4 according to this voltage value, thus changing the brightness of the LED.

The following is the code:

 1/**********************************************************************
 2  Filename    : SoftLight
 3  Description : Controlling the brightness of LED by potentiometer.
 4  Auther      : www.freenove.com
 5  Modification: 2024/06/18
 6**********************************************************************/
 7#define PIN_ANALOG_IN   4
 8#define PIN_LED         25
 9#define CHAN            0
10void setup() {
11  ledcAttachChannel(PIN_LED, 1000, 12, CHAN);
12}
13
14void loop() {
15  int adcVal = analogRead(PIN_ANALOG_IN); //read adc
16  int pwmVal = adcVal;                    // adcVal re-map to pwmVal
17  ledcWrite(PIN_LED, pwmVal);             // set the pulse width.
18  delay(10);
19}

In the code, read the ADC value of potentiometer and map it to the duty cycle of PWM to control LED brightness.

Project 11.2 Soft Colorful Light

In this project, 3 potentiometers are used to control the RGB LED and in principle it is the same as the Soft Light project. Namely, read the voltage value of the potentiometer and then convert it to PWM used to control LED brightness. Difference is that the original project only controlled one LED, but this project required (3) RGB LEDs.

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

RGBLED x1

Chapter11_03

Resistor 220Ω x3

Chapter01_04

Jumper M/M x13

Chapter01_05

Rotary potentiometer x3

Chapter09_00

Circuit

Schematic diagram

Chapter11_04

Hardware connection

If you need any support, please contact us via: support@freenove.com

Chapter11_05

Sketch

Sketch_11.2_SoftColorfulLight

../../../_images/Chapter11_06.png

Download the code to ESP32-WROVER, rotate one of the potentiometers, then the color of RGB LED will change.

The following is the program code:

 1/**********************************************************************
 2  Filename    : SoftColorfulLight
 3  Description : Controlling the color of RGBLED by potentiometer.
 4  Auther      : www.freenove.com
 5  Modification: 2024/06/18
 6**********************************************************************/
 7const byte ledPins[] = {15, 2, 4};      //define led pins
 8const byte pwmChns[] = {0, 1, 2};       //define the pwm channels
 9const byte adcChns[] = {13, 12, 14};    // define the adc channels
10int colors[] = {0, 0, 0};               // red, green ,blue values of color.
11void setup() {
12  for (int i = 0; i < 3; i++) {   //setup the pwm channels
13    ledcAttachChannel(ledPins[i], 1000, 8, pwmChns[i]);
14  }
15}
16
17void loop() {
18  for (int i = 0; i < 3; i++) {
19    colors[i] = map(analogRead(adcChns[i]), 0, 4096, 0, 255); //calculate color value.
20    ledcWrite(ledPins[i], 256 - colors[i]);   //set color
21  }
22  delay(10);
23}

In the code you can read the ADC values of the 3 potentiometers and map it into a PWM duty cycle to control the 3 LED elements to vary the color of their respective RGB LED.

Project 11.3 Soft Rainbow Light

In this project, we use potentiometer to control Freenove 8 RGB LED Module.

Component List

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

Freenove 8 RGB LED Module x1

Chapter11_08

Jumper F/M x3 Jumper M/M x3

Chapter11_07

Sketch

Sketch_11.3_Soft_Rainbow_Light

../../../_images/Chapter11_09.png

Download the code to ESP32-WROVER, rotate the handle of the potentiometer, and the color of the lamp ring will change.

../../../_images/Chapter11_10.png

If you need any support, please feel free to contact us via: support@freenove.com

The following is the program code:

 1/**********************************************************************
 2  Filename    : SoftRainbowLight
 3  Description : Make the strip light up in rainbow colors.
 4  Auther      : www.freenove.com
 5  Modification: 2024/06/18
 6**********************************************************************/
 7#include "Freenove_WS2812_Lib_for_ESP32.h"
 8
 9#define PIN_POT     13    // Define analog input pins
10#define LEDS_COUNT  8     // The number of led
11#define LEDS_PIN    2     // define the pin connected to the Freenove 8 led strip
12#define CHANNEL     0     // RMT module channel
13#define BRIGHTNESS  100   // breghtness, the value range is 0-255.
14Freenove_ESP32_WS2812 strip = Freenove_ESP32_WS2812(LEDS_COUNT, LEDS_PIN, CHANNEL, TYPE_GRB);
15
16void setup() {
17  strip.setBrightness(BRIGHTNESS);
18  strip.begin();
19}
20
21void loop() {
22  int colorPos = map(analogRead(PIN_POT), 0, 4095, 0, 255);
23  for (int i = 0; i < LEDS_COUNT; i++) {
24    strip.setLedColorData(i,strip.Wheel(colorPos + i * 255 / 8)); // Set color data.
25  }
26  strip.show(); // Send color data to LED, and display.
27  delay(10);
28}

The overall logical structure of the code is the same as the previous project rainbow light, except that the starting point of the color in this code is controlled by potentiometer.