Writing Your First MicroPython Script: LED with ESP32

Watch as the LED connected to the ESP32 flashes in sync with the program, demonstrating the successful control of the circuit using MicroPython.

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:

  1. ESP32 microcontroller
  2. LED (any color, with a 220Ω resistor)
  3. Breadboard
  4. 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.
Diagram showing the longer leg (anode) of an LED connected to GPIO21 on the ESP32, illustrating the correct orientation for current flow and LED control.
Connect the longer leg (anode) of the LED to GPIO21 on the ESP32, ensuring proper orientation for current flow. This setup allows the microcontroller to control the LED’s behavior, such as turning it on, off, or blinking.

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.

Photo of a circuit with an LED connected to GPIO21 on the ESP32, illuminated to indicate it is turned on.
The LED connected to GPIO21 on the ESP32 glows brightly, demonstrating successful control using MicroPython.

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:

  1. The Timer instance schedules the toggle_led function to run every 1,000 milliseconds (1 second).
  2. The toggle_led function switches the LED state and prints its status.
  3. This approach frees the main loop, allowing you to perform other tasks concurrently.

Testing the Script

  1. Power up your ESP32 and upload the script.
  2. Observe the LED: it will toggle on and off every second, controlled by the Timer class.
  3. 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:


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.

Leave a Reply

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