SPIKE™ Prime with Python

Charging Rhino

Students will explore using the force sensor to control movement.

45 min
Years 7-9 or Key Stage 3

Questions to investigate

• How can a force sensor be used to help control movements and actions?


• Ensure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.


(Group Discussion, 5 minutes)

Engage students in a discussion about how to stop a charging rhinoceros. Consider viewing videos that show a rhino when charging or running into an object. Discuss how the rhino moves and what it would take to stop it. Also consider discussing the senses that the rhino might use and how these might be similar to the sensors that are available for students to program.


(Small Groups, 20 minutes)

Students will build a Rhino model to investigate different ways to move with the force sensor.

Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for the Rhino model. Ask students to build the model. The building instructions are also available at https://education.lego.com/en-us/support/spike-prime/building-instructions.

Direct students to open a new project in the Python programming canvas. Ask students to erase any code that is already in the programming area. Students should connect their hub.

Rhino Run

Challenge students to have their rhino run forward. Start students with this program.

import runloop
import motor_pair
from hub import port

async def main():
    # pair motors with the left motor on port B and right motor on port A
    motor_pair.pair(motor_pair.PAIR_1, port.B, port.A)
    # move motor pair straight forward
    motor_pair.move(motor_pair.PAIR_1, 0, velocity=750)


Ask students to stop the program. Discuss the fact that there is nothing in the program that tells the Rhino to stop. Eventually, the Rhino runs into something.

Discuss ideas for making the rhino stop. One way is to add a stop to the program or set the start for a certain time, distance, etc. Another way is to use the force sensor attached as the Rhino’s nose.

Prompt students to stop the charging Rhino when he runs into the wall using the force sensor. Students should position the Rhino facing the wall or another strong object a foot or more away. Share this sample program with students. Students will need to type this program into the programming canvas.

import runloop
import motor_pair
import force_sensor
from hub import port

def is_force_sensor_pressed():
    # collect input from force sensor
    return force_sensor.pressed(port.E)

async def main():
    # pair motors with the left motor on port B and right motor on port A
    motor_pair.pair(motor_pair.PAIR_1, port.B, port.A)

    # move motor pair straight forward
    motor_pair.move(motor_pair.PAIR_1, 0, velocity=750)

    # wait until the force sensor is pressed
    await runloop.until(is_force_sensor_pressed)

    # stop the rhino


Note: Remind students to watch for errors in the console. Students can reference the line from the error message to pinpoint where a typing error might have occurred.

Ask students to run the program several more times. Each time, the students should move the Rhino model further away from the wall. Allow students to investigate how moving the model further back does not change the way the program runs.


(Whole Group, 5 minutes)

Discuss with students how the Rhino model moved and review the code as a group.
Ask students questions like:
• How did the program work?
• What do the 0 and 750 represent in the motor_pair.move line of code?
• How did the force sensor work?
• What happened when you moved the Rhino further from the wall? Did running the model at different distances change the way the program worked?
• What was difficult about this challenge?


(Small Groups, 10 minutes)

Challenge students to change their program to investigate how the Rhino stops when he is charging (moving fast) versus when he is moving slowly.

Ask students to run their program two more times. Once with a high velocity (velocity = 1000) and a low velocity (velocity = 250).

    # move motor pair at high velocity
    motor_pair.move(motor_pair.PAIR_1, 0, velocity=1000)

    # move motor pair at low velocity
    motor_pair.move(motor_pair.PAIR_1, 0, velocity=250)

Discuss what happens after each program is run. Students should note that the Rhino just stops, while the force sensor is still touching the object, when the velocity is set at 250. However, when the velocity set to 1000, the Rhino hits the object and bounces back. Discuss as a group why this happens.

Allow students to build a small wall from the additional LEGO elements in their set. Challenge students to program the Rhino to charge through the wall and then play an appropriate sound. Students can also put a message in the console using the print() function to add the Rhino saying something as he hits the wall (ouch!).

Allow students to share and discuss their final programs.


(Group Exercise, 5 minutes)

Teacher Observation
Discuss the program with students.
Ask students questions like:
• How did your force sensor work to control your Rhino?
• Why does the power of the motor affect the way the model reacts when stopping with the force sensor?
• How did the force sensor provide information to the program to tell the Rhino what to do?

Have students answer the following in their journals:
• What did you learn today about using the force sensor to control your Rhino?
• What characteristics of a good teammate did I display today?
• Ask students to rate themselves on a scale of 1-3, on their time management today.
• Ask students to rate themselves on a scale of 1-3, on their materials (parts) management today.

Teacher Support

Students will:
• Explore the force senor
• Understand effects of power on movement

• SPIKE Prime sets ready for student use
• Devices with the SPIKE App installed
• Student journals

2-AP-10 Use flowcharts and/or pseudocode to address complex problems as algorithms
2-AP-13 Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
2-AP-16 Incorporate existing code, media, and libraries into original programs, and give attribution.
2-AP-17 Systematically test and refine programs using a range of test cases.
2-AP-19 Document programs in order to make them easier to follow, test, and debug.