STARLEAP / SPIKE PRIME PYTHON / 2026–27
Build 2026.09.22-090630-0400
On this page
Project 15: Package Sorter
You need: a LEGO SPIKE Prime set and the SPIKE App Python editor.
Goal
Use a color sensor to sort packages or bricks.
Build
Build Package Sorter, Quality Check Robot, or a simple one-motor sorting gate.
Starting ports:
- color sensor: Port A
- sorter motor: Port F
Change the labels if your build uses different ports.
Using the code: gray lines are already in your program; dark lines are the edit. Keep the shown indentation. Replace a displayed main() definition as a whole, keeping imports and the final start call.
What You Will Learn
- A color reading can choose a motor direction.
- A reset function returns the sorter to the middle.
- A loop keeps the sorter ready for the next package.
Before you run: with the program stopped, align the sorter motor to its zero marker and put the gate in the center. Feed one package at a time and remove it after sorting, before the next reading.
Step 1: Read A Color
Open a new Python project named Package Sorter.
Start with this short code:
from hub import port
import runloop
import color_sensor
color_port = port.A
async def main():
while True:
print(color_sensor.color(color_port))
await runloop.sleep_ms(1000)
runloop.run(main())
Run the program with different colored LEGO pieces.
Observe: The console reading changes.
Step 2: Sort Two Colors
Update your existing program to match this version. Keep the gray reference lines; add or change the dark lines:
from hub import port
import runloop
import motor
import color_sensor
import color
color_port = port.A
sorter_motor = port.F
async def sort_left():
print("sort left")
await motor.run_for_degrees(sorter_motor, -90, 300)
async def sort_right():
print("sort right")
await motor.run_for_degrees(sorter_motor, 90, 300)
async def reset_sorter():
await motor.run_to_absolute_position(sorter_motor, 0, 300)
async def main():
while True:
package_color = color_sensor.color(color_port)
if package_color == color.RED:
await sort_left()
await reset_sorter()
elif package_color == color.BLUE:
await sort_right()
await reset_sorter()
else:
print("waiting")
await runloop.sleep_ms(1000)
runloop.run(main())
Run the program.
Observe: Red and blue should make different sorter movements.
Step 3: Add A Third Color
Add one more elif:
if package_color == color.RED:
await sort_left()
await reset_sorter()
elif package_color == color.BLUE:
await sort_right()
await reset_sorter()
elif package_color == color.YELLOW:
print("yellow")
else:
print("waiting")
Run the program with a yellow piece. Observe: the console prints yellow. Then choose a movement, add its function call inside this branch, and run the program again.
Physical Challenge: Sorting Station
- Level 1: detect two colors
- Level 2: move left for one color and right for another
- Level 3: reset after each package
- Level 4: sort five packages in a row
Teacher Check
Show your teacher:
- the color sensor reading
- the
ifandelif - the sorter moving differently for two colors
Before You Put The Kit Away
Save your program. Stop the program before moving wires or changing the build. Show the teacher your working behavior, point to one changed line, and explain what it controls. Keep your place for next time.