STARLEAP / SPIKE PRIME PYTHON / 2026–27

Build 2026.09.22-090630-0400

On this page

Project 04: LEGO Bin Rectangle

You need: a LEGO SPIKE Prime set and the SPIKE App Python editor.

Goal

Tune a driving robot so it goes around the LEGO bin in a rectangle.

This is a core class challenge. It will probably not work on the first try. That is the point.

Build

Use a Driving Base, Hopper-style vehicle, or any two-motor driving robot.

Use these starting ports unless your build uses different ones:

Put a LEGO bin on the floor or table. Leave enough space around it for the robot to turn.

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

Motor degrees measure wheel rotation, not the angle of the whole robot. A turn_degrees value of 120 is a starting test value; tune it until the chassis makes a corner. Start from the same floor mark for each test.

Before you run: use a clear floor area away from table edges. Test the wheel direction with the wheels lifted, then stop before placing the robot on the floor.

Step 1: One Side And One Turn

Open a new Python project named Bin Rectangle.

Start with this short code:

CODE
from hub import port
import runloop
import motor_pair

left_wheel = port.E
right_wheel = port.F

long_side = 540
short_side = 360
turn_degrees = 120
drive_speed = 400
turn_speed = 300

async def main():
    motor_pair.pair(motor_pair.PAIR_1, left_wheel, right_wheel)
    await motor_pair.move_for_degrees(
        motor_pair.PAIR_1, long_side, 0, velocity=drive_speed,
    )
    await motor_pair.move_for_degrees(
        motor_pair.PAIR_1, turn_degrees, 100, velocity=turn_speed,
    )

runloop.run(main())

Run the program.

Observe: The robot drives one side, then turns.

Step 2: Repeat The Pattern

Replace only main() with this version. Each repetition drives a long side, turns, drives a short side, and turns again. Keep the imports, variables, and final runloop.run(main()) line.

EDIT VIEW · Gray = already there; dark = add or change
async def main():
    motor_pair.pair(motor_pair.PAIR_1, left_wheel, right_wheel)

    for pair in range(2):
        await motor_pair.move_for_degrees(
            motor_pair.PAIR_1, long_side, 0, velocity=drive_speed,
        )
        await motor_pair.move_for_degrees(
            motor_pair.PAIR_1, turn_degrees, 100, velocity=turn_speed,
        )
        await motor_pair.move_for_degrees(
            motor_pair.PAIR_1, short_side, 0, velocity=drive_speed,
        )
        await motor_pair.move_for_degrees(
            motor_pair.PAIR_1, turn_degrees, 100, velocity=turn_speed,
        )

Run the program.

Observe: Two repetitions make four sides and four turns. Unlike a square, this route has two separately tunable side lengths.

Step 3: Tune One Number At A Time

If the robot hits the bin or drifts away, decide which part is wrong:

Change one value, run, then change again.

Step 4: Add A Start Signal

Add this import:

CODE
from hub import light_matrix, port

Replace the old from hub import port line with that one.

Add this at the start of main():

EDIT VIEW · Gray = already there; dark = add or change
async def main():
    await light_matrix.write("GO")

Run the program.

Physical Challenge: Around The LEGO Bin

Extra setup idea: Try the Line Ladder or Crater Turn Course from the Challenge Setups sheet.

Teacher Check

Show your teacher:

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.