SPIKE™ Prime with Python

Communicating with Light

Students learn how to control the light matrix to show images and write words.

45 min
الصفوف 6-8

Questions to investigate

  • How can technology help us solve problems?
  • How can you plan for the use of hardware in a solution to a problem?


Check to make sure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.

1. Engage

What shapes can you create in a 5x5 grid? Could you make a rectangle? A triangle? What types of faces could you make?

Why would you create on paper before you code?

Students will investigate programming lights on the hub.

  • Have students draw a 5x5 matrix in their journals.
  • Ask students to cut 25 small squares of colored paper to fit inside their matrix.
  • Draw a simple image or show a picture to the students that could be created on the light matrix on the hub. Examples could include a square or heart.
  • Ask students to consider how to create an image on their hubs. Ask them to create an image or picture by placing their squares onto the matrix to represent what squares will be lit. They do not need to use all 25 paper squares at the same time.

2. Explore

Students will explore how to create images on the light matrix.

Prompt students to consider how to break this task down into steps that a computer could do. Ask students to write the steps in pseudocode in their journals. Encourage them to think about previous coding experiences to think about the steps needed. Pseudocode is the set of directions written in words that show step-by-step instructions for what should happen. Pseudocode is used to help write code.

Hint: What libraries will they need to import?

Allow students to share their ideas.

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.

Students can follow the lesson through the Knowledge Base Getting Started Part 1: Programming Simple Outputs. This lesson utilizes the Light Matrix part of the getting started. Open the Console by clicking the two short horizontal lines at the bottom center of the canvas.
Provide students with the sample code below. Have students type in the code to the programming canvas. Students can also copy and paste the sample code from the Knowledge Base Getting Started Part 1: Programming. Ask students to run this program.

# Import the PrimeHub class
from spike import PrimeHub

from spike.control import 

# Initialize the Hub 
your_hub = PrimeHub() 

# Light up a smiley face 

Discuss with students what happens.

Ask students if the program is still running on their hub. If the smiley face is still showing on the hub, then the program is still running. Direct students to stop the program by either clicking the red circle with the white square on the lower right side of the screen or pressing the large circle button on the hub.

Review the lines of code with students. Discuss what they see happening when they run the program and what each line of code is telling the hub to do.

Create a New Image
Have students change the image that is displayed on the light matrix.

Challenge students to change the display from HAPPY to HEART which will change the display on the hub. Ask students to indicate what needs to be changed in the code to make it display a heart instead of a smiley face. Discuss with students.

Have students change their code and the comment in green. Run the program.

Light up a heart


Discuss the program with students. Ask students what the # signals in the program. The # signals a comment which is not a command for the computer, but is something programmers add to help them remember what a section of code is supposed to do.

Review with students how long did the heart image stay lit. The image was lit for 5 seconds because the wait_for_seconds show a “5” in the parenthesis immediately after the command. The value inside the parenthesis is the number of seconds.

3. Explain

Discuss the program with students. Ask students questions like:

  • Which libraries do you need to import to run these programs?
  • What is the purpose of the wait_for piece of the code?
  • Do you need to rewrite the entire code each time to change the image that is shown?
  • How do you recognize if a program is still running? What are ways to stop a program?

4. Elaborate

Investigate programming a word on the hub.

Display Two Images
Have students create two images to display on the light matrix.
Challenge students to copy the lines of code under #Light up a heart and paste it at the end of their code. Students will now have two images to code.

Have them insert HAPPY into the second section. Discuss with students what will happen when they run their code with both sections of code. Review the lines of code as a group. The code should show a heart for 5 seconds followed by a smiley face for 5 sections.

Light up a heart


Light up a smiley face


Discuss the program with students.


Investigate how to read errors.
Ask students to click the two horizontal lines in the center at the bottom of the programming canvas. This allows the console to open. The console is where information or error messages are printed.

Ask students to change their HEART to Heart and run the program again.

Light up a heart


Light up a smiley face


Ask students to explain what happened. This represents an error which can occur if the information is not inputted correctly.

Notice that the error message in the console point you to line 7. If you look at line 7, you can see the word Heart in not all in capital letters. Change Heart to HEART and run the program again.

Notice that the program works correctly but the error message in the console remains unchanged. If you make an additional error, the next message in the console will follow exactly underneath the previous one.

Explain to students how to tell you have a new message. You will see a group of numbers, for example, [14:14:27.303] in black and the rest of the error message in red. This is the beginning of the error message and tells the time the code was executed.

Additional Exploration

Allow students to explore all the options for displaying images on the hub. To access additional images that students can display on their hub light matrix, look in the Knowledge Base under Light Matrix Actions show_image(). Allow students time to explore changing the images shown as time allows.

Have students look at the code they have been working with and consider how to change this code to write a word out on the hub rather than just showing an image.

Students should recognize that they will still need to import and initialize the hub. However, they need to code the hub light matrix to write instead of showing an image. Students might initially think that they need to show the word as an image. Prompt students to think about writing words out rather than showing like a picture.

Ask students to write the work hello on their hub light matrix.

Import the PrimeHub class

from spike import PrimeHub
hub = PrimeHub()

Show the number 1 on the Light Matrix


Ask students to reflect on the difference in this code from the previous code used to show images. This code allows students to write text on the light matrix, one letter at a time, scrolling from right to left. This is a string, or an array of characters. For example, the word “Hello” consists of a string of characters “h – e – l – l – o.” Five characters are in this string.

Challenge students to write their names on the hub using this code. What is the only thing that students need to change in the code?

Optional: Allow students to explore writing various words and challenge them to write a word and then show an image.

For additional reference, point students to the Knowledge Base under Light Matrix Actions write().

5. Evaluate

Teacher Observation:
Discuss the program with students.

Ask students questions like:

  • What is the purpose of the wait_for_seconds?
  • What would happen if you left off the wait_for_seconds from your program?
  • What troubleshooting or debugging did you run into?
  • What is the function of the green words starting with the # sign in your code?
  • What is the difference between coding with “image” and coding with “write”?

Have students answer the following in their journals:

  • What did you learn today about programming the light matrix?
  • 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.

دعم المعلم

Students will:

  • Describe the function of hardware and software.
  • Program the light matrix and learn how to debug a simple program.
  • SPIKE Prime sets
  • Device with SPIKE App installed
  • Student journal
  • Sticky notes
  • Scissors
  • Colored paper

Design projects that combine hardware and software components to collect and exchange data.

Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.

Incorporate existing code, media, and libraries into original programs, and give attribution.

Document programs in order to make them easier to follow, test, and debug.

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

Light Matrix

LEGO, the LEGO logo, the Minifigure, DUPLO, the SPIKE logo, MINDSTORMS and the MINDSTORMS logo are trademarks and/or copyrights of the LEGO Group. ©2022 The LEGO Group. All rights reserved. Use of this site signifies your agreement to the terms of use.