Students learn how to import libraries in Python and why this is important when connecting software and hardware.
Questions to investigate
How can engineers and computer programmers work together to create a way to communicate ideas to others?
- 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.
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.
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 spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
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
spike import _______
- the blank will be filled in with the hardware item.
- PrimeHub is the hub
- LightMatrix 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
- StatusLight is the light color surrounding the center button
- ForceSensor refers to the force sensor which contains a black button to push in
- MotionSensor is contained within the hub, like a gyro
- Speaker is on the outside of the hub
- ColorSensor is the small square color sensor that has one light
- App is the ability to play sounds
- DistanceSensor is the rectangular ultrasonic sensor that appears to have two “eyes”
- Motor refers to any size motor
- MotorPair refers to two motors that will work in tandem.
Have students return all materials to the SPIKE Prime set and correctly place back into the proper areas.
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?
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 spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair from spike.control import wait_for_seconds, wait_until, Timer hub = PrimeHub() hub.light_matrix.show_image('HAPPY')
Have students look carefully at which pieces of hardware are available to use in this code. Discuss the imported libraries with students.
Note: Students might need to close the Knowledge Base panel that is open to read the entire code. To close the Knowledge Base, click the two lines on the center left of the Knowledge Base area. The Knowledge Base slides right and closes. To reopen, click the two lines and the Knowledge Base will slide left to appear.
Allow students to run the sample code by selecting the play button (yellow circle with white triangle) at the bottom of the screen. A smiley face should appear on the hub screen.
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 light is on the hub icon, then it is connected. If a red light is on the hub icon, then the hub is disconnected.
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 line 1 in the code.
Ask students to refer to line 4 of the code. What is the hub called during programming?
- The word “hub” is now equal to the word “PrimeHub.” Why did they do that? To make programming a bit simpler - you type 3 letters rather than 8.
- 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 the Getting Started information and then select “Writing a Python Program”. 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 input different shapes like SQUARE, TRIANGLE in the place of “HAPPY” to see how the program changes. What other shapes did they find that worked?
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.
- Materials are not all located in their correct tray; some parts are still together.
- Materials are located correctly, but only one person helped put things away.
- Both partners worked together and all parts in the correct locations.
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 student to rate themselves on a scale of 1-3, on their materials (parts) management today.
Suporte ao Professor
- 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
Design projects that combine hardware and software components to collect and exchange data.
Incorporate existing code, media, and libraries into original programs, and give attribution.
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
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
Follow precisely a multistep procedure when carrying out experiments, taking measurements, or performing technical tasks
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