MicroPython on SPIKE Prime

by CEEOInnovations in Circuits > Microcontrollers

14995 Views, 3 Favorites, 0 Comments

MicroPython on SPIKE Prime

mionpi.jpg

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.

Supplies

SPIKE Prime hub

Computer with USB Port / bluetooth

USB cable to connect the hub to the computer

Grab a Terminal Emulator

Screen Shot 2020-01-07 at 3.30.08 PM.png
milan1.JPG

There are many options to choose from.

CoolTerm works on all platforms, including Pi

Putty works on windows

screen command in Terminal on a unix operating system

Connect SPIKE Prime to the USB Port

IMG_5295.jpeg
IMG_5296.jpeg

Use a microUSB cable to connect the SPIKE Prime to the computer.

Find the Port

Screen Shot 2020-01-07 at 2.57.28 PM.png
milan2.jpeg

We need to know what serial port the SPIKE Prime hub is connected to.

On mac, type

ls /dev/tty.usbmodem* 

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

Connect Up

Screen Shot 2020-01-07 at 3.21.46 PM.png
milan3.JPG

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

Starting the REPL

Screen Shot 2020-01-07 at 3.22.04 PM.png
milan4.JPG
milan5.JPG

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.

Your First Code

IMG_5301.jpg
import hub
hub.display.show(‘Tufts’)

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.

Display Your Name

now try typing

hub.display.show('<your name>') 

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

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:

2+2

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." (do not forget the period after 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.

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

hub.motion.accelerometer()[0] 

or nicer way to code this would be to use variables like this:

import hub
accel = hub.motion.accelerometer()
xAccel = accel[0]
hub.display.show(str(xAccel))

Reading Sensor Values...2

IMG_5311.jpeg

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:

import hub,utime
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.

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.