SPIKE™ Prime with Python

Importing Libraries

Students learn how to import libraries in Python and why this is important when connecting software and hardware.

45 min
Beginner
Years 7-9 OR Key Stage 3

Questions to investigate

How can engineers and computer programmers work together to create a way to communicate ideas to others?

Prepare

  • Determine how students will be grouped (2 students work with one SPIKE Prime set).
  • Determine your student expectations for teamwork.
  • Check to make sure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.

1. Engage

Ignite a discussion with students. Think about what is needed when you follow a recipe to cook. You can go to the store to purchase the ingredients you need. When you get home, you need to be sure you have the tools you need to prepare the food. Think about what you might need. You might need a stove or an oven. You might need a mixer and a spatula.
When writing a Python program, you will need to make sure you “gather” all the things you will use before you start to make a model run.

2. Explore

Students will learn what a library is and how and why you import libraries.

When using Python to program, students must import the libraries, the words, or terms, that signal the program to recognize the hardware that can be used. Because Python is a text-based coding language, capitalization and punctuation are important. For the SPIKE App to communicate with the SPIKE Prime components, you will need the right libraries that link the hardware components to the software.

Prompt students to open their SPIKE Prime sets and locate all the hardware pieces. Pull each hardware item out of the set and lay them on the table (1 Hub, 3 motors, 3 sensors).

Ask students to identify each piece of hardware.

Review each hardware item. Hold up each hardware piece and ask students to locate the same piece. Turn on the hub by pressing the large center button.

Ask students how they think they would import each piece of hardware into the software.

Show the introductory program in the SPIKE App Python canvas.

from hub import light_matrix
import runloop


async def main():
    # write your code here
    await light_matrix.write("Hi!")


runloop.run(main())

Introduce students to the line for importing a hardware piece which is called a library.

Ask students to identify the hardware that matches the items in the import code
- import _______
- the blank will be filled in with the hardware item.

Note

  • light_matrix is the 5x5 grid on the hub
  • Button refers to the three buttons located at the bottom on the front of the hub - left, center and right buttons
  • light is the light color surrounding the center button
  • force_sensor refers to the force sensor which contains a black button to push in
  • motion_sensor is contained within the hub, like a gyro
  • speaker is on the outside of the hub
  • color_sensor is the small square color sensor that has one light
  • App is the ability to play sounds
  • distance_sensor is the rectangular ultrasonic sensor that appears to have two “eyes”
  • motor refers to any size motor
  • motor_pair refers to two motors that will work in tandem.

Have students return all materials to the SPIKE Prime set and correctly place them back into the proper areas.

3. Explain

Ask students questions like:

  • Why do you think it is important to import a library before you begin writing code?
  • What can be imported for use with the hub? How are they imported?
  • How are motors imported?

4. Elaborate

Students will practice importing libraries and see what importing the different types of libraries looks like.

Ask students to open a new project in their SPIKE App, selecting Python as the programming type. Students should connect their hub.

Students will see the program already written in the canvas.

from hub import light_matrix
import runloop

async def main():
    # write your code here
    await light_matrix.write("Hi!")

runloop.run(main())

Have students look carefully at which pieces of hardware are available to use in this code. Discuss the imported libraries with students.

Allow students to run the sample code by selecting the play button (yellow circle with white triangle) at the bottom of the screen. The light matrix should scroll Hi! Across the display.

Tips for troubleshooting: Ensure the hub is connected properly. On the programming canvas in the upper left corner is an icon of the hub. If a green ring is around the hub icon, then it is connected with the USB cable. If a blue ring is around the hub icon, then it is connected with Bluetooth. If the hub icon is yellow, then the hub is disconnected.

Start Importing
To start learning how to import libraries, ask students to locate the Knowledge Base on the right-side panel.

Look at the code on screen. What libraries have been imported? Have students reference lines 1 and 2 in the code.

Ask students to refer to line 64 of the code. What is being used during programming?

  • What do you think the program is going to do? Talk with your partner and then start your program.

Refer students to the Getting Started section of the Knowledge Base. Read 1. Introduction to Python and then review the importing libraries information with students.

  • Ask them what it means when they read, “The imported libraries are located at the beginning of the .py file and should appear only once in the program.”
  • Students may not know what a .py file is. The .py file is the Python program that is being coded. “.py” stands for Python.
  • You do not see .py anywhere on the canvas because the .py file contains everything on the canvas.
  • The name of the file or project is listed at the top left of the screen next to the picture of a house. If this is the first project created, the name of the Project is likely Project 1.

Ask students to open a new project using Python. Have students try different phrases to see how the program changes.

5. Evaluate

Teacher Observation:
Discuss the program with students.

Ask students questions like:

  • What happened on your hub when you ran the program?
  • What libraries did you use to run this program?
  • Why do you need to import different parts of the hub?
  • How can engineers and computer programmers work together to create a way to communicate ideas to others?

Remind students that they are responsible for materials management. Parts should not be shared between sets. If a part is missing, ask the teacher. Remember, the teacher has limited spare parts. Let the teacher know immediately if you cannot locate something.

Each day, students will make a journal entry about the materials management of their set. They grade themselves using a three-point scale. The goal is for all team members to obtain 3 points.
Listed below is the self-scoring guide.

  1. Materials are not all located in their correct tray; some parts are still together.
  2. Materials are located correctly, but only one person helped put things away.
  3. Both partners worked together and all parts in the correct locations.

Self-Assessment:
Have students answer the following in their journals:

  • Why do you need to import libraries at the beginning of a Python program?
  • 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:

  • Learn why a Python program must have libraries imported.
  • Import libraries.
  • Run a program.

LEGO® Education SPIKE Prime Set
Device with the LEGO Education SPIKE App installed
Any other relevant material for a project

CSTA
2-CS-02
Design projects that combine hardware and software components to collect and exchange data.

2-AP-16
Incorporate existing code, media, and libraries into original programs, and give attribution.

CCSS ELA
SL.8.1
Engage effectively in a range of collaborative discussions (one-on-one, in groups, and teacher-led) with diverse partners on grade 8 topics, texts, and issues, building on others' ideas and expressing their own clearly

SL.8.4
Present claims and findings, emphasizing salient points in a focused, coherent manner with relevant evidence, sound valid reasoning, and well-chosen details; use appropriate eye contact, adequate volume, and clear pronunciation

RST.6-8.3
Follow precisely a multistep procedure when carrying out experiments, taking measurements, or performing technical tasks

L.8.6
Acquire and use accurately grade-appropriate general academic and domain-specific words and phrases; gather vocabulary knowledge when considering a word or phrase important to comprehension or expression

Vocabulary
Library
Import