Controlling Neopixel ws2812 LEDs with LEGO MINDSTORMS – LMS-ESP32 Video Tutorial Part 2

Ste7an

Updated on:

This video tutorial will show how to control a circular Neopixel LED matrix connected to the LMS-ESP32 board. We will control the Neopixel LEDs – also known as ws2812 RGB LEDs – from a LEGO MINDSTORMS or SPIKE Prime Hub.

Preparing the Neopixel LEDs and LEGO MINDSTORMS Hub for this tutorial

We assume you connected the LMS-ESP32 module with your LEGO Hub. You also need some RGB LEDs of the ws2812b type. We used a 12 LED circular LED matrix, but any LED strip will do for this tutorial. We have developed special LED strips that fit LEGO tiles and cross axles. You can use these strips, too, for this tutorial. They are for sale in the webshop.

The WS2812 LEDs have three pins that you should connect to the LMS-ESP32.

We use the following connections:

LED pinLMS-ESP32 pin on GPIO connector
DIGPIO 21
GGND
5V5V
DOnot connected
Pinout pin connections of LMS-ESP32 to WS2812 RGB LEDs

Once the LEDs are connected to the LMS-ESP32 and the LMS-ESP32 is connected to your Lego Hub, you can start the tutorial below.

LMS-ESP32 tutorial Part 1: controlling an LED circular matrix

MINDSTORMS Code for the RGB Neopixel LEDs used in this tutorial

We created a handy library to send and receive commands between the LEGO Hub and the LMS-ESP32. The library is here: https://github.com/antonvh/UartRemote. Documentation of the library can be found on this readthedocs page.

The code we created in this tutorial is in this GitHub repository. The programs are called led_LMS_ESP.py and led_MINDSTORMS.py, respectively.

Code running on LMS-ESP32 module (led_LMS_ESP.py) :

from uartremote import *            # import UartRemote library
import random   
from neopixel import NeoPixel       # import NeoPixel library       
from machine import Pin

ur=UartRemote()
np=NeoPixel(Pin(21),12)             # 12 led neopixel is connected to Pin(21)


def led(angle):
    led_nr=int(angle/(360/12))      # calculate led corresponsing to angle
    for i in range(12):
        np[i]=(0,0,0)               # switch all leds off
    np[led_nr]=(100,0,0)            # set led corresponding with angle to 'red'=(100,0,0)
    np.write()                      # write values to NeoPixel
    
ur.add_command(led)                 # add the command 'led' for usage by the SPIKE PRIME

ur.loop()                           # wait for the 'led' command to be received

Code running on the Lego hub (led_MINDSTORMS.py):

import hub
import time
from mindstorms import Motor
from projects.uartremote import *           # import UartRemote library
motora=Motor('A')                           # connect motot to port "A"
ur=UartRemote('D')                          # LMS-ESP32 is connected to port "D"



while not hub.button.left.was_pressed():
    angle=motora.get_position()             # read absolute angle from motor A
    ack,resp=ur.call('led','repr',angle)    # call function led remotely and pass
                                            # angle as argument
    time.sleep_ms(50)                       # sleep 50ms (loop 20 times per second)

Need more help with MicroPython, LEGO MINDSTORMS Hubs, and NeoPixel LEDs?

This tutorial is part of a multi-part tutorial. First, we set up the hardware, then we explain communication; next, you’ll learn about controlling RGB LEDs and then about I2C joysticks. You can also load MicroPython modules dynamically with our library. And there are more libraries to come.

We’d love to see what you build, and maybe we can help you with some questions. Drop us a line on Facebook!

Like this article? Help us make more!

Your support matters

We appreciate your support on Patreon and YouTube! Small things count.

Become a Patron

Don't miss a thing

Subscribe below to get an email when we publish a new article.

Share this with someone who needs to know

Leave a Reply

Item added to cart.
0 items - 0.00