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.
We use the following connections:
|LED pin||LMS-ESP32 pin on GPIO connector|
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.
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.
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!