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

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

Jumper M/M x10

Chapter01_05

Resistor 220Ω x10

Chapter01_04

LED bar graph x1

Chapter03_00

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.

../../../_images/Chapter03_01.png

Circuit

Schematic diagram

Chapter03_02

Hardware connection

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

Chapter03_03

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

../../../_images/Chapter03_04.png

Download the code to ESP32-WROVER and LED bar graph will light up from left to right and from right to left.

../../../_images/Chapter03_05.png

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}