The ESP32 is a powerful microcontroller widely used in electronics projects. One of the most common beginner projects is controlling an LED with ESP32. This simple task demonstrates how to use GPIO pins, introduces MicroPython scripting, and lays the groundwork for more advanced experiments.
This tutorial covers everything you need to get started: setting up your ESP32, writing a script to control an LED, and running the program to see your LED light up and blink. Let’s dive into the details and master controlling an LED with ESP32!
Table of Contents
Setting Up the Hardware
To begin, you’ll need the following:
- ESP32 microcontroller
- LED (any color, with a 220Ω resistor)
- Breadboard
- Jumper wires
Here’s how to connect your LED with ESP32:
- Connect the longer leg (anode) of the LED to GPIO21 on the ESP32.
- Connect the shorter leg (cathode) to GND through a 220Ω resistor.
- Power your ESP32 via USB or an external source.
Writing the MicroPython Script
Before anything else, it is important to get this module out of the way. The machine
module in MicroPython is essential for controlling hardware components on the ESP32, such as an LED. Using its Pin
class, you can configure GPIO pins as inputs or outputs, enabling direct control of the LED’s state (on or off). For example, Pin(2, Pin.OUT)
sets GPIO2 as an output pin, allowing it to send electrical signals to the LED. The module also supports advanced hardware interactions, like timers and PWM, which can be used to create more dynamic LED effects, such as blinking or dimming.
Use the following MicroPython script to control the LED with ESP32:
Option 1:
from machine import Pin
from time import sleep
# Configure GPIO2 as an output pin
led = Pin(21, Pin.OUT)
while True:
led.value(1) # Turn LED on
print("LED is ON")
sleep(1) # Wait 1 second
led.value(0) # Turn LED off
print("LED is OFF")
sleep(1) # Wait 1 second
Option 2:
from machine import Pin
from time import sleep
# Configure GPIO2 as an output pin
led = Pin(21, Pin.OUT)
while True:
led.on() # Turn LED on
print("LED is ON")
sleep(1) # Wait 1 second
led.off() # Turn LED off
print("LED is OFF")
sleep(1) # Wait 1 second
Save this code as main.py
and upload it to your ESP32 using an IDE like Thonny or uPyCraft.
Using the Timer Class for LED Control
To enhance your control over the LED with ESP32, you can use the Timer
class. This approach allows you to schedule tasks at regular intervals without relying on the while
loop or sleep()
, making your program more efficient.
Here’s an example script using the Timer
class:
from machine import Pin, Timer
# Configure GPIO2 as an output pin
led = Pin(21, Pin.OUT)
# Define a function to toggle the LED state
def toggle_led(timer):
led.value(not led.value())
print(f"LED is {'ON' if led.value() else 'OFF'}")
# Create a Timer instance
timer = Timer(0)
# Set the timer to call toggle_led every 1 second
timer.init(period=1000, mode=Timer.PERIODIC, callback=toggle_led)
Explanation:
- The
Timer
instance schedules thetoggle_led
function to run every 1,000 milliseconds (1 second). - The
toggle_led
function switches the LED state and prints its status. - This approach frees the main loop, allowing you to perform other tasks concurrently.
Testing the Script
- Power up your ESP32 and upload the script.
- Observe the LED: it will toggle on and off every second, controlled by the
Timer
class. - Monitor the serial console for the LED’s status updates.
Enhancing the Project
Once you’ve mastered controlling an LED with ESP32, consider extending your project:
- Control multiple LEDs with different timers.
- Timers in ESP32: Why Using Sleep Inside Timers Can Cause Issues
Conclusion
By completing this project, you’ve learned the basics of controlling an LED with ESP32 using both traditional loops and the Timer
class. These approaches provide flexibility for various applications, from simple blinking LEDs to advanced multitasking projects.
Continue exploring the capabilities of the ESP32 to build innovative and exciting projects. For more tutorials, check out other guides on ESP32 development and MicroPython scripting.