ESP32 MicroPython Fade LED

This tutorial instructs you how to fade LED using ESP32 and MicroPython. In detail, we will learn:

ESP32 MicroPython Fade 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

LED has two metal pins.

  • Negative (-) pin: Connect it to ground (0V).
  • Positive (+) pin: Use it to control the LED's state.
LED Pinout

How It Works

After connecting the negative side, also called the cathode, to the ground (GND):

  • If you connect the ground (GND) to the positive side of the LED, the LED will turn off.
  • If you connect the power supply (VCC) to the positive side of the LED, the LED will light up.
  • By using a Pulse Width Modulation (PWM) signal on the positive side of the LED, you can change how bright it is. The PWM value can be between 0 and 255. The LED shines brighter with a higher PWM value and less brightly with a lower value.
  • If the PWM value is 0, the LED switches off, like connecting it to GND.

If the PWM value is 255, the LED is completely on, just like it's connected directly to VCC.

How LED Works

※ NOTE THAT:

For most LEDs, you need to connect a resistor from the positive side (anode) to the power supply. The resistor's value depends on the LED's requirements.

ESP32 - fade LED

To adjust the brightness of an LED with the ESP32, use pins that produce a PWM signal. First, connect the LED's positive pin (+) to a pin on the ESP32. Then, link the LED's negative pin (-) to the ground (GND). After that, set up the chosen pin on the ESP32 to send out a PWM signal.

Wiring Diagram

The wiring diagram between ESP32 MicroPython LED

This image is created using Fritzing. Click to enlarge image

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-fade-led """ from machine import Pin, PWM from time import sleep # Define pin LED_PIN = 18 # The ESP32 pin GPIO18 connected to the LED # Initialize PWM on the LED pin led = PWM(Pin(LED_PIN)) led.freq(1000) # Set the PWM frequency to 1kHz # Variables to control the LED brightness brightness = 0 # How bright the LED is fade_step = 5 # How many points to fade the LED by # Main loop while True: # Set the brightness of the LED led.duty_u16(int(brightness * 4095 / 255)) # Convert 0-255 range to 0-4095 # Change the brightness for next time through the loop brightness = brightness + fade_step # Reverse the direction of the fading at the ends of the fade if brightness <= 0 or brightness >= 255: fade_step = -fade_step # Wait for 30 milliseconds to see the dimming effect sleep(0.03)

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).
  • 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.
  • Observe the LED to see how it reacts.

Code Explanation

The explanation is in the comments at the top of the ESP32 MicroPython code.

※ NOTE THAT:

In the example, we used a function called sleep() to slowly adjust the light's brightness. However, this function makes the brightness change less smoothly and stops other parts of the program. Next, we will learn how to smoothly fade the light while allowing the rest of the program to continue using the LED library from DIYables.

How to fade in/out LED

With the LED library made by DIYables, making the LED fade in/out is very easy. You can adjust the brightness level and the fading speed.

""" 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-fade-led """ from DIYables_MicroPython_LED import LED, CTRL_ANODE, LED_IDLE led = LED(18, CTRL_ANODE) # create an LED object that attaches to pin GPIO18 is_faded_in = False while True: led.loop() # MUST call the led.loop() function in loop() if led.get_state() == LED_IDLE: if not is_faded_in: print("FADING IN") led.fade(0, 255, 3000) # fade in from 0 to 255 in 3000ms, fade immediately # led.fade(0, 255, 3000, 1000) # fade in from 0 to 255 in 3000ms, fade after 1 second is_faded_in = True else: print("FADING OUT") led.fade(255, 0, 3000) # fade out from 255 to 0 in 3000ms, fade immediately # led.fade(255, 0, 3000, 1000) # fade out from 255 to 0 in 3000ms, fade after 1 second is_faded_in = False

Detailed Instructions

  • In Thonny IDE, Go to Tools Manage packages.
  • 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 LED's state.

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!