ESP32 MicroPython Blink LED Without Sleep
This totorial instructs you how to write MicroPython code for ESP32 to blink LED without blocking other tasks. In detail, we will learn:
- ESP32 blinks LED using the non-blocking method
- ESP32 blinks LED in a period of time
- ESP32 blinks LED with number of times.
Hardware Preparation
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | USB Cable Type-C | |
1 | × | LED | |
1 | × | 220 ohm resistor | |
1 | × | Breadboard | |
1 | × | Jumper Wires | |
1 | × | (Recommended) Screw Terminal Expansion Board for ESP32 |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of LED
If you're not yet familiar with LEDs (including their pinout, how it works, interfacing with the ESP32, and writing MicroPython code for the ESP32 to interact with them), you can learn more at:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
ESP32 MicroPython Code - Blinking LED without sleep
This MicroPython code makes an LED blink quickly, turning it on for 250 milliseconds and off for 750 milliseconds, repeating this pattern continuously without stopping other programs.
Detailed Instructions
Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:
- Make sure Thonny IDE is installed on your computer.
- Confirm that MicroPython firmware is loaded on your ESP32 board.
- If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
- Connect the ESP32 board to the LED according to the provided diagram.
- Connect the ESP32 board to your computer with a USB cable.
- Open Thonny IDE on your computer.
- In Thonny IDE, go to Tools Options.
- Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
- Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
- Go to Tools Manage packages in Thonny IDE.
- Search for “DIYables-MicroPython-LED” and find the LED library by DIYables.
- Click on DIYables-MicroPython-LED and then click the Install button to install the LED library on the ESP32 board.
- Copy the provided MicroPython code and paste it into Thonny's editor.
- Save the code to your ESP32 by:
- Clicking the Save button or pressing Ctrl+S.
- In the save dialog, choose MicroPython device.
- Name the file main.py.
- Click the green Run button (or press F5) to execute the script.
- Check out the message in the Shell at the bottom of Thonny IDE to see the output.
- Observe the LED: The LED will continue to blink indefinitely.
ESP32 MicroPython Code - Blink LED in a Period of Time
This code makes a LED flash every 10 seconds, then stops, without interrupting other code.
Detailed Instructions
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your ESP32.
- Click the green Run button (or press F5) to run the script. The script will execute.
- Observe the LED: It will flash for 10 seconds and then turn off.
ESP32 MicroPython Code - Blink LED with a Number of Time
This code makes a LED flash on for 250 milliseconds and off for 750 milliseconds. It repeats this pattern 10 times and does not stop other code from running.
Detailed Instructions
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your ESP32.
- Click the green Run button (or press F5) to run the script. The script will execute.
- Observe the LED: it will blink for 10 times, then it will stop.