SPIKE™ Prime with Python

Safe Delivery

Students will explore how motors and sensors can work together safely.

45 min
Years 7-9 or Key Stage 3

Questions to investigate

• How can sensors be used to provide safety?


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


(Group Discussion, 5 minutes)

Kate and Kyle are working on adding a new treehouse to the playground. The delivery cart is coming with the needed materials, but Kyle is concerned the cart might damage the other items on the playground.

Ignite a discussion about how to help ensure the cart does not bump into any other materials on the playground. Consider showing an image of a playground as an example to support the discussion and later planning.


(Small Groups, 20 minutes)

Challenge students to investigate how the Cart can move more safely.

Discuss with students how the cart can move more safely through the playground. Ask students to create a small obstacle course with 2 or 3 items to mimic the playground using objects that are positioned high enough for the distance sensor to “see” them.
Ask students to write a program to allow the Cart to drive forward and stop before hitting an object, thus allowing the cart to move safely through the playground and stop in front of an object. Students should create the program to allow the cart to turn the motor on, until the distance sensor is closer than 15 cm to an object and then stop the motor.

Sample Code:

import motor_pair
import runloop
import distance_sensor
import motor
from hub import port

def distance_sensor_closer_than():
        #return if the distance sensor is less than 150 mm
    distance = distance_sensor.distance(port.B)
    return distance < 150 and distance > -1  
async def main():
    # Pair motors on port A and B
    motor_pair.pair(motor_pair.PAIR_1, port.E, port.A)

    # Move the rear motor to the 0 degree position to make the cart move straight
    await motor.run_to_absolute_position(port.C, 0, 250  )

    # start motor pair moving
    motor_pair.move(motor_pair.PAIR_1, 0, velocity=200)
    # wait until the distance sensor is clower than 150 mm
    await runloop.until(distance_sensor_closer_than)

    # The cart will stop when the distance is less than 150 mm.


Have students create a chart to document each run like the one below.


Students should run the program 5 times changing the speed for the motor each time. Students should run the program, then read the distance shown on the SPIKE App under the Distance Sensor. Allow students time to complete each trial and fill in the chart.


(Whole Group, 5 minutes)

Discuss with students what they found when running the program at different velocity levels.

Discuss the difference between speed and power with students. Explain that when students are setting the velocity in the program what they are actually setting is the degrees per second the motor will rotate. This then determines how fast or at what speed the robot will travel.

Ask students questions like:
• How did the speed of the cart change when the power was increased?
• How did increasing the speed affect the way the cart is able to stop?
• Which power level or speed allows the cart to stop with the most accuracy?
• How should we consider power level or speed in creating the cart to move more safely?


(Small Groups, 10 minutes)

Challenge students to move through the playground without hitting objects no matter where the cart enters.

Discuss with students how the cart needs to move through the playground to deliver the materials. The cart might enter from different spots. Students should try to run their program from different angles to make sure it can safely to move through the playground without hitting an object by using the distance sensor.

Note: The delivery cart cannot make sharp turns (like a 90-degree turn) but rather makes arc turns.

Students should consider the angles the cart might need to move and change the position of the rear wheels as needed (i.e. not set to the 0 degrees position).

Allow students time to explore how to move safely through the playground.

Discuss how the students were able to use the cart in the playground area.

Asking students questions like:
• How were you able to use the sensor to avoid hitting objects?
• How can sensors be used to add safety to moving objects?
• How did you use the additional motor in the back to help move the cart?
• What was difficult about this challenge?

Discuss different programs with students to prompt them to think about where they are relying on programming the motors to move and where they are using the sensors to decide.


(Group Exercise, 5 minutes)

Teacher Observation
Discuss the program with students. Ask students questions like:
• How did you program the sensor to allow the cart to move more safely?
• Why does the power of the motor or speed affect the way the model reacts when stopping with the distance sensor?

Have students answer the following in their journals:
• What did you learn today about setting the power level or speed of the motor when using the sensor to ensure safety and accuracy?
• 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:
• Program model to move safely using sensors.
• Investigate effects of motor power when using sensors.

• SPIKE Prime sets ready for student use
• Devices with the SPIKE App installed
• Delivery Cart model, which was used in the Cart Control lesson
• Student journals
• Ruler

2-CS-02 Design projects that combine hardware and software components to collect and exchange data.
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.