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

Code

Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.

Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “11.1_Soft_LED” and double click “Soft_LED.py”.

11.1_Soft_LED

../../../_images/Chapter11_11.png

Click “Run current script”. Rotate the handle of potentiometer and the brightness of LED will change correspondingly.

The following is the code:

 1from machine import Pin,PWM,ADC
 2import time
 3
 4pwm =PWM(Pin(25,Pin.OUT),1000)
 5adc=ADC(Pin(36))
 6adc.atten(ADC.ATTN_11DB)
 7adc.width(ADC.WIDTH_10BIT)
 8
 9try:
10    while True:
11        adcValue=adc.read()
12        pwm.duty(adcValue)
13        print(adc.read())
14        time.sleep_ms(100)
15except:
16    pwm.deinit()

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

Code

Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “11.2_Soft_Colorful_Light” and double click “Soft_Colorful_Light.py”.

11.2_Soft_Colorful_Light

Click “Run current script” and control the change of RGBLED color by rotating the handles of three rotary potentiometers.

The following is the program code:

from machine import Pin,PWM,ADC
import time

pwm0=PWM(Pin(15,Pin.OUT),10000)
pwm1=PWM(Pin(2,Pin.OUT),10000)
pwm2=PWM(Pin(5,Pin.OUT),10000)
adc0=ADC(Pin(36))
adc1=ADC(Pin(39))
adc2=ADC(Pin(34))
adc0.atten(ADC.ATTN_11DB)
adc1.atten(ADC.ATTN_11DB)
adc2.atten(ADC.ATTN_11DB)
adc0.width(ADC.WIDTH_10BIT)
adc1.width(ADC.WIDTH_10BIT)
adc2.width(ADC.WIDTH_10BIT)

try:
    while True:
        pwm0.duty(1023-adc0.read())
        pwm1.duty(1023-adc1.read())
        pwm2.duty(1023-adc2.read())
        time.sleep_ms(100)
except:
    pwm0.deinit()
    pwm1.deinit()
    pwm2.deinit()
















In the code, read the ADC value of 3 potentiometers and map it into PWM duty cycle to control the control 3 LEDs with different color of RGBLED, respectively.

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

Code

Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “11.3_Soft_Rainbow_Light” and double click “Soft_Rainbow_Light.py”.

11.3_Soft_Rainbow_Light

../../../_images/Chapter11_12.png

Click “Run current script”. Rotate the handle of potentiometer and the color of the lights will change.

../../../_images/Chapter11_13.png

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

The following is the program code:

 1from machine import Pin,ADC
 2import neopixel
 3import time
 4
 5np = neopixel.NeoPixel(Pin(2, Pin.OUT), 8)
 6
 7brightness=0.1         #brightbess
 8red=0                  #red
 9green=0                #green
10blue=0                 #blue
11
12adc0=ADC(Pin(36))
13adc0.atten(ADC.ATTN_11DB)
14adc0.width(ADC.WIDTH_10BIT)
15
16def wheel(pos):
17    global red,green,blue
18    WheelPos=pos%255
19    if WheelPos<85:
20        red=(255-WheelPos*3)
21        green=(WheelPos*3)
22        blue=0
23    elif WheelPos>=85 and WheelPos<170:
24        WheelPos -= 85;
25        red=0
26        green=(255-WheelPos*3)
27        blue=(WheelPos*3)
28    else :
29        WheelPos -= 170;
30        red=(WheelPos*3)
31        green=0
32        blue=(255-WheelPos*3)
33
34while True:
35        adcValue=adc0.read()//4
36        for j in range(0,8):
37            wheel(adcValue+j*255//8)
38            np[j]=(int(red*brightness),int(green*brightness),int(blue*brightness))
39        np.write()
40        time.sleep_ms(100)
41
42
43
44
45

The logic of the code is basically the same as the previous project Rainbow Light. The difference is that in this code, the starting point of the color is controlled by the potentiometer.