SPIKE™ Prime with Python

Get Moving to Get Data

Investigate gathering, displaying, and using data

45 min
Beginner
Years 7-9 OR Key Stage 3

Questions to investigate

• How can you gather and display data?

Prepare

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

Engage

(Group Discussion, 5 minutes)

Engage students in a conversation about transportation and ways that people get around.

Prompts students to share the different ways they get from one place to another. Have them think about what types of transportation they take regularly. Discuss the situations that require the use of different types of transportation.

Provide different examples of places to discuss what transportation types might be used in each place. Be sure to include walking, biking, different types of vehicles, boats, planes, etc.

Ask students to consider when they want to get to these places what is important information or data to have. Discuss ideas like knowing the distance, how long it will take, and how fast they need to move.

Explore

(Small Groups, 20 minutes)

Students will investigate ways to take in data from sensors and manage their data.

Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for Smart Kettlebell 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.  

Prompt students to think about how to use their sensor to take in information. Students should look at the upper left area of the programming canvas. Point out where the live sensor data is displayed. Ask students to move their model around and press the force sensor to see the live data stream. Discuss how moving the model provides different data. Have students indicate ways that they could program the distance sensor to provide data.

Ask students to review the three sample programs. Discuss which will be the best to gather and manage data in the console. Allow students to investigate the programs as needed.

Sample Program 1:

from hub import port
import runloop
import distance_sensor

async def main():
    #read the distance from the sensor
    dist_mm = distance_sensor.distance(port.F)
    print('mm:', dist_mm)  

runloop.run(main())

Sample Program 2:

from hub import port
import runloop
import distance_sensor

async def main():
    #read the distance from the sensor
    while True:
        dist_mm = distance_sensor.distance(port.F)  
        print('mm:', dist_mm)

runloop.run(main())

Sample Program 3:

from hub import port
import runloop
import distance_sensor
import force_sensor

async def main():
    #read the distance from the sensor
    while True:
        if force_sensor.pressed(port.B):
            dist_mm = distance_sensor.distance(port.F)  
            print('mm:', dist_mm)
            await runloop.sleep_ms(1000)

runloop.run(main())

Allow students time to investigate the three programs. Please note that the distance sensor returns a value in millimeters, not centimeters. Students can convert from millimeters to centimeters by changing this line of code by adding a /10 at the end to divide by 10.

dist_cm = distance_sensor.distance(port.F)/10

from hub import port
import runloop
import distance_sensor
import force_sensor

async def main():
    #read the distance from the sensor
    while True:
        if force_sensor.pressed(port.B):  
            dist_cm = distance_sensor.distance(port.F)/10
            print('cm:', dist_cm)
            await runloop.sleep_ms(1000)

runloop.run(main()) 

Explain

(Whole Group, 5 minutes)

Discuss with students how each program worked. Ask students questions like:
• What did the data output in the console look like for each program?
• Which program would provide the best way to gather different data points?
• What are some ways that you could organize your data outside the console?

Prompt students to display their data in a table.

Remind students that there are different data types: integer, float, and string. Consider referencing the Knowledge Base under the distance sensor to help students decide what type of data is being used here. Discuss what type of data has been provided from their sensor.

Elaborate

(Small Groups, 10 minutes)

Challenge students to create a new program that will provide data using the force sensor.

Ask students to look at the ways that they can get data from the force sensor. Discuss ideas for getting the number of Newtons from pressing the force sensor and displaying the data so students can easily gather and organize the information into a table. Consider ideas for displaying in the console or on the hub.

Allow students time to explore and develop their program. Encourage students to share their ideas and data with other groups.

Evaluate

(Group Exercise, 5 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• What is the difference between the three sample programs you investigated? What is the same?
• What happened when you were adding the force sensor?
• What are some ways you can display data to reference or capture to create a table?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about gathering data using your robot?
• 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:
• Investigate ways to take in data from sensors
• Create a new program that will provide data using the force sensor

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