ESP32 MicroPython LED Module

This tutorial instructs you how to use the 10mm LED module with the ESP32 and MicroPython. In detail, we will learn:

ESP32 MicroPython and 10mm LED module

Hardware Preparation

1×38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
1×Alternatively, 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×LED Module
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for ESP32
1×Recommended: Breakout Expansion Board for ESP32
1×Recommended: Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
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 .

Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.

Overview of LED Module

A plain LED is a tiny part with two legs, and it is easy to get wrong: plug it in backwards and it stays dark, forget the resistor and it can burn out. The LED module takes these worries away. It is a 10mm LED that already sits on a small 20mm x 20mm board together with its resistor and a small transistor switch. You give it power, you give it a signal, and it lights up. The modules come in a pack of five colors: red, yellow, blue, green and white.

LED Module
LED 10mm, round, diffused
Board size 20mm x 20mm
Operating voltage 3.3V to 5V DC
Onboard parts 330Ω LED resistor, 1KΩ input resistor, transistor switch
Control logic active HIGH (HIGH = ON, LOW = OFF)
Brightness control yes, with PWM on the IN pin
Header 3 pins, 2.54mm, right-angle, already soldered

Pinout

LED Module Pinout
image source: diyables.io

The three pins are printed on the board in this order:

  • GND pin: connect to GND (0 volts) of the ESP32.
  • VCC pin: connect to VCC (3.3 volts or 5 volts). The LED takes its current from this pin.
  • IN pin: connect to a GPIO pin of the ESP32. This pin only tells the transistor to open or close, so it draws almost no current from the ESP32.

Why this matters for the ESP32: its GPIO pins give only 3.3 volts and a small current. With a bare LED, the brightness depends on what the pin can give. With the module, the GPIO pin only flips the switch, and the light comes from VCC. Write 1 to the pin and the LED turns on. Write 0 and it turns off. Send a PWM signal and the LED gets dimmer or brighter.

Moving from a bare LED? There are two things you can forget about: the series resistor (it is already on the board) and the long-leg / short-leg polarity (the pins are labeled instead).

Wiring Diagram

Only three wires are needed. GPIO18 is used here because it supports PWM, so the same wiring works for both scripts in this tutorial.

The wiring diagram between ESP32 MicroPython LED module

This image is created using Fritzing. Click to enlarge image

LED Module Pin ESP32 Pin
GND GND
VCC 3.3V (or VIN/5V when powered by USB, for a slightly brighter light)
IN GPIO18

Try It From The Thonny Shell First

MicroPython has one big plus over the Arduino IDE: you can talk to the ESP32 line by line, with no upload step. After you wire the module and open Thonny (the setup is in the Quick Steps below), click inside the Shell and type these lines one at a time:

from machine import Pin led_module = Pin(18, Pin.OUT) led_module.value(1) led_module.value(0)

The LED module turns on after the third line and off after the fourth. If this works, your wiring is correct, and you can move on to the full scripts.

ESP32 MicroPython Code - LED Module Fade

The second script swaps machine.Pin for machine.PWM. The ESP32 now turns GPIO18 on and off 1000 times every second. The share of time the pin stays HIGH sets how bright the LED looks. The helper function set_brightness() takes a percent from 0 to 100 and turns it into a duty_u16() value from 0 to 65535. The loop raises the brightness in 5% steps up to 100%, then lowers it back to 0%, which gives a soft "breathing" light.

""" 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-led-module """ from machine import Pin, PWM from time import sleep LED_MODULE_PIN = 18 # The ESP32 pin GPIO18 connected to the IN pin of the LED module # Create a PWM output on GPIO18 led_module = PWM(Pin(LED_MODULE_PIN)) led_module.freq(1000) # 1kHz PWM, far too fast for the eye to see any flicker def set_brightness(percent): # Convert 0-100% into the 0-65535 range used by duty_u16() led_module.duty_u16(percent * 65535 // 100) while True: # Fade in: brightness goes up from 0% to 100% for percent in range(0, 101, 5): set_brightness(percent) sleep(0.05) # Wait 50 milliseconds between steps print("LED module: fully bright") # Fade out: brightness goes down from 100% to 0% for percent in range(100, -1, -5): set_brightness(percent) sleep(0.05) print("LED module: dark")

Detailed Instructions

The wiring stays the same, so there is nothing to change on the breadboard.

  • Click the red Stop button in Thonny to stop the blink script.
  • Copy the fade code above and paste it into Thonny's editor, in place of the old code.
  • Save it to the MicroPython device as main.py again.
  • Click the green Run button (or press F5).
  • Watch the LED module glow up slowly, then fade out slowly, again and again.
  • The Shell prints one line at the top and one line at the bottom of each fade:
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MPY: soft reboot LED module: fully bright LED module: dark LED module: fully bright LED module: dark LED module: fully bright LED module: dark
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

To make the fade slower, raise sleep(0.05) to sleep(0.1). To make it smoother, change the step in range() from 5 to 1.

If you want to fade the LED in the background while your code does other jobs, see the ESP32 MicroPython Fade LED tutorial. It uses the DIYables LED library, and it works with this module too, since the IN pin acts just like the leg of a bare LED.

Project Ideas

With one or more LED modules on the ESP32, you can build:

  • A WiFi status light: on when the ESP32 joins your network, blinking while it tries to connect.
  • A "heartbeat" light that fades in and out to show your program is still running.
  • A fault lamp: a red module that flashes when a sensor reading goes out of range.
  • A traffic-light model with a red, a yellow and a green module, like in the ESP32 MicroPython Traffic Light tutorial.

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!