Microcontroller Monday: Lego Spike Prime

Lego!!!

It is no secret that I am a big fan of Lego. From childhood I had buckets of the stuff causing injury to bare feet and being used to build elaborate sets for my Star Wars figures.

Earlier this year I was invited to an event run by Tufts University, School of Engineering and it took place at Lego Education London Hub.

So what is Lego Spike Prime?



Ok I'll start by telling you the price. It is around £300 (~$329) for the kit which I have, and this kit was given to me by Tufts University.

Still reading? Ok the Lego Spike Prime is an electronic building kit. We have sensors and inputs, motors and outputs all controlled by a central hub.



The hub is a smart brick. It is compatible with Lego System and Technic pieces. On the face of the hub is a central button used to power on / off the hub and to select options. This button is flanked by two cursor buttons which navigate through the stored programs. At the top right of the hub is a Bluetooth button, used to connect the hub to a PC using Bluetooth.



It has a micro USB connector, which is used to charge the onboard battery and to make a physical connection to a PC.



The supplied battery is bespoke and is a 7.3V 2100mAh Lithium Ion battery.



Around the edge of the hub are a series of lettered ports A to F, and these proprietary ports are used to interface with Lego branded input and output devices.

The hub can be used as is with the accompanying inputs and outputs, offering a zero code option for younger hackers.

What inputs / outputs are in the box?

We have...Inputs



A force sensor (nothing to do with Jedi!) that is used to measure the amount of force placed upon the plunger.



A light sensor, which measure light levels.



An ultrasonic distance sensor

Also inside the hub we have

  • Temperature sensor
  • Accelerometer (measures direction and G force)

For outputs we have...



One large motor, and two smaller versions.



The hub also has a 5x5 LED matrix, which can show simple images, and scroll text. The central button can also change colour. There is also a small speaker in the hub that can make simple beeps and play notes.



We also get a load of Lego bricks and Technic pieces, including many new pieces and colours.

Hi reader!

I never put my blog posts behind paywall or pop ups because quite frankly that is annoying and prevents anyone from accessing the content. I will always keep my blog content free of charge. But I do ask that if you are able and willing, that you buy me a "coffee" as it helps me to pay for hosting this blog, and to buy stuff to hack from Poundshops / Dollar Stores / Aliexpress which are used in free projects and reviews on this blog. It is You dear reader who make this possible, and I am immensely grateful for your support.

Thanks!

Ok so how do I code it?

Lego has their own Spike Prime app, and it works really well. It feels just like Scratch 3.0 and so younger hackers will be right at home.



The Lego Spike Prime app also provides a means to read the hubs onboard sensors.

How did I test it?





I made a robotic frame to fly my X-Wing!





The yellow clips lock and hold the X-Wing in place.



There are two small clips on the rear to stop it from shaking out.



I then took it for a spin, writing code on my laptop and sending over to the hub via Bluetooth.

MicroPython

So here is a not so secret, secret. The Lego Spike Prime runs MicroPython as standard! I used tio to open a serial console and directly access the hub.

Tio?

"tio is a simple TTY terminal application which features a straightforward command line interface to easily connect to TTY devices for basic input/output." - tio website.

I covered this great tool back in January 2020, here is the blog post



The Hub is identified as a LEGO Technic Large Hub with STM32F413xx which is a Cortex M4 unit running at around 100MHz. Flash memory is approximately 1024Kb to 1536Kb, and there is 320Kb of SRAM. I need to do a little more digging around to fully figure this out.

I was then able to send a simple command to show @biglesp on the LED matrix.

Built in modules

I did a little digging around on the hub and here are the built in modules.

__main__          math              uerrno            urandom
_onewire          micropython       uhashlib          ure
builtins          sys               uheapq            uselect
cmath             uarray            uio               ustruct
firmware          ubinascii         ujson             utime
gc                ucollections      umachine          utimeq
hub               uctypes           uos               uzlib

Interesting to see that we have modules for working with JSON, time and direct access to the machine.

Hub Module

The most useful module in the group is hub and this offers us direct access to the inputs, outputs and built in sensors of the hub. Here we can directly control everything via the MicroPython REPL.

__class__       __name__        __version__     BT_VCP
Image           USB_VCP         battery         ble
bluetooth       button          display         info
led             motion          port            power_off
sound           status          supervision     temperature

So by importing the hub module.

import hub

We can control the LED matrix, for example to show "@biglesp"

hub.display.show("@biglesp")

Filesystem

Using import os and running print(os.listdir()) I can see these files.

['bt-lk1.dat', 'bt-lk2.dat', 'local_name.txt', 'boot.py', 'commands', 'event_loop', 'hub_runtime.mpy', 'main.py', 'programrunner', 'projects', 'protocol', 'runtime', 'sounds', 'system', 'ui', 'util', 'version.py', 'runtime.log']

Can I access the files? Well not as a standard USB device. Rather I have to access them via MicroPython. It would be awesome to edit the boot.py and main.py files and write code in MicroPython that would survive a reset.

Firmware

import firmware
firmware.info()
{'appl_checksum': 884719712, 'new_appl_image_stored_checksum': 0, 'appl_calc_checksum': 884719712, 'new_appl_valid': False, 'new_appl_image_calc_checksum': 0, 'new_image_size': 0, 'currently_stored_bytes': 0, 'upload_finished': True, 'spi_flash_size': '32 MBytes', 'valid': 0}

So how can we use it?

Right now the only option is to send commands, character by character to the hub using serial. Not the best way, but it is all we have.

In this video I use that technique with Anvil, a tool to make web applications using Python, to send a command to spin up both motors and fire off a Lego brick. I demoed this at the Tufts University workshop and was able to control the motors from my mobile device.

I will investigate a better approach, but in the meantime I shall create a simple wrapper using Python functions so that anyone can control the hub using simple Python.

Happy Hacking!!!