Chapter 5 RGB LED
In this chapter, we will learn how to control a RGB LED. It can emit different colors of light. Next, we will use RGB LED to make a multicolored light.
Project 5.1 Random Color Light
In this project, we will make a multicolored LED. And we can control RGB LED to switch different colors automatically.
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
|
Breadboard x1
|
||
RGBLED x1
|
Resistor 220Ω x1
|
Jumper M/M x2
|
Circuit
Schematic diagram |
|
Hardware connection |
If you need any support, please feel free to contact us via: support@freenove.com
|
Code
We need to create three PWM channels and use random duty cycle to make random RGBLED color.
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” -> “05.1_RandomColorLight”and double click “RandomColorLight.py”.
05.1_RandomColorLight
Click “Run current script”, RGBLED begins to display random colors.
If you have any concerns, please contact us via: support@freenove.com
The following is the program code:
1from machine import Pin,PWM
2from random import randint
3import time
4
5pins=[4,2,15]
6
7pwm0=PWM(Pin(pins[0]),10000)
8pwm1=PWM(Pin(pins[1]),10000)
9pwm2=PWM(Pin(pins[2]),10000)
10
11def setColor(r,g,b):
12 pwm0.duty(1023-r)
13 pwm1.duty(1023-g)
14 pwm2.duty(1023-b)
15
16try:
17 while True:
18 red = randint(0,1023)
19 green = randint(0,1023)
20 blue = randint(0,1023)
21 setColor(red,green,blue)
22 time.sleep_ms(200)
23except:
24 pwm0.deinit()
25 pwm1.deinit()
26 pwm2.deinit()
Import Pin, PWM and Randon Function modules.
1from machine import Pin,PWM
2from random import randint
3import time
Configure ouput mode of GPIO15, GPIO2 and GPIO0 as PWM output and PWM frequency as 10000Hz
1pins=[4,2,15]
2
3pwm0=PWM(Pin(pins[0]),10000)
4pwm1=PWM(Pin(pins[1]),10000)
5pwm2=PWM(Pin(pins[2]),10000)
Define a function to set the color of RGBLED.
1def setColor(r,g,b):
2 pwm0.duty(1023-r)
3 pwm1.duty(1023-g)
4 pwm2.duty(1023-b)
Call random function randint()to generate a random number in the range of 0-1023 and assign the value to red.
1red = randint(0,1023)
Obtain 3 random number every 200 milliseconds and call function setColor to make RGBLED display dazzling colors.
1while True:
2 red = randint(0,1023)
3 green = randint(0,1023)
4 blue = randint(0,1023)
5 setColor(red,green,blue)
6 time.sleep_ms(200)
Reference
- Class random
Before each use of the module random , please add the statement “ import random “ to the top of Python file.
randint(start, end): Randomly generates an integer between the value of start and end.
start: Starting value in the specified range, which would be included in the range.
end: Ending value in the specified range, which would be included in the range.
random(): Randomly generates a floating point number between 0 and 1.
random.unifrom(start, end): Randomly generates a floating point number between the value of start and end
start: Starting value in the specified range, which would be included in the range.
end: Ending value in the specified range, which would be included in the range.
random.getrandbits(size): Generates an integer with size random bits
For example:
size = 4, it generates an integer in the range of 0 to 0b1111
size = 8, it generates an integer in the range of 0 to 0b11111111
random.randrange(start, end, step): Randomly generates a positive integer in the range from start to end and increment to step.
start: Starting value in the specified range, which would be included in the range
end: Ending value in the specified range, which would be included in the range.
step: An integer specifying the incrementation.
random.seed(sed): Specifies a random seed, usually being applied in conjunction with other random number generators
sed: Random seed, a starting point in generating random numbers.
random.choice(obj): Randomly generates an element from the object obj.
obj: list of elements
Project 5.2 Gradient Color Light
In the previous project, we have mastered the usage of RGBLED, but the random color display is rather stiff. This project will realize a fashionable Light with soft color changes.
Component list, the circuit is exactly the same as the project random color light.
Using a color model, the color changes from 0 to 255 as shown below.
In this code, the color model will be implemented and RGBLED will change colors along the model.
Open “Thonny”, click”This computer” -> “D:” -> “Micropython_Codes” -> “05.2_GradientColorLight” and double click “GradientColorLight.py”.
The following is the program code:
1from machine import Pin,PWM
2import time
3
4pins=[4,2,15];
5
6pwm0=PWM(Pin(pins[0]),1000)
7pwm1=PWM(Pin(pins[1]),1000)
8pwm2=PWM(Pin(pins[2]),1000)
9
10red=0 #red
11green=0 #green
12blue=0 #blue
13
14def setColor():
15 pwm0.duty(red)
16 pwm1.duty(green)
17 pwm2.duty(blue)
18
19def wheel(pos):
20 global red,green,blue
21 WheelPos=pos%1023
22 print(WheelPos)
23 if WheelPos<341:
24 red=1023-WheelPos*3
25 green=WheelPos*3
26 blue=0
27
28 elif WheelPos>=341 and WheelPos<682:
29 WheelPos -= 341;
30 red=0
31 green=1023-WheelPos*3
32 blue=WheelPos*3
33 else :
34 WheelPos -= 682;
35 red=WheelPos*3
36 green=0
37 blue=1023-WheelPos*3
38
39try:
40 while True:
41 for i in range(0,1023):
42 wheel(i)
43 setColor()
44 time.sleep_ms(15)
45except:
46 pwm0.deinit()
47 pwm1.deinit()
48 pwm2.deinit()
The function wheel() is a color selection method of the color model introduced earlier. The value range of the parameter pos is 0-1023. The function will return a data containing the duty cycle values of 3 pins.
1def wheel(pos):
2 global red,green,blue
3 WheelPos=pos%1023
4 print(WheelPos)
5 if WheelPos<341:
6 red=1023-WheelPos*3
7 green=WheelPos*3
8 blue=0
9
10 elif WheelPos>=341 and WheelPos<682:
11 WheelPos -= 341;
12 red=0
13 green=1023-WheelPos*3
14 blue=WheelPos*3
15 else :
16 WheelPos -= 682;
17 red=WheelPos*3
18 green=0
19 blue=1023-WheelPos*3







