ESP32 MicroPython Blink LED
This tutorial instructs you how to control an LED with the ESP32 using MicroPython. It covers how to write MicroPython code to turn the LED on and off and make it blink.
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 | |
1 | × | (Recommended) Power Splitter 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
Pinout
LEDs have two legs.
- Attach the negative (-) pin to the ground (0V)
- The positive (+) pin manages the LED's status
How It Works
After connecting the negative side (cathode) to the ground (GND):
- If you connect the negative side (GND) to the long leg (+) of the LED, the LED won't turn on.
- If you connect the power source (VCC) to the long leg (+) of the LED, the LED will turn on.
You can also adjust the brightness of the LED by applying a PWM signal to the positive terminal (anode). How to change the LED light intensity using PWM is explained in detail in this tutorial.
※ NOTE THAT:
LEDs generally require a resistor. You can connect the resistor to either the positive side, also known as the anode, which links to the power source or VCC, or the negative side, called the cathode, which connects to the ground or GND. The needed resistor value varies based on the LED's requirements. Some LEDs come with a built-in resistor. If your LED includes one, you likely don't need an additional resistor.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
How To Program
- Configure ESP32 pin GPIO18 to a digital output.
- Turn off the LED.
- Turn on the LED.
ESP32 MicroPython Code
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 LED to the ESP32 as shown in the 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).
- 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.
- Look at the LED to see if it's working.
Code Explanation
You can find the explanation in the comments section of the ESP32 MicroPython code mentioned above.
※ NOTE THAT:
The code you're looking at uses sleep(), which prevents the ESP32 from doing other things at the same time. If your project has to manage several tasks, avoid using this function because it stops the ESP32. Instead, think about using a method that keeps the ESP32 active.