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
Beginner
Grades 6-8

Questions to investigate

• How can sensors interact with or control motors?

Prepare

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

Engage

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

Explore

(Small Groups, 20 minutes)

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

Direct students to the Getting Started section of the knowledge base in the SPIKE App. Here students can access 7. Sensor Control. This gives information and an example of how you can use the force sensor with the SPIKE Prime. Use this information to guide students in connecting the motor and force sensor to the hub.

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
• receive input from force sensor
• 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.

Ask students to run the program.

import runloop
import motor
import force_sensor
from hub import port

def is_force_sensor_pressed():
    # collect input from force sensor
    return force_sensor.pressed(port.B)
    
def is_force_sensor_not_pressed():
    # collect input from force sensor
    return not force_sensor.pressed(port.B)
    
async def main():
    # wait until the force sensor is pressed
    await runloop.until(is_force_sensor_pressed)
    
    motor.run(port.A, 750)
    # wait until the force sensor is not pressed
    await runloop.until(is_force_sensor_not_pressed)
    
    motor.stop(port.A)
    
runloop.run(main())

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 750 degrees per second. 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.

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.

import runloop
import motor
import force_sensor
from hub import port

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

async def main():
    # turn on motor
    motor.run(port.A, 1000)

    # wait until the force sensor is pressed
    await runloop.until(is_force_sensor_pressed)
    
    # stop motor
    motor.stop(port.A)

runloop.run(main())

Note: Students need to change the code to move the motor.run line before the until line. This will start the motor running and then wait until the forec sensor is pressed. Also notice that the function is force sensor not pressed is not needed.

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

Explain

(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?
• 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?

Elaborate

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

import runloop
import motor
import force_sensor
from hub import port

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

async def main():
    # turn on motor
    motor.run(port.A, 1000)

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

    # stop motor
    motor.stop(port.A)

    # print Hello! in the console
    print('Hello!')

runloop.run(main())

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.

Evaluate

(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?

Self-Assessment
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 students to rate themselves on a scale of 1-3, on their materials (parts) management today.

Teacher Support

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

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

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