Chapter 3 LED Bar
We have learned how to control a LED blinking, next we will learn how to control a number of LEDs.
Project 3.1 Flowing Light
In this project, we use a number of LEDs to make a flowing light.
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
|
Breadboard x1
|
||
Jumper M/M x10 |
Resistor 220Ω x10
|
LED bar graph x1
|
Component knowledge
Let’s learn about the basic features of these components to use and understand them better.
LED bar
A LED bar graph has 10 LEDs integrated into one compact component. The two rows of pins at its bottom are paired to identify each LED like the single LED used earlier.
Circuit
Schematic diagram |
|
Hardware connection |
If you need any support, please feel free to contact us via: support@freenove.com
If LED bar does not work, try to rotate it for 180°. The label is random. |
Sketch
This project is designed to make a flowing water lamp. Which are these actions: First turn LED #1 ON, then turn it OFF. Then turn LED #2 ON, and then turn it OFF… and repeat the same to all 10 LEDs until the last LED is turns OFF. This process is repeated to achieve the “movements” of flowing water.
Upload following sketch:
Freenove_Ultimate_Starter_Kit_for_ESP32\Sketches\Sketch_03.1_FlowingLight.
Sketch_03.1_FlowingLight
Download the code to ESP32-WROVER and LED bar graph will light up from left to right and from right to left.
If you have any concerns, please contact us via: support@freenove.com
The following is the program code:
1/**********************************************************************
2 Filename : FlowingLight
3 Description : Using ledbar to demonstrate flowing lamp.
4 Auther : www.freenove.com
5 Modification: 2024/06/18
6**********************************************************************/
7byte ledPins[] = {15, 2, 0, 4, 5, 18, 19, 21, 22, 23};
8int ledCounts;
9
10void setup() {
11 ledCounts = sizeof(ledPins);
12 for (int i = 0; i < ledCounts; i++) {
13 pinMode(ledPins[i], OUTPUT);
14 }
15}
16
17void loop() {
18 for (int i = 0; i < ledCounts; i++) {
19 digitalWrite(ledPins[i], HIGH);
20 delay(100);
21 digitalWrite(ledPins[i], LOW);
22 }
23 for (int i = ledCounts - 1; i > -1; i--) {
24 digitalWrite(ledPins[i], HIGH);
25 delay(100);
26 digitalWrite(ledPins[i], LOW);
27 }
28}
Use an array to define 10 GPIO ports connected to LED bar graph for easier operation.
1byte ledPins[] = {15, 2, 0, 4, 5, 18, 19, 21, 22, 23};
In setup(), use sizeof() to get the number of array, which is the number of LEDs, then configure the GPIO port to output mode.
1ledCounts = sizeof(ledPins);
2for (int i = 0; i < ledCounts; i++) {
3 pinMode(ledPins[i], OUTPUT);
4}
Then, in loop(), use two “for” loop to realize flowing water light from left to right and from right to left.
1for (int i = 0; i < ledCounts; i++) {
2 digitalWrite(ledPins[i], HIGH);
3 delay(100);
4 digitalWrite(ledPins[i], LOW);
5}
6for (int i = ledCounts - 1; i > -1; i--) {
7 digitalWrite(ledPins[i], HIGH);
8 delay(100);
9 digitalWrite(ledPins[i], LOW);
10}







