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 MicroPython Blink LED

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)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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

The wiring diagram between ESP32 MicroPython LED

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.

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-blink-led-without-sleep """ from DIYables_MicroPython_LED import LED, CTRL_ANODE # Create an LED object that attaches to pin 18 led = LED(18, CTRL_ANODE) led.blink(250, 750) # 250ms ON, 750ms OFF, blink immediately # led.blink(250, 750, 1000) # Uncomment to blink after 1 second delay # Main loop while True: led.loop() # MUST call the led.loop() function in loop # DO OTHER TASKS HERE # To stop blinking immediately, call led.cancel()

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.
ESP32 MicroPython LED library
  • 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.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!