RobotLabs
  • Welcome
  • ESP32
    • PlatformIO
      • 1.1 Arduino Basics
      • 1.2 Install PlatformIO
      • 2.1 GPIO - LED Blink
      • 2.2 GPIO - LED Multi
      • 2.3 GPIO - LED Button
      • 3.1 PWM - LED Fade
      • 3.2 ADC - Analog Input
      • 4.1 IIC - OLED
    • MicroPython
    • ESP32 Projects
  • ROS
    • ROS2 Jazzy
      • 1.1 Install ROS 2 Jazzy
      • 1.2 Install VS Code
      • 1.3 Save ROS code on GitHub
      • 2.1 Create a Mobile Robot with URDF and Visualize it
      • 2.3 Create Launch Files to Display the URDF in RViz
      • 2.4 Simulation with Gazebo
      • 2.5 Gazebo sensors
      • 2.6 Sensor fusion and SLAM
    • MoveIt2
      • Moveit 2 joint position control with keyboard
      • Moveit2 joint velocity control for Panda robot arm
    • Perception
      • PCL-ROS
  • FOC
    • SimpleFOC
      • Install simpleFOC Lib
  • Template
    • Markdown
    • Interactive blocks
    • OpenAPI
    • Integrations
Powered by GitBook
On this page
  • Hardware Circuit
  • 1. Parts Required
  • 2. Wiring
  • Coding and Testing
  • 1. Flowing LED
  • 2. Reciprocating flowing LED
  • 3. Moving LED
  • Summary
  1. ESP32
  2. PlatformIO

2.2 GPIO - LED Multi

Previous2.1 GPIO - LED BlinkNext2.3 GPIO - LED Button

Last updated 3 months ago

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

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.

Make sure you have it installed PlatformIO extension in your VScode. ()

If you have any questions for setting up the IDE or uploading the code, please check the previous tutorial:

Install PlatformIO procedure
2.1 GPIO - LED Blink