In this tutorial, we will show you how to blink an LED using the ESP32-S3 and MicroPython. The ESP32-S3 is a powerful microcontroller, and MicroPython is a lightweight version of Python that runs on microcontrollers, making it easy to program hardware projects quickly.
In this project, we will use a simple LED blink program, where the LED connected to a GPIO pin will blink on and off at regular intervals. This is a great starting point for anyone new to ESP32-S3 and MicroPython.
What You Will Need
-
ESP32-S3 development board
-
LED (a standard 5mm LED will work fine)
-
330Ω resistor (current-limiting resistor for the LED)
-
Breadboard and jumper wires
-
MicroPython installed on the ESP32-S3
-
Thonny IDE or WebREPL (for uploading the code)
Circuit Diagram
Before writing the code, let's wire the circuit:
-
Connect the Anode (long leg) of the LED to GPIO2 (D2) on the ESP32-S3.
-
Connect the Cathode (short leg) of the LED to the 330Ω resistor, and the other end of the resistor goes to GND on the ESP32-S3.
This simple circuit ensures that the LED receives safe current levels, protecting both the ESP32-S3 and the LED.
Writing the Code
We will now write the MicroPython code to make the LED blink on and off at a 100ms interval.
from machine import Pin
from time import sleep
led = Pin(2, Pin.OUT) # D2 corresponds to GPIO2
while True:
led.on()
sleep(0.3) # 100 milliseconds
led.off()
sleep(0.3)
Code Explanation
-
Pin(2, Pin.OUT)
: This line initializes GPIO2 (D2) as an output pin, where we will connect the LED. ThePin.OUT
mode sets it to output voltage, controlling the LED. -
led.on()
: Turns the LED ON by applying 3.3V to the GPIO pin. -
sleep(0.1)
: Thesleep()
function pauses the program for 100 milliseconds. This delay controls how fast the LED blinks. A 100ms delay means the LED will be ON for 100ms, then OFF for another 100ms. -
led.off()
: Turns the LED OFF by setting the GPIO pin to LOW (0V).
The program will repeat indefinitely, causing the LED to blink on and off.
Uploading the Code
Option 1: Using Thonny IDE
-
Connect the ESP32-S3 to your computer via USB.
-
Open Thonny IDE, select ESP32 as the target device.
-
Write or paste the above MicroPython code into the Thonny editor.
-
Click the Run button to upload and execute the code. You should see the LED blink every 100ms.
Option 2: Using WebREPL
-
Enable WebREPL on your ESP32-S3.
-
Open the WebREPL interface in your browser.
-
Connect to the ESP32-S3 over Wi-Fi.
-
Paste the code into the WebREPL console and press Enter.
-
Your LED will start blinking!
Troubleshooting
If the LED does not blink as expected, here are a few things to check:
-
LED Orientation: Make sure the long leg (Anode) of the LED is connected to GPIO2 (D2), and the short leg (Cathode) goes to ground through the resistor.
-
Resistor: Ensure you're using the 330Ω resistor in series with the LED to limit the current and protect both the LED and the ESP32-S3.
-
MicroPython Installation: Ensure that MicroPython is properly installed on your ESP32-S3. You can follow the official MicroPython installation guide for ESP32.
-
GPIO Pin: Double-check that you're using the correct GPIO pin (GPIO2) for the LED. Some ESP32 boards might have different pinout configurations.
Conclusion
In this tutorial, you learned how to blink an LED using ESP32-S3 and MicroPython. You wrote a simple script to control the LED and used either Thonny IDE or WebREPL to upload the code to your ESP32-S3. Blinking LEDs is a great beginner project to get familiar with hardware programming using MicroPython.
Feel free to modify the code and experiment with different GPIO pins or blink rates to customize the behavior of your LED.
Happy coding, and enjoy working with your ESP32-S3!
Related Articles:
-
LED Control with ESP32 using Arduino IDE
Arduino LED blinking using Matlab
VS Code PlatformIO IDE Arduino LED blink Tutorial
Getting Started with NodeMCU and Johnny-Five: LED Blink