# 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.

{% embed url="<https://youtu.be/jcvla5WDN5Q>" %}

## 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.

<figure><img src="/files/0quKupzFbCFwSQkuicrL" alt="" width="375"><figcaption></figcaption></figure>

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.

<figure><img src="/files/clwUHDdSZ0Pr9urdEYGb" alt="" width="375"><figcaption></figcaption></figure>

## Coding and Testing

Make sure you have it installed PlatformIO extension in your VScode. ([Install PlatformIO procedure](/docs/esp32/platformio/1.2-install-platformio.md))

### 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.

```c
// *** 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.

```cpp
// *** 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.

```cpp
// *** 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.&#x20;

If you have any questions for setting up the IDE or uploading the code, please check the previous tutorial: [2.1 GPIO - LED Blink](/docs/esp32/platformio/2.1-gpio-led-blink.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://robotlabs.gitbook.io/docs/esp32/platformio/2.2-gpio-led-multi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
