SPIKE™ Prime with Python

Start Sensing

In this lesson, students will explore conditional statements using the force sensor and how to program sensors.

45 Min.
Klassen 5–8

Questions to investigate

• How can sensors interact with or control motors?


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


(Whole Class, 5 minutes)

Start a discussion with students in thinking about how sensors work.

Have students play a game of freeze dance. Hold up a force sensor and explain to students that they can only move when you are holding the sensor button down. Be sure to make it obvious when you are pushing and releasing the button. Students can move in any way they like when it is time to dance but must freeze when you release the button.

Discuss with students how the sensor was used to provide information on how they should move. Ask students to explain how they think sensors work.


(Small Groups, 20 minutes)

Students will explore working with sensors by coding the force sensor.

Direct students to the START section in the SPIKE App. Here students can access 2. Motors and Sensors. This getting started lesson provides students with an early experience in building and coding with SPIKE Prime. Use this lesson to guide students in building the motor and force sensor. Students should complete the building instructions through steps 1-7 and 22-29. You will not need to complete the rest of this lesson.

Direct students to leave the Motors and Sensors lesson.

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.

Push, Start, Stop
Sensors can be used in a variety of ways. The force sensor can be used like a “button” you can program to start and stop actions.

Brainstorm with students how they think you should program the sensor to work. What information will the software need to run the hardware correctly? Write a pseudocode program for making the force sensor work to start and stop the motor when pushed. Pseudocode is writing in words what you want the program to do.
An example could be:
• Import force sensor, motor, and hub
• Initialize motor
• Initialize force sensor
• Wait until force sensor pressed
• Motor turn on
• Move clockwise for time 2 seconds
Note: The code you write in steps (pseudocode) does not need to match exactly what will be put in the program. Pseudocode helps students think through what code is needed.

Provide students with the sample code to use the force sensor to stop and start the motor. Students can type this program into the programming canvas. Students can also copy and paste this program into the programming canvas by accessing the Knowledge Base Getting Started Part 3: Using the Force Sensor section.  

Ask students to run the program.

from spike import ForceSensor, Motor

\# Initialize the Force Sensor and a motor
force_sensor = ForceSensor('B')
motor = Motor('C')

\# Press the button slowly, it will work only once
# Play the program again to try it again

Discuss with students what happened when they ran the code. Students should identify that when the force sensor was pushed, the motor started running at 25% power. The motor continued to run until the force sensor was released, which caused the motor to stop. Review the program with the students to ensure they understand each line of code.

Point out to students how the force sensor was named. Explain to them that we need to be thoughtful of how to name parts of the program like variables. We are creating a variable to store the information about the port we are using for the force sensor and setting an easily recognized name to use in the program itself.

Prompt students to think about how to change this code to start the motor running, then stop it when the button is pushed. Ask students to change their code and run the program.

\# Press the button slowly, it will work only once

Note: Students need to change the code to move the motor.start before the force sensor and take out the when released line of code.

Allow students additional time to explore with their force sensor and motor.


(Whole Class, 5 minutes)

Have students share their new code and discuss how they used the force sensor.

Ask students questions like:
• How can you use a sensor to control the motors’ actions?
• What are different ways that you can use or program a force sensor?
• What does the “wait_until” mean when you are programming the force sensor?
• Why do we not set a time or distance for the motor to move when we start the motor?
• What are some errors you think might occur with the force sensor?


(Small Groups, 10 minutes)

Challenge students to try something new with their force sensor. Ask students to open the console. Students can print messages in the console using the print() function.
Introduce a new line of code to students. They can modify their previous code or type it in.

from spike import ForceSensor, Motor

\# Initialize the Force Sensor and a motor
force_sensor = ForceSensor('B')
motor = Motor('C')

\# Press the button slowly, it will work only once

Discuss with students what happens when the new line of code “print('hello!')” is run.

Now students can include messages with their physical robot as it moves. Students can use the print function to document what is happening in the program as an output. It can also be fun.

For debugging help, remind students that the printed messages have to be included in parenthesis ( ) with quotation marks – (‘ ‘) or (“ “) works.

Challenge students to create a question and answer game where one person pushes the button to answer a question and then the right answer pops up (or is printed) on screen.


(Whole Class, 5 minutes)

Teacher Observation
Discuss the program with students.
Ask students questions like:
• What are some ways you were able to program the force sensor to work?
• What are some ways that the sensor and motor can interact?
• What are some ideas you have for using the print function?

Have students answer the following in their journals:
• What did you learn today about using a force sensor?
• When can you use the print function?
• 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 student to rate themselves on a scale of 1-3, on their materials (parts) management today.

Unterstützung für Lehrkräfte

Students will:
• Program the force sensor.
• Create conditional statements.

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

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-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.