Introduction: MicroPython on SPIKE Prime
You can code SPIKE Prime using MicroPython which is a subset of python for small microprocessors.
You can use any terminal emulator to code the SPIKE Prime hub.
SPIKE Prime hub
Computer with USB Port / bluetooth
USB cable to connect the hub to the computer
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Grab a Terminal Emulator
Step 2: Connect SPIKE Prime to the USB Port
Use a microUSB cable to connect the SPIKE Prime to the computer.
Step 3: Find the Port
We need to know what serial port the SPIKE Prime hub is connected to.
On mac, type
On a pc, look in your device manager under serial to see what serial ports you have connected
On pi, it will be something like ttyAMC0 - check in your /dev/folder
Step 4: Connect Up
Connect up to the right port (from previous step) at 115200 baud
In Terminal, type
usercomputer$ screen /dev/<portname> 115200
In other IDE ,
hit Open/ connect (after setting up the ports and baudrates)
Note: no parity, 8 data bits, and 1 stop bit
Step 5: Starting the REPL
When you connect to the SPIKE Prime from the terminal/ PUTTY you will see a stream of numbers and characters. Those are data from internal sensors of SPIKE Prime hub. To begin press control + c
It will interrupt the serial port and you should see something like this.
MicroPython v1.9.4-1146-gca9944357 on 2019-09-25; LEGO Technic Large Hub with STM32F413xx Type "help()" for more information.
Now you are ready to code.
Step 6: Your First Code
Notice the “import” command - that pulls in a python library that lets you talk to SPIKE Prime. You should see Tufts written on the LED matrix on the hub.
Step 7: Display Your Name
now try typing
note that since you already imported hub above, it is already in memory. If it were not, you would get an error like:
Traceback (most recent call last):
File "<stdin>" , line 1, in <module>
NameError: name 'hub' isn't defined
Step 8: Using REPL
One of the more powerful attributes of Python is that you can test anything out before writing code in the REPL (read eval print loop).
It will execute any python command - try typing 2 + 2 below and see what it says:
Step 9: Exploring MicroPython on SPIKE Prime
Now it is time to explore.
hub has a lot of functions - you can find out by just typing hub and then hitting the TAB key in the REPL. That will show you all the different ways you can complete the command.
Challenge: See if you can read the acceleration.
Step 10: Reading Sensor Values...1
The acceleration data comes back as an array of values. so if you just want the X value, you can try
or nicer way to code this would be to use variables like this:
import hub accel = hub.motion.accelerometer() xAccel = accel hub.display.show(str(xAccel))
Step 11: Reading Sensor Values...2
You could also display all three accelerations using a loop.
We will also import the library utime so that we can pause and give you time to read the number on the screen.
Try this code:
accel = hub.motion.accelerometer() for Acc in accel: hub.display.show(str(Acc)) utime.sleep(0.8)
At this point a few things become important:
spaces - Python is all about indenting right - similar to brackets in other languages, indentation tells you what is inside the for loop and what is not.
when using the REPL, you will notice that when you indent, it no longer executes the line but rather waits for you to finish the lines for the for loop before executing (and the >>> is replaced by …). To finish the for loop, just hit return three times and the loop will be executed.
Step 12: Challenge
Next, see if you can figure out what the code below does - and try executing it to see if you are right.
import hub,utime while True: accel = hub.motion.accelerometer() for Acc in accel: hub.display.show(str(Acc)) utime.sleep(0.8) if hub.port.B.device.get(): break
Hint -you need a sensor on port B.