STEMBoT STEMBoT 2 User manual

User’s Manual
Rev. 1.1.0

STEMBoT 2 User’s Manual
========================================================================
Table of Contents
1.0.0.0 Introduction
1.1.0.0 The Purpose of the STEMBoT 2
1.2.0.0 How to use this manual
2.0.0.0 Hardware Overview
2.1.0.0 STEMBoT 2 Board
2.2.0.0 Battery and Charger
2.2.1.0 Charging the SB2 Battery
2.3.0.0 Remote Controller
2.3.1.0 Pairing the Remote to the SB2
2.4.0.0 Chassis and Motors
3.0.0.0 Reserved
4.0.0.0 Programming
4.1.0.0 General
4.1.1.0 Programming via Serial Communication
4.1.2.0 Programming via Mass Storage
4.2.0.0 Programmable Hardware Components
4.2.1.0 Multicolor LED
4.2.2.0 Switches
4.2.3.0 Buzzer
4.2.3.1 Playing Songs from the Board
4.2.3.2 Creating Songs using RTTTL
4.3.0.0 Liquid Crystal Display (LCD)
4.3.1.0 Colors
4.3.2.0 Text
4.3.3.0 Shapes
4.3.3.1 Drawing Rectangles
4.3.3.2 Drawing Circles
4.3.3.3 Drawing Triangles
4.4.0.0 Radio (Remote Controller)
4.4.1.0 Initialization and Configuration
4.4.2.0 Reading Remote Controller Signals
4.5.0.0 Motors
4.5.1.0 Initialization and Configuration
4.5.2.0 Speed and Acceleration
4.5.3.0 Distance and Time
4.6.0.0 UEXT Headers
4.6.1.0 General Purpose Use
4.6.2.0 Serial Communication
4.6.2.1 Reserved
4.6.2.2 Reserved
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
4.6.2.3 Inter-Integrated Circuit (I2C)
4.6.3.0 Plug and Play Modules
4.6.3.1 LED Module
4.6.3.2 Seven Segment Display Module
4.6.3.3 Reserved
4.6.3.4 Reserved
4.6.3.5 Reserved
4.6.3.6 Reserved
4.6.3.7 SHT21 Temperature/Humidity Sensor
4.6.3.8 MPU6050 Motion Processing Unit
4.7.0.0 Servos
4.7.1.0 Servo Calibration
4.7.2.0 Angles and Timing
5.0.0.0 Mobile Commander
6.0.0.0 Troubleshooting
7.0.0.0 Module/Class/Function Quick Reference
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
1.0.0.0 Introduction
MicroPython is an implementation of the Python programming language optimized for
embedded systems. MicroPython is written in C and includes a Python compiler and parser to
allow users to program in Python on low-level hardware.
The STEMBoT2 (SB2) uses version 1.10 of MicroPython loaded onto an STM32 32-bit
microcontroller unit (MCU), and is intended for education and embedded systems development.
Its features include an integrated LCD, speaker, several programmable push buttons, and
headers for motors, servos, and serial communication. The three ports for serial communication
use the UEXT (universal extension) layout and were included to be used with interchangeable
peripherals.
1.1.0.0 The Purpose of the STEMBoT 2
The STEMBoT 2 is the ultimate platform for learning how to program using the Python
programming language. With the proliferation of technology in our everyday lives, it’s as
important as ever to develop an understanding of programming and computer science topics.
Python is one of the most popular programming languages in fields such as data science,
financial services, and artificial intelligence, but it’s also one of the easiest and most forgiving
languages for new programmers to learn. The STEMBoT 2 takes this ease-of-use one step
further by providing an engaging and interactive programming experience.
1.2.0.0 How to use this manual
This manual should be read start to finish by those new to programming Python. The
purpose of this manual is not to teach Python, but to show how the STEMBoT 2 board works.
For those already familiar with programming in Python, this manual contains high-level function
descriptions at the end.
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
2.0.0.0 Hardware Overview
The STEMBoT 2 package comes with all of the parts necessary to construct the robot as
well as a remote control and starter set of UEXT modules. The remote control and UEXT
modules come pre-assembled and assembly instructions for the robot are outlined in the
following section (3.0.0.0).
The package contains the following items:
- One STEMBoT 2 Board
- One Aluminum Chassis
- One Laser-cut Acrylic Enclosure
- Two 2-Phase Stepper Motors
- Two Rubberized Wheels
- Two Omnidirectional Wheels
- One Remote Control
- One LED Plug and Play Module
- One OPT3001 Plug and Play Module
- One SHT21/30 Temperature Sensor Plug and Play Module
- One Seven Segment Display Plug and Play Module
The standard configuration for the SB2 is the way the robot is assembled and delivered
to users. The PCB is inside the acrylic enclosure, which is mounted to the front of the aluminum
chassis. The stepper motors are mounted underneath the enclosure, and plugged into the motor
headers closest to each motor. The battery is affixed to the chassis via velcro tape and a plastic
cable hook attached with a screw and nylock nut. The omnidirectional wheels are attached to
the rear of the chassis, facing inwards, and at the bottom of the screw slot.
2.1.0.0 STEMBoT 2 Board
The heart of the STEMBoT 2 system in the main printed circuit board (PCB). This board
contains 5 programmable switches, a full-color LCD, 4 servo ports, a tri-color LED, two motor
headers, three UEXT headers for plug and plug functionality, and a piezoelectric buzzer.
There’s also a barrel jack for wall charging, a power indicating LED, a reset button, and a
USB-B connector for programming.
2.2.0.0 Battery and Charger
The SB2 comes with a 6-cell, 7.2V, 3800mAh Nickel-Metal Hydride (NiMH) battery , as
well as a 12V AC/DC adapter. The battery is connected to the board through an XT-60 adapter.
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
2.2.1.0 Charging the SB2 Battery
Without the charger connector, the SB2 will notify the user of a low battery condition if
the green LED adjacent to the charging connector starts to blink. To charge the SB2 battery,
make sure both the battery and charger are plugged in. Charging will begin automatically,
whether the board is turned on or not. One full charge cycle takes a maximum of 4 hours, and a
completed cycle is indicated by a solid green light on the LED.
2.3.0.0 Remote Controller
The STEMBoT 2 comes with a remote controller for wirelessly operating the robot. The
controller is a standard XBox controller design with custom internal hardware.
2.3.1.0 Pairing the Remote Controller
To pair the remote, navigate to “Tools” on your STEMBoT 2 and then click the B button
to enter into the pairing mode. Next press the sync button on the remote, near to Left-button
bumper. After it says “Successfully paired” you can then drive your bot by selecting the remote
control application.
2.3.2.0 Calibrating the Remote Controller
After successfully pairing, if either of your STEMBoT’s motors are moving on their own,
you can recalibrate your remote by holding the middle green button. Once the red LED flashing
sequence begins, move your joysticks in circular patterns until the LED flashing sequence
finishes.
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
4.0.0.0 Programming - Introduction
Programming of the STEMBoT 2 is done through the USB port of the board and the
provided cable. With the USB plugged in, the user can view and edit Python files on the board,
and program the board through a serial communication program.
4.1.0.0 How to Program the SB2
After starting the SB2, the main menu will appear on the LCD. In this menu, there is an
option to Exit to Python REPL. With the USB cable plugged in, this option will automatically
mount a drive to your computer called PYBFLASH. The /apps folder in this drive contains the
example apps found on the board’s main menu, and can be used to store user generated
programs. Exiting to Python REPL will also allow the user to access the board’s functions
through a serial communication program.
Caution: Editing the default boot.py file can make the STEMBoT 2 inoperable.
Warning: The PYBFLASH drive on your computer must be unmounted before
the USB cable is removed. Failure to do so could corrupt data in transit, making the
STEMBoT 2 inoperable.
4.1.1.0 Programming via Serial Communication
After selecting the Exit to Python REPL option in the main menu, the SB2 can be
accessed through a serial communication program. This allows programming in Python on a
line-by-line basis. Connect to the SB2 using your serial communication program of choice.
Note: A popular program for this purpose is PuTTY, a free and open-source terminal
emulator. This program will be used throughout this document, and can be downloaded
at https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
Type the following three lines, pressing enter after each one. If you are successfully
connected, this will turn on the blue LED just above the SB2 display.
import pyb
LED=pyb.LED(3)
LED.on()
Note: For information on selecting the correct port and properly configuring PuTTY for
use with the SB2, see User Guide: Setting Up Serial Communication for the STEMBoT
2, posted on the STEMBoT Inc website. Click here for the Putty Setup Guide.
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
Note: Most functions of the SB2 can be accessed via serial communication, but the
radio cannot, due to conflicts with the serial communication bus.
4.1.2.0 Programming via Mass Storage
Python programs can be added to the SB2 by dragging and dropping files into the /apps
folder located on the PYBFLASH drive. Restarting the STEMBot 2 will allow the user to start
using their new programs right away, assuming there are no compilation or runtime errors. On
your computer, create a new file using the text editor of your choice. In this file, type the
following code:
import pyb #imports LED functions like with the above example
import uasyncio #necessary for all programs placed in mass storage
async def main(): #necessary header for all programs in mass storage
pyb.LED(1).off() #turn off red LED
pyb.LED(2).off() #turn off green LED
pyb.LED(3).on() #turn on blue LED
Program 1: Turning on the Blue LED
Save the file as “new_led.py” to the /apps folder of the PYBFLASH drive. Restart the
SB2 by pressing the blue reset button. On the main menu, the new program titled “new led”
should appear. Running this program will turn on the blue LED just above the SB2 display.
Note: Due to the nature of the boot.py file, main function headers should start with the
keyword async, for example, async def main():
Caution: When changing data on the SB2 via this method, the red LED will turn on. The
SB2 should remain plugged into the computer until this red LED turns off. Unplugging
the SB2 before the LED turns off could interrupt the data transfer and result in
unintended operation.
4.2.0.0 Programmable Hardware Components
The STEMBoT 2 board itself comes with several programmable components, including
switches, a multicolor LED, and a buzzer. These components are common among electronics
and serve a variety of purposes. For example on the SB2, the switches are used to navigate the
main menu and the LED is used as a status indicator, telling the user when programs are being
uploaded or when an error has occurred. What follows is a description of these components and
examples on how to access them through Python programming.
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
There are three modules used for accessing low-level functions of the SB2: pyb,
switch and buzzer. The pyb module is used for the multicolor LED, as demonstrated in
previous examples. The switch module can be used for accessing the buttons and other
general-purpose input/output pins (GPIOs). The buzzer module is used for accessing the on
board buzzer to produce songs and other sounds.
Note: The pyb module was developed primarily for a different board which also uses
MicroPython, while sb contains similar functions generalized for any board. When
possible, sb should be used over pyb.
4.2.1.0 Multicolor LED
The LED is accessible via the LED() function of the pyb module, and it is
programmable for 3 different colors.
# This program will turn off the main LED, then cycle through
# its red, green and blue components, blinking each of them
# twice in an infinite loop.
import pyb #grants access to the LED class
import uasyncio #grants access to asynchronous delays (very useful!)
async def main(): #standard header for programs in the /apps folder
x=1
while x<4: #turn off LEDs 1 through 3 (all of them)
pyb.LED(x).off #turn off the x LED
x+=1 #increment x
while(True): #infinite loop
x = 1
while x<4: #cycle through RGB
led=pyb.LED(x)
led.on() #turn on LED
await uasyncio.sleep_ms(1000) #one second delay
led.off() #turn off LED
await uasyncio.sleep_ms(1000) #one second delay
led.on()
await uasyncio.sleep_ms(1000)
led.off()
await uasyncio.sleep_ms(1000)
x+=1
Program 2: Blinking the LEDs with Delays
Note: The LED is also used for several status indications: it will blink red and green if an
error has occurred and will stay solid red while data is being transferred to the SB2.
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
The three available colors, red, green, and blue, are numbered 1, 2, and 3, respectively,
when using the LED() function.
4.2.2.0 Switches
As detailed in the Hardware Overview (section 2.1.0.0 of this guide) the SB2 has five
programmable switches. These buttons can be used by importing the machine Python module.
The switches by default are configured as inputs with their pull-up resistors enabled, as the
following code demonstrates.
pinS8=machine.Pin('E5',machine.Pin.IN,machine.Pin.PULL_UP)
pinS7=machine.Pin('B6',machine.Pin.IN,machine.Pin.PULL_UP)
pinS6=machine.Pin('B5',machine.Pin.IN,machine.Pin.PULL_UP)
pinS5=machine.Pin('B2',machine.Pin.IN,machine.Pin.PULL_UP)
pinS4=machine.Pin('F11',machine.Pin.IN,machine.Pin.PULL_UP)
The pins above are named for their label on the PCB. Their values can be read with the
value() method of the Switch() class in the switch module.
Caution: The Pin() function of the machine module can also be used to access other
pins on the SB2’s main microcontroller. Changing the function or value of certain pins
may render critical functions of the SB2 inoperable.
As an example of how the switches work, open a serial connection to the SB2. Type in
the following lines, pressing enter after each one.
from switch import Switch
button=Switch(“up”)
button.value()
Program 3: Getting the State of the Up Button
After entering the last line, False should appear on the serial terminal. This indicates that
the button is not being pressed. Now, hold down the upper left button and send the
button.value() line again. The 0 that appears on the serial terminal indicates the button is
being pressed. Sampling this value is how the buttons are used to scroll through the main
menu, but they can also be used for custom programs.
Note: In the serial terminal, the last line of code can be quickly accessed by pressing the
up button on your keyboard.
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
4.2.3.0 Buzzer
Use of the piezoelectric buzzer requires the buzzer Python module. The buzzer is
used for creating sounds, from simple tones to more complex musical pieces
4.2.3.1 Playing Songs from the Board
The SB2 comes with several songs pre loaded. These songs can be accessed using the
play_song() function of the buzzer module.
import buzzer
buzzer.play_song(‘NationalAnthem’)
Program 4: Playing the National Anthem
A complete list of available songs can be found in the document User Guide: List of
Available Buzzer Songs.
4.2.3.2 Creating Songs using RTTTL
RTTTL('C Major Scale:d=4,o=5,b=180:c,d,e,f,g,a,b,c6') #C major scale
The first parameter is the title of the tune. The next set of parameters are d, o, and b,
which represent the default note, the default octave, and the tempo, respectively. For d, 1
represents a whole note, 2 is a half note, 4 is a quarter note, etc. The o sets the default octave
between 4 and 7, but individual notes can be programmed for different octaves if need be. The
b is the tempo given in beats per minute (bpm).
The last set of parameters defines the tune itself, and uses a duration-pitch-octave
structure for its strings. For example, the string 2f#6 would play the F# note in the sixth octave
for two intervals (defined by the d parameter).
# This program uses RTTTL to play the national anthem.
import rtttl
import buzzer
async def main():
anthem=RTTTL('Anthem:d=16,o=5,b=90:2g,e,4c,4e,4g,2c6,8e6,d6,4c6,4e,4f#,2g')
buzzer.play(anthem)
Program 5: Playing the National Anthem
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
4.3.0.0 Liquid Crystal Display (LCD)
Liquid crystal displays (LCDs) are used virtually anywhere modern electronics are found.
From TVs to computers to cell phones, they allow people to connect with technology in an
intuitive and seamless way. The LCD on the SB2 is 320x240 pixels wide, and manipulating
pixels is done by defining their location, and then the data to be written.
The origin of the LCD, that is, where x and y are equal to 0, is at the upper left hand
corner. Functions and methods that use x and y for drawing or printing to the LCD are thus
measured from this point. For example, drawing an arrow that points to the x,y coordinate
160,120 will point to the center of the LCD (half of the total width and height).
The SB2’s LCD is accessible through the graphics and color modules. These
modules allow the user to create colors, write text to the LCD, and draw shapes.
4.3.1.0 Colors
The LCD uses standard RGB values to determine which color should be displayed.
These colors can be created by using the RGB() function of the color module. This function
takes three parameters, numbers between 0-255, corresponding to red, green, and blue..
Open a serial communication with the SB2. Input the following lines of code to
demonstrate the basic functions of the lcd module.
import graphics
import color
red=color.RGB(255,0,0)
graphics.paint(red)
Program 6: Coloring the LCD Red
This set of code will color the entire LCD with the color red. The screen() class is used
to access the LCD’s functions, so it should be instantiated before using those functions. The
paint() function of the screen() class fills the entire LCD with a given color. This can be used
to set a solid colored background or to erase the entire screen.
Note: While the entire screen will be colored by the paint() function, using the mass
storage method of programming will allow the SB2 status bar to override that effect on
the very top of the screen. For this reason, it is recommended that programs using the
LCD avoid using the upper (rewrite) pixels of the screen.
Note: An online RGB color wheel can be found here:
https://www.colorspire.com/rgb-color-wheel/
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
4.3.2.0 Text
Writing text to the LCD is done through the print() function of the graphics() module.
The print() function takes four parameters: the string to be written, the x location, the y
location, and the color. For ease of use, three additional functions are provided: printTop(),
printMiddle(), and printBottom(). These functions accept only two arguments, the string and
color, and print to the top of the LCD, the middle, and the bottom, respectively.
# This program is for demonstrating the operation of the LCD by
# printing "Hello, world!" in white text on a blue background.
import graphics #imports the module used for LCD operations
import color #imports the module for LCD colors
import uasyncio #imports the module used for concurrent programming
bgColor=color.RGB(0,0,255) #blue
textColor=color.RGB(255,255,255) #white
async def main(): #main function header
graphics.paint(bgColor) #fill screen with color
graphics.print("Hello, World!",40,120,textColor) #print to LCD
Program 7: Hello, world!
Text that has been written using the print() function can be erased using the erase()
function. This function accepts four arguments as well: the number of characters to be erased,
the x location, the y location, and the color. The x and y locations should be the same as those
sent to the print() function. The color should be the background color on which the characters
have been written.
For an example using the print() and erase() functions, set up a serial connection to
the SB2, and type in the following lines:
import graphics
import color
red=color.RGB(255,0,0)
black=color.RGB(0,0,0)
graphics.paint(red)
graphics.print(“Hello, world!”,40,120,black)
Now, after making sure the text has shown up, send the following command to the SB2:
graphics.erase(13,40,120,red)
This line tells the SB2 how many characters there are in “Hello, world!” (be sure to
include spaces), the placement of the text, and the background color. The erase() function
works by drawing a filled rectangle (section 4.3.3.1) around the previously printed text. If a color
other than the background color is sent, the LCD will simply print a box of that color over the
text.
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
Text that is printed to the LCD will not automatically clear if new text is printed, so it’s up
to the programmer to “clean up” after printing text.
# The following program is a basic second counter. It prints to the LCD how
many
# seconds have passed.
import lcd #import lcd functions
import gfx #this module can be used to draw shapes
import uasyncio #import timing functions
lcdWidth=320 #define the LCD dimensions
lcdHeight=240
LCD=lcd.screen() #instantiate a screen object
bgColor=LCD.colorRGB(0,0,255) #blue
textColor=LCD.colorRGB(0xFF,0xFF,0xFF) #white
#the GFX function allows shapes and other objects to be printed with ease
graphics = gfx.GFX(lcdWidth, lcdHeight, LCD.lcd.pixel)
async def main(): #standard header
LCD.paint(bgColor) #fill the screen
x=1 #set up variable to count
while(True): #infinite loop
# Print the time to the LCD: the curly brackets allow numbers to be
# printed as strings when using the format() method
LCD.lcd.text("Time = {}".format(x),40,90,textColor)
# Wait for 1 second to pass
await uasyncio.sleep_ms(1000)
# Increment the counter
x+=1
# “Erase” the previous number by filling in the same space with the
# background color. The parameters are x, y, width, height, color.
# Choosing 96 for the x value starts coloring in after the “Time = “
# text. You’ll notice starting with 10 seconds, only the first number is
# erased.
LCD.erase(5,96,90,bgColor)
Program 8: LCD Timer
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
4.3.3.0 Shapes
The SB2 includes functions for drawing various shapes, either filled or unfilled, to the
LCD. The functions require defined coordinates as well as the desired color of the shape.
# This program demonstrates the SB2’s ability to draw shapes by drawing a green
# square, red circle, and blue triangle to the middle of the screen
import graphics
import color
import uasyncio
black=color.RGB(0,0,0)
red=color.RGB(255,0,0)
green=color.RGB(0,255,0)
blue=color.RGB(0,0,255)
async def main():
graphics.paint(black)
graphics.fill_rectangle(100,60,120,120,green) #green square
graphics.fill_circle(160,120,60,red) #red circle
graphics.fill_triangle(160,60,100,180,220,180,blue) #blue triangle
Program 9: Drawing Filled Shapes
4.3.3.1 Drawing Rectangles
The two functions available for drawing rectangles are rectangle() and
fill_rectangle(). The first function draws the outline of the shape, while the second fills in the
shape with a given color. Both of these functions take five parameters: the starting x and y
coordinates, the desired width, the desired height, and the color.
4.3.3.2 Drawing Circles
The two functions available for drawing circles are circle() and fill_circle(). The first
function draws the outline of the shape, while the second fills in the shape with a given color.
Both of these functions take four parameters: the x and y coordinate for the center of the circle,
the radius of the circle, and the color.
4.3.3.3 Drawing Triangles
The two functions available for drawing triangles are triangle() and fill_triangle().
The first function draws the outline of the shape, while the second fills in the shape with a given
color. Both of these functions take seven parameters: three pairs of x and y coordinates which
define the corners of the triangle, and the desired color.
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
4.4.0.0 Radio
The SB2 has a built in radio for communicating with the remote control.
4.4.1.0 Initialization and Configuration
4.4.2.0 Reading Radio Signals
Once the remote control is paired with the SB2, it starts transmitting button presses and
joystick information.
4.5.0.0 Motors
The SB2 comes with two stepper motors for driving the robot. Stepper motors get their
name from the fact that they move incrementally, or in steps. The standard motors for the SB2
take 200 steps to complete a full rotation.
4.5.1.0 Initialization and Configuration
Use of the two stepper motors for movement requires the sb module. The Motor() class
contains a set of methods that turn the motors on or off, and set their speed and acceleration.
There’s also a distance() function that commands the motors to move a given number of steps
at a given speed. To begin using the motors, turn them on by setting the sleep mode to false
with sleep(False), as shown in the next program.
4.5.2.0 Speed and Acceleration
The speed() method is used for setting a constant, immediate speed, or for
accelerating to a given speed in a given period of time. Passing no parameters with the speed()
method will return the current speed. Passing the speed() method one parameter (between 50
and 500) will start the motor immediately at that speed. Passing two parameters will cause the
motor to rev up to a given speed (the first parameter) in a given time (the second parameter, in
milliseconds). The motor can be stopped by sending 0 speed, and sending negative speeds will
cause the motor to move in reverse.
import uasyncio as asyncio
import sb
async def main():
#create the two motor objects
leftMotor=sb.Motor(1)
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
rightMotor=sb.Motor(2)
#motor control logic is initially disabled. Let’s enable it
leftMotor.sleep(False)
rightMotor.sleep(False)
while(True):
#left motor moves forward at 200 steps per second
#and will take 1 second to transition to that speed
leftMotor.speed(200,1000)
#right motor moves backwards at 200 steps per second
#and will take 1 second to transition to that speed
rightMotor.speed(-200,1000)
#do the above (spin) for 5 seconds
await asyncio.sleep_ms(5000)
#left motor moves backward at 200 steps per second
#and will take 1 second to transition to that speed
leftMotor.speed(-200,1000)
#right motor moves forward at 200 steps per second
#and will take 1 second to transition to that speed
rightMotor.speed(200,1000)
#do the above (spin) for 5 seconds
await asyncio.sleep_ms(5000)
Program 11: Spinning Robot
4.5.3.0 Distance and Time
The distance() method accepts 4 parameters: the distance to move (in steps), the
speed, the acceleration, and blocking mode. The speed and acceleration are the same as in the
speed() method. If the blocking mode is set to True, the code will not continue until the motor
has finished rotating. This can be useful if there are several lines of distance() methods placed
one after the other.
4.6.0.0 UEXT (Plug-and-Play) Headers
On the STEMBoT 2, there are three headers for UEXT connections. UEXT is a standard
means of connecting to modules via three types of serial interfaces, UART, SPI, and I2C. The
header also contains pins for power (3.3V) and ground.
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
Figure 1: UEXT Header Pin Labels
4.6.1.0 General Purpose Use
The pins used for serial communication can also be used as GPIO (general purpose
input/output) pins. These pins can be accessed by including the pnp module, which contains
the GPIO class.
#the following statements will return a list of pins
#according to the parameter in the GPIO function
import uext
port=pnp.GPIO(“J11”)
The port object in the example above will give access to the GPIOs on header J11,
which are set as outputs by default. A parameter of “top” can also be used to access this port.
The other ports, J8 and J10 are also called “left” and “right”. The individual pins can be
accessed through the GPIO class like in the following example:
port.pin[3].on() #turn on pin 3 (SDA)
port.pin[3].off() #turn off pin 3
port.pin[3]() #returns either 1 or 0 if the pin if high or low
The GPIO class of the uext module also contains two functions for turning all of the pins
on or off. These functions are called allOn() and allOff(), respectively. Toggling of individual pins
can be done with the togglePin() function, which takes the target pin as its only parameter.
port.allOn() #activate all pins
port.allOff() #deactivate all pins
port.togglePin(port.pin[3]) #activate if off, deactivate if on
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
The pins can be also be assigned individually by importing the machine module. In the
case of J11, the header on the top of the STEMBoT 2, the pins have been defined below.
pinTX=machine.Pin('C12',machine.Pin.OUT)
pinRX=machine.Pin('D2',machine.Pin.OUT)
pinSDA=machine.Pin('B11',machine.Pin.OUT)
pinSCL=machine.Pin('B10',machine.Pin.OUT)
pinSSEL=machine.Pin('A5',machine.Pin.OUT)
pinMOSI=machine.Pin('B15',machine.Pin.OUT)
pinMISO=machine.Pin('B14',machine.Pin.OUT)
pinCLK=machine.Pin('B13',machine.Pin.OUT)
The MOSI, MISO, and CLK pins are common among all three UEXT connectors. That is,
turning one on or off does the same for all three. The SDA and SCL pins are common between
the J8 and J10 UEXT ports, and are on by default. Toggling the pins is done by using the on()
or off() functions. The value() function can be used to determine if the pin is high (1) or low
(0).
pinTX.on()
pinTX.off()
pinTx.value() #this will return 0, since it was just turned off
Note: Toggling can be done at 35.7kHz (T/2=13.3us). [Initial test]
4.6.2.0 Serial Communication
4.6.2.1 Reserved
4.6.2.2 Reserved
4.6.2.3 Inter-Integrated Circuit (I2C)
I2C objects are created using the machine module. Initially, the user must select the
bus and baud rate (or bit rate) of the I2C object. The SB2 has two busses for I2C
communication: bus 1 corresponds to headers J8 and J10, located below the LCD, and bus 2
corresponds to header J11, just above the LCD.
import machine
#create I2C object on bus 1 with baud rate of 9600 bits per second
i2c=machine.I2C(1,freq=9600)
Note: The baud/bit rate is the rate at which data will be sent serially, measured in bits
per second. A rate of 9600 is fairly common, but the SB2 is capable of transmitting at
standard rates up to 115200 bits/second.
========================================================================
Revision 1.1.0 : January 2021

STEMBoT 2 User’s Manual
========================================================================
I2C based UEXT modules can either accept commands directly or consist of a set of
registers which can be accessed individually. Writing via I2C can be done using the writeto()
function by using an integer for the address and a bytes() type value for the data to be sent. A
third boolean (True/False) parameter can be sent to include or exclude a stop condition (check
the appropriate datasheet for more information). Reading can be done with the readfrom
function, again using an integer for the address, but using a bytesarray() type buffer for the
received data. The address for both of these functions should be 7-bits, as MicroPython will
append the integer with either a binary 1 or 0 for reading or writing.
#initialize variables (these types must be used for I2C to work properly)
address=64 #address for SHT21 temperature module
fetchTemp=bytes([0xF3]) #SHT21 temperature command
tempData=bytearray(3) #3 byte buffer for SHT21 temperature data
#create I2C object (remember to import machine first)
i2c=machine.I2C(1,freq=9600)
i2c.writeto(address,fetchTemp,False) #send command to address
tempData=i2c.readfrom(address,3) #read three bytes, store in tempData
Program 12: Controlling an SHT21 PnP Module Manually
Writing and reading via I2C to a register-based device is done similarly. The two relevant
functions, writeto_mem and readfrom_mem_into require an integer for the device and
register addresses, and bytes() or bytearray() type objects for data sending and storage.
4.6.3.0 Plug and Play (PnP) Modules
The SB2 Plug and Play modules can be used by importing the pnp module in Python.
This module contains classes for controlling the available modules as well as control over the
UEXT ports as GPIOs.
4.6.3.1 LED Module
The LED module is controlled through the methods provided by the GPIO class. As well
as the on() and off() methods described in section 4.6.1.0, the GPIO class also contains allOff()
and allOn() methods which turn all of the pins off or on, respectively. This class also contains
the togglePin() method, which turns a pin on if it was previously off, and vice versa.
port.allOff() # deactivate all pins
port.togglePin(port.pin[3]) # activates pin 3, since it was just
# deactivated
========================================================================
Revision 1.1.0 : January 2021
This manual suits for next models
1
Table of contents