SPIKE™ Prime with Python

Automate the Clean Up

Create a sorting robot and program it to use functions for an automated clean-up process

120-90 دقيقة.
الصفوف 6-8

Questions to investigate

• How can functions be used in programming for efficiency?


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


(Group Discussion, 10 minutes)

Engage students in a discussion about how to automate the clean-up effort.

Discuss with students the ways that they need to work to keep the environment clean. Think of ways that this can be automated using robotics to help keep things clean. Discuss that some materials are trash, but many can be recycled. Have students identify various materials that would be considered trash and items that could be recycled.

Prompt students to think about ways to sort the materials into trash and recyclable materials.


(Small Groups, 45 minutes)

Students will investigate how to automate clean up and sorting materials into trash and recycling.

Direct students to build the Automate It! model. The building instructions are 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 program their sorting robot to identify if a material is trash or recyclable. Students should create a function for checking the color of materials then sorting the recyclable materials into the blue area in front of the robotic sorter while throwing the trash away.
Note: Students will need to assign a color to trash and a color to recycle. The colors used in the building instructions are yellow and violet. Consider letting students change these to other colors if they want.

Sample Program:

from spike import PrimeHub, ColorSensor, App, Motor
from spike.control import wait_for_seconds, wait_until, Timer

hub = PrimeHub()


def check_color():
    if color_sensor.get_color()=='violet':
        for x in range(3):
            arm_motor.run_for_degrees(-100, speed=100)
            arm_motor.run_for_degrees(100, speed=100)







Allow students time to create and run their program. Remind students to watch the console for error messages during their programming. Students may need to debug their program as they test it.


(Whole Group, 15 minutes)

Discuss with students how the program worked.
Ask students questions like:
• Why did we use a function in this program? How did it make the program more efficient?
• What are other ways that functions could be used in your program?
• If students included parameters, what parameters were used and why?
• Show students the sample program used here and ask them to provide code comments (using the #) that should be included to make the program easier to ready.


(Small Groups, 25 minutes)

Challenge students to modify their program to include a second function in order to make their program more efficient.

Discuss the idea of making programs more efficient with students. Taking time to define variables, functions, etc. at the beginning of the program can allow students to make the body of the program shorter and easier to execute.

Discuss ways to make the program more efficient. One way could be to include a second function that will help the robotic checker know what to do with the materials after they are checked. Students should try to reduce the lines of code being used after their function is defined. If there are currently 12 lines of code after the function, could they cut that in half with a new program. Challenge students to see how efficient they can make their program. Remind students to include their code comments


(Group Exercise, 15 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• What happened when you added an additional function to your program?
• Why is efficiency important to creating large programs?
• What are different ways to use functions in your programs? What did you learn from how other students approached this challenge?

Have students answer the following in their journals:
• What did you learn today about making programs efficient?
• 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:
• Program a sorting robot to identify if a material is recyclable or non-recyclable
• Incorporate a second function into a program to make the program more efficient

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

2-CS-02 Design projects that combine hardware and software components to collect and exchange data.
2-AP-13 Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
2-AP-14 Create procedures with parameters to organize code and make it easier to reuse.
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.

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.