lightofshadow's blog

How to enable UART communication on LEGO MINDSTORM/SPIKE

If you are developing an advanced project using LEGO kits for robotics you maybe want to connect some custom made sensors based on MCUs like Arduino or Raspberry Pi Pico or maybe you want to link two LEGO hubs together. To do that you can use the LEGO hub ports as Serial.

LEGO currently has two robotics hubs:

legoHubs.png

they have the exact same internals but the software on them is kinda different.

To use the hub ports as UARTs you have to use the hub api, documented on this lego github page. The LEGO hub api is available only on the MINDSTORM firmware so, if you have a blue hub you are good to go. if you are working with a SPIKE hub, the yellow one, you have to install the MINDSTORM hub’s firmware on it.

Installing MINDSTORM firmware on the spike hub

Connect the hub to your computer

Go to https://code.pybricks.com/

Select the “Restore Official LEGO Firmware” option

immagine-1.png

Then select the mindstorm hub option screenshot2.png

After that follow the on screen instructions until your hub is flashed.

To update the hub’s firmware to the latest version you have to connect it to official LEGO MINDSTORM app

The app will then automatically ask you to update the hub’s software

Hardware needed

if you want to hook up your hub to a microcontroller such as Arduino, ESP32 or Raspberry Pi Pico i suggest buying a LEGO powered up cable like this one from AliExpress and then crimping or soldering jumper ends to it.

You can also buy a double ended cable like this if you want to connect two hubs together.

As an alternative you can 3d print and build your own powered up cable, to do that follow this blogpost

Here is the Powered Up port pinout as described in this blogpost

pinout.gif

Pin Label Function
1 M1 Motor power lead 1 (PWM controlled)
2 M2 Motor power lead 2 (PWM controlled)
3 GND Ground (0V)
4 VCC Power for device electronics (3.3V)
5 ID1 Analog identification line 1 / Serial data (hub -> device)
6 ID2 Analog identification line 2 / Serial data (device -> hub)

To use the SPIKE’s UART you’ll only need

It looks like the other three pins are used for VCC and motor power control.

Code to use the SPIKE’s UART

import hub
import utime
from mindstorms import *

#function to read commands formatted like: <message>
def read(port):
    recv = False
    s = ""
    while True:
        b = port.read(1)
        if b == b"<":
            recv = True
            s = ""
        if recv:
            s += b.decode()
            if b == b">":
                recv = False
                return s[1:-1]
        #print(s)

#function to write commands
def write(port, msg):
    port.write(str("<" + msg + ">").encode())

the first function continuously reads bytes from the UART port, it detects < and > as the messages start and stop chars so it continuously reads chars, after it receives < it appends the chars to a string until it receives > then it returns the string.

the second function simply writes a string delimited by < and >.

portC = hub.port.C
portC.mode(1)
utime.sleep_ms(2000)
portC.baud(115200)

this part of the code declares the hub’s port it sets it as full duplex and sets the port’s baud rate to 115200 baud.

Now you can connect any other MCU like Arduino or Raspberry Pi Pico and then you can normally read/write data using the UART connection. You can also connect another LEGO hub so that you can enable comunication between the two of them