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.

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

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

Pinout

LEDs have two legs.

  • Attach the negative (-) pin to the ground (0V)
  • The positive (+) pin manages the LED's status
LED Pinout

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.
How LED Works

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

The wiring diagram between ESP32 MicroPython LED

This image is created using Fritzing. Click to enlarge image

How To Program

  • Configure ESP32 pin GPIO18 to a digital output.
led = Pin(18, Pin.OUT)
  • Turn off the LED.
led.value(0) # Turn the LED off
  • Turn on the LED.
led.value(1) # Turn the LED on

ESP32 MicroPython Code

""" 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 """ from machine import Pin from time import sleep led = Pin(18, Pin.OUT) while True: led.value(1) # Turn the LED on sleep(1) # Wait for 1 second led.value(0) # Turn the LED off sleep(1) # Wait for 1 second

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.

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!