2.2 GPIO - LED Multi
In last tutorial, we successfully light a led with PlatformIO IDE. So this time let’s expand this example to multiple LEDs and do some further programming.
Hardware Circuit
1. Parts Required
Here’s a list of the parts to you need to build the circuit:
ESP32-S3-N16R8 x 1
5 mm LED x 5
330 - 1000 Ohm resistor
Breadboard
Jumper wires
2. Wiring
At this example, we control five LEDs here is the diagram. The positive pole of each LED is connected to a GPIO pin of the development board and a resistor is connected in series, and the negative pole is connected to GND.

While we can use only one resistor to realize this. Following is the diagram, and you can use each one of them and the code would be the same.

Coding and Testing
Make sure you have it installed PlatformIO extension in your VScode. (Install PlatformIO procedure)
1. Flowing LED
As we are going to use multiple GPIO pins, it is best to put all the GPIO pins in an array and then iterate through the array.
// *** This ESP32 code is created by RobotLabs ***
#include <Arduino.h>
// define array for GPIO pins
int pin_list[5] = {13, 12, 11, 10, 9};
// get the length of the array
int size = sizeof(pin_list) / sizeof(pin_list[0]);
void setup() {
// set GPIO pin mode to output
for (int i=0; i<size;i++) {
pinMode(pin_list[i], OUTPUT);
}
}
void loop() {
// Set all pins to high
for (int i=0;i<size;i++) {
digitalWrite(pin_list[i], HIGH);
delay(50);
}
// Set all pins to low
for (int i=0;i<size;i++) {
digitalWrite(pin_list[i], LOW);
delay(50);
}
}
2. Reciprocating flowing LED
We can also update the program. For example, before, the status of the running lights was changed sequentially. Now, we can change it to make the lights running back and forth.
// *** This ESP32 code is created by RobotLabs ***
#include <Arduino.h>
// define array for GPIO pins
int pin_list[5] = {13, 12, 11, 10, 9};
// get the length of the array
int size = sizeof(pin_list) / sizeof(pin_list[0]);
void setup() {
// set GPIO pin mode to output
for (int i=0; i<size;i++) {
pinMode(pin_list[i], OUTPUT);
}
}
void loop() {
// Set all pins to high
for (int i=0;i<size;i++) {
digitalWrite(pin_list[i], HIGH);
delay(50);
}
// Set all pins to low
for (int i=size-1;i>=0;i--) {
digitalWrite(pin_list[i], LOW);
delay(50);
}
}
3. Moving LED
The effect of making the LED shift is achieved like this. Every time I light up this LED, the state of the previous LED is changed to a low level at the same time, and when the index value is 0, the state of the last LED is changed to a low level.
// *** This ESP32 code is created by RobotLabs ***
#include <Arduino.h>
// define array for GPIO pins
int pin_list[5] = {13, 12, 11, 10, 9};
// get the length of the array
int size = sizeof(pin_list) / sizeof(pin_list[0]);
void setup() {
// set GPIO pin mode to output
for (int i=0; i<size;i++) {
pinMode(pin_list[i], OUTPUT);
}
}
void loop() {
// Set all pins to high
for (int i=0;i<size;i++) {
digitalWrite(pin_list[i], HIGH);
if (i > 0){
digitalWrite(pin_list[i-1], LOW);
}else {
digitalWrite(pin_list[size-1], LOW);
}
delay(250);
}
}
Summary
At this example, we use for loop to realize different effects of LEDs.
If you have any questions for setting up the IDE or uploading the code, please check the previous tutorial: 2.1 GPIO - LED Blink
Last updated