ESP32 Multiple LEDs: Simple On/Off Example with MicroPython

In this tutorial, we’ll explore how to control ESP32 multiple LEDs using MicroPython. Specifically, we’ll walk you through a simple example where we toggle LEDs connected to pins 18, 19, and 21. By the end of this guide, you’ll have a solid understanding of how to control ESP32 multiple LEDs in basic patterns, which can be expanded for more advanced applications.

The ESP32 is an excellent choice for controlling multiple hardware components like LEDs, and with MicroPython, you can easily manipulate these components with just a few lines of code.


Table of Contents


Wiring the LEDs

Diagram showing the wiring of an ESP32 controlling three LEDs through GPIO pins 18, 19, and 21.

To control ESP32 multiple LEDs, connect the following components:

  • LED 1 (Red): Connect the anode (positive leg) to pin 21 and the cathode (negative leg) to GND.
  • LED 2 (Green): Connect the anode to pin 19 and the cathode to GND.
  • LED 3 (Yellow): Connect the anode to pin 18 and the cathode to GND.

Add a 220Ω resistor between the anode of each LED and the respective pin to ensure safe current levels.


Basic LED Control (Patterns)

Let’s start with controlling ESP32 multiple LEDs in basic on/off patterns:

from machine import Pin
from time import sleep

# PINS
led1 = Pin(21, Pin.OUT)  # RED
led2 = Pin(19, Pin.OUT)  # GREEN
led3 = Pin(18, Pin.OUT)  # YELLOW

# PATTERN 0
led1.off()
led2.off()
led3.off()
sleep(1)

# PATTERN 1
led1.on()
led2.on()
led3.on()
sleep(1)

# PATTERN 2
led1.on()
led2.on()
led3.off()
sleep(1)

# PATTERN 3
led1.on()
led2.off()
led3.off()
sleep(1)

# PATTERN 4
led1.off()
led2.off()
led3.off()
sleep(1)

Expanding to a While Loop

Now, we can make ESP32 multiple LEDs toggle continuously in a repeating pattern using a while loop:

from machine import Pin
from time import sleep

# PINS
led1 = Pin(21, Pin.OUT)  # RED
led2 = Pin(19, Pin.OUT)  # GREEN
led3 = Pin(18, Pin.OUT)  # YELLOW

while True:
    # PATTERN 0
    led1.off()
    led2.off()
    led3.off()
    sleep(1)

    # PATTERN 1
    led1.on()
    led2.on()
    led3.on()
    sleep(1)

    # PATTERN 2
    led1.on()
    led2.on()
    led3.off()
    sleep(1)

    # PATTERN 3
    led1.on()
    led2.off()
    led3.off()
    sleep(1)

    # PATTERN 4
    led1.off()
    led2.off()
    led3.off()
    sleep(1)

Expanding to a Function with Inputs for Each LED

We can further optimize the code by turning it into a reusable function. This function will take three inputs to control each LED individually, making it more flexible for controlling ESP32 multiple LEDs in different configurations.

from machine import Pin
from time import sleep

# Function to control LEDs
def control_leds(led1_state, led2_state, led3_state):
    led1.value(led1_state)
    led2.value(led2_state)
    led3.value(led3_state)

# PINS
led1 = Pin(21, Pin.OUT)  # RED
led2 = Pin(19, Pin.OUT)  # GREEN
led3 = Pin(18, Pin.OUT)  # YELLOW

while True:
    # PATTERN 0
    control_leds(0, 0, 0)  # All LEDs off
    sleep(1)

    # PATTERN 1
    control_leds(1, 1, 1)  # All LEDs on
    sleep(1)

    # PATTERN 2
    control_leds(1, 1, 0)  # Red and Green on, Yellow off
    sleep(1)

    # PATTERN 3
    control_leds(1, 0, 0)  # Red on, Green and Yellow off
    sleep(1)

    # PATTERN 4
    control_leds(0, 0, 0)  # All LEDs off
    sleep(1)

Explanation of the Code

  1. Function Definition:
    • The control_leds() function allows you to control the state (on/off) of each LED dynamically by passing 1 (on) or 0 (off).
  2. Pin Setup:
    • Pins 18, 19, and 21 are configured as output pins for the LEDs.
  3. LED Control:
    • The function is called with different combinations of on/off states to create patterns.
  4. Looping:
    • The while True: loop ensures that the patterns repeat continuously, providing ongoing feedback through the LEDs.

Conclusion

In this guide, we demonstrated how to control ESP32 multiple LEDs using MicroPython. From basic on/off patterns to using a function for more flexibility, you can now toggle multiple LEDs on your ESP32 with ease. This skill is the foundation for building more complex projects that involve LED feedback and dynamic control, making the ESP32 a powerful tool for DIY electronics and automation systems.

Leave a Reply

Your email address will not be published. Required fields are marked *