0-ESP32-MicroPython ESP32 ESP8266 MicroPython Project Tech

MicroPython: WS2812B Addressable RGB LEDs with ESP32 and ESP8266

ws2812b addressable rgb led micropython esp32 esp8266

This tutorial exhibits how one can management WS2812B addressable RGB LEDs (neopixels) with the ESP32 and ESP8266 utilizing MicroPython.

There’s a built-in library in MicroPython that makes it extraordinarily straightforward to regulate these LEDs: the neopixel library. We’ll present you find out how to management particular person LEDs, create features to supply superior lighting results, and construct a easy venture as an example how every part works.

This tutorial might be utilized to any strip or PCB board that has WS2812B addressable RGB LEDs (neopixels) like:

On this tutorial we’ll management two addressable RGB LED rings, and one addressable LED stick wired in collection.

WS2812B addressable RGB LEDs Strips, rings, PCB sticks


To comply with this tutorial it’s essential have MicroPython firmware put in in your ESP32 or ESP8266. You additionally want an IDE to put in writing and add the code to your board. We advise utilizing Thonny IDE or uPyCraft IDE:

Introducing WS2812B RGB LEDs

The WS2812B LEDs are addressable RGB LEDs that may be related in collection, and be managed individually utilizing only one digital pin of a microcontroller. These LEDs have an IC constructed proper into the LED that make all of this attainable.

addressable ws2812 rgb led neopixel

You possibly can solder a number of LED rings and sticks and they’ll behave as one piece. Every PCB has IN and OUT pins that make wiring quite simple:

The next determine exhibits how our setup seems like after soldering the LEDs.

ws2812b neopixel wired in series

To wire the RGB LED strip to the ESP32 or ESP8266 could be very easy. You’ll want to apply 5V to the VCC pin, GND to GND and join a GPIO to the Din (knowledge) pin. We’ll join the info pin to GPIO 5.

Controlling WS2812B RGB LEDs

There’s a built-in MicroPython module referred to as neopixel to regulate WS2812B addressable LEDs. For instance, the subsequent script controls four particular person pixels:

Importing libraries

First, import the neopixel and machine modules:

import machine, neopixel

Create a neopixel object

Set the variety of pixels in your strip to the n variable:

n = 48

Save the GPIO quantity that may management the strip on the p variable:

p = 5

Create a NeoPixel object referred to as np on the GPIO you’ve outlined earlier and with the variety of LEDs you’ve additionally outlined:

np = neopixel.NeoPixel(machine.Pin(p), n)

Controlling particular person pixels

After initializing the neopixel object, you can begin controlling the LEDs. Controlling a person pixel could be very straightforward. You possibly can consider the strip as an array with n parts (variety of pixels on this pattern strip). Then, we simply have to set a colour to a selected component. For instance, to set the primary pixel to pink:

np[0] = (255, zero, zero)

The next determine might provide help to higher perceive the way it works:

ws2812b control individual pixels micropython

Then, use the write() technique for the modifications to take impact.


ws2812b control individual pixels micropython

WS2812 RGB LEDs Lighting Results

Now that you understand how to regulate particular person pixels, you can also make your personal lighting results. We’ll present some features (based mostly on the library examples) that you should use in your personal tasks.

Clear all pixels

Clearing all pixels is identical as setting all pixels to (zero, zero, zero) colour.

def clear():
for i in vary(n):
np[i] = (zero, zero, zero)

Set all pixels to the identical colour

In an identical approach, to set all of the pixels to the identical shade, you need to use the next perform that accepts as arguments, the r, g, and b colour parameters .

def set_color(r, g, b):
for i in vary(n):
np[i] = (r, g, b)

Bounce impact

The bounce() perform creates a bounce impact and accepts the r, g and b parameters to set the colour, and the ready time. The ready time determines how briskly the bouncing impact is .

def bounce(r, g, b, wait):
for i in vary(four * n):
for j in vary(n):
np[j] = (r, g, b)
if (i // n) % 2 == zero:
np[i % n] = (zero, zero, zero)
np[n – 1 – (i % n)] = (zero, zero, zero)

This impact exhibits an off pixel that runs by means of all of the strip positions.

Cycle impact

The cycle impact works equally to the bounce impact. There’s a pixel on that runs by way of all of the strip positions whereas the opposite pixels are off.

def cycle(r, g, b, wait):
for i in vary(four * n):
for j in vary(n):
np[j] = (zero, zero, zero)
np[i % n] = (r, g, b)

Shifting raibow impact

To supply a shifting rainbow impact, you want two features. The wheel() perform generates the rainbow shade spectrum by various every colour parameter between zero and 255.

def wheel(pos):
Enter a worth zero to 255 to get a shade worth.
The colors are a transition r – g – b – again to r.
if pos 255:
return (zero, zero, zero)
if pos < 85:
return (255 – pos * three, pos * three, zero)
if pos < 170:
pos -= 85
return (zero, 255 – pos * three, pos * three)
pos -= 170
return (pos * three, zero, 255 – pos * three)

After that, use the rainbow_cycle() perform that makes use of the outcomes from the wheel() perform to distribute the rainbow throughout the variety of LEDs in your strip.

def rainbow_cycle(wait):
for j in vary(255):
for i in vary(n):
rc_index = (i * 256 // n) + j
np[i] = wheel(rc_index & 255)

This perform accepts as argument the ready time. The ready time defines how briskly the rainbow impact strikes.

Understanding how these features work, you’ll be able to construct your personal tasks to supply superb lighting results. To see how all the things works collectively, within the subsequent part we’ll construct a easy venture to regulate a bunch of addressable RGB LEDs.

WS2812B RGB LEDs with MicroPython: Venture instance

Right here, we’ll construct a easy circuit with four pushbuttons. It’ll make totally different lighting results relying on the pushbutton pressed.

ws2812b led with esp32 esp8266 micropython circuit

Elements Required

On this challenge we’re utilizing two addressable RGB LED rings with totally different sizes and an addressable RGB LED stick. Nevertheless, you need to use an RGB LED strip or addressable RGB LEDs in different configurations.


In each ESP32 and ESP8266 we’ll wire the circuit as follows:

Button 1 GPIO 14
Button 2 GPIO 12
Button three GPIO 13
Button four GPIO 15
Addressable RGB LED knowledge pin GPIO 5

Observe: you’ll be able to selected some other digital pins, if wanted.


You can even comply with the subsequent schematic diagram to wire the circuit for ESP32:

esp32 neopixel pushbutton ws2812b project


Comply with the subsequent schematic diagram for those who’re utilizing an EPS8266:

esp8266 neopixel pushbutton ws2812b project


Add the next code to your ESP32 or ESP8266 as important.py.

How the Code Works

Proceed studying this part if you wish to find out how the code works. In any other case, you possibly can skip to the “Demonstration” part.

Begin by importing the required libraries:

from machine import Pin
import machine, neopixel, time

The pushbuttons will probably be set as interrupts. So, we have to create an interrupt dealing with perform that may run each time an interrupt occurs – on this case, the button_handler() perform.

def button_handler(pin):
international button_pressed
button_pressed = pin

The interrupt dealing with perform has an enter parameter (pin) during which an object of sophistication Pin can be handed when the interrupt occurs. This enables us to know which pin generated the interrupt.

In our instance, the deal with interrupt perform is known as button_handler and it saves the pushbutton that was pressed on the button_pressed variable.

def button_handler(pin):
international button_pressed
button_pressed = pin

Necessary: button_pressed is outlined as a worldwide variable, as a result of we would like it to be accessible all through all of the code, not simply inside the button_handler() perform.

After that, configure the pushbuttons as interrupt pins:

button1 = Pin(15, Pin.IN)
button1.irq(set off=Pin.IRQ_RISING, handler=button_handler)
button2 = Pin(14, Pin.IN)
button2.irq(set off=Pin.IRQ_RISING, handler=button_handler)
button3 = Pin(12, Pin.IN)
button3.irq(set off=Pin.IRQ_RISING, handler=button_handler)
button4 = Pin(13, Pin.IN)
button4.irq(set off=Pin.IRQ_RISING, handler=button_handler)

By default, the button_pressed variable is the same as button1, as a result of this button clears the LEDs and we would like them to be off by default.

button_pressed = button1

Then, we create a neopixel object on GPIO 5, with 48 LEDs. You must change the n variable with the variety of LEDs you’re controlling.

n = 48 # variety of pixels
p = 5 # strip management gpio
np = neopixel.NeoPixel(machine.Pin(p), n)

After that, we outline the features we’ve seen in a earlier part: bounce(), cycle(), wheel(), rainbow_cycle(),and clear().

Within the whereas loop, we examine which button was pressed and name a unique perform relying on the button pressed:

  • Button 1: clears the strip (all neopixels off)
  • Button 2: bounce impact
  • Button three:cycle impact
  • Button four: rainbow impact

whereas True:
if button_pressed == button1:
elif button_pressed == button2:
bounce(23, 210, 15, 10)
elif button_pressed == button3:
cycle(123, zero, 154, 20)
elif button_pressed == button4:

Notice: you’ll be able to change the arguments of the earlier features to set the LEDs in several colours or regulate the wait parameter to make the impact quicker or slower.


After importing the earlier code to your ESP32 or ESP8266 as important.py, press the ESP Allow/Reset button to run the brand new code.

Press every pushbutton to supply totally different results. You possibly can watch the video under for a stay demonstration:

Notice: whenever you press the pushbutton to pick an impact, it’ll solely begin when the impact that’s operating stops.

Wrapping Up

On this tutorial you’ve discovered the best way to management WS2812B addressable RGB LEDs (rings, strips, or sticks). Controlling these LEDs with MicroPython is straightforward because of the neopixel library. You’ve additionally discover ways to set a number of interrupts with MicroPython.

Now, you possibly can apply the ideas discovered on this tutorial in your personal tasks. For instance, you possibly can construct an internet server with totally different buttons that controls the LEDs remotely.

We have now extra tutorials about RGB LEDs that you could be like:

Different tutorials about MicroPython and ESP32 and ESP8266:

We hope you loved this undertaking and discovered one thing new.

Thanks for studying.

Really helpful Assets