ESP32 MicroPython RGB LED

This guide shows you how to control an RGB LED using a ESP32 and MicroPython. We will cover:

ESP32 MicroPython RGB LED

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×RGB LED Module
1×(Alternative) RGB LED
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 RGB LED

The RGB LED can make any color by combining red, green, and blue, the three basic colors. It has three different LEDs - one red, one green, and one blue - all in a single unit.

Pinout

An RGB LED comes with four pins.

  • Attach the Common (Cathode-) pin to GND (0V).
  • The R (red) pin manages the red color.
  • The G (green) pin manages the green color.
  • The B (blue) pin manages the blue color.
RGB LED Pinout

To connect an RGB LED to a ESP32, it's better to use resistors that control the current, making the process complex. But, you can use the RGB LED module which already includes these resistors.

The RGB LED module also has four pins.

  • Connect the Common (Cathode-) pin to GND (0V).
  • The R (red) pin controls the red color.
  • The G (green) pin controls the green color.
  • The B (blue) pin controls the blue color.
RGB LED Module Pinout

※ NOTE THAT:

This guide shows how to use an RGB LED that has a common cathode. This means the cathode is the common pin. Some RGB LEDs might have the anode as the common pin.

How it works

In physics, a color consists of three values: Red (R), Green (G), and Blue (B). These values range from 0 to 255.

There are 16,777,216 colors made by mixing three different values.

By sending PWM signals (with a duty bit range from 0 to 255) to the R, G, and B pins, we can cause the RGB LED to show any color. The PWM duty cycle directed to the R, G, and B pins corresponds to the respective color values for Red (R), Green (G), and Blue (B).

Wiring Diagram

  • How to connect ESP32 and RGB LED using breadboard
The wiring diagram between ESP32 MicroPython RGB LED

This image is created using Fritzing. Click to enlarge image

Do not connect just one resistor to the common pin of an RGB LED. You should connect three different resistors, one to each of the other pins as shown in the diagram. The LEDs in the RGB package have different characteristics, so they do not use the same amount of current. This difference can make the LEDs shine with uneven brightness and might even harm them if only one resistor is used on the common pin.

The wiring diagram between ESP32 MicroPython RGB LED module

This image is created using Fritzing. Click to enlarge image

How To Control RGB LED

Let's learn how to change the RGB LED to a specific color, like #00979D, step by step.

  • Pick your preferred color and get its color code.
  • Change the color code to RGB format with this converter: RGB converter. Note these numbers: R = 0, G = 151, B = 157.
RGB LED color picker
  • Define the ESP32 pins that connect to the R (red), G (green), and B (blue) pins. For example:
PIN_RED = 23 # ESP32 pin GPIO23 connected to the LED's red pin PIN_GREEN = 22 # ESP32 pin GPIO22 connected to the LED's green pin PIN_BLUE = 21 # ESP32 pin GPIO21 connected to the LED's blue pin
  • Set these ESP32 pins to output mode.
red = PWM(Pin(PIN_RED)) green = PWM(Pin(PIN_GREEN)) blue = PWM(Pin(PIN_BLUE))
  • Set the PWM frequency to 1000 Hz
red.freq(1000) green.freq(1000) blue.freq(1000)
  • Set the PWM duty cycle
red.duty_u16(int(0 * 65535 / 255)) # RED: 0 green.duty_u16(int(151 * 65535 / 255)) # GREEN: 151 blue.duty_u16(int(157 * 65535 / 255)) # BLUE: 157

ESP32 - RGB LED Example Code

The code below alters the color of the LED in this sequence:

  • #00C9CC (Red = 0, Green = 201, Blue = 204)
  • #F7788A (Red = 247, Green = 120, Blue = 138)
  • #34A853 (Red = 52, Green = 168, Blue = 83)
""" 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-rgb-led """ from machine import Pin, PWM from time import sleep # Define the GPIO pins for the RGB LED PIN_RED = 23 # ESP32 pin GPIO23 connected to the LED's red pin PIN_GREEN = 22 # ESP32 pin GPIO22 connected to the LED's green pin PIN_BLUE = 21 # ESP32 pin GPIO21 connected to the LED's blue pin # Initialize the pins as PWM outputs red = PWM(Pin(PIN_RED)) green = PWM(Pin(PIN_GREEN)) blue = PWM(Pin(PIN_BLUE)) # Set the PWM frequency to 1000 Hz (you can adjust this as needed) red.freq(1000) green.freq(1000) blue.freq(1000) def setColor(r, g, b): # Set the duty cycle for each color channel red.duty_u16(int(r * 65535 / 255)) green.duty_u16(int(g * 65535 / 255)) blue.duty_u16(int(b * 65535 / 255)) while True: # Set RGB color to teal setColor(0, 201, 204) sleep(1) # Wait for 1 second # Set RGB color to soft red setColor(247, 120, 138) sleep(1) # Wait for 1 second # Set RGB color to green setColor(52, 168, 83) sleep(1) # Wait for 1 second

※ 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!