STARLEAP / SPIKE PRIME PYTHON / 2026–27
Build 2026.09.22-090630-0400
On this page
Project 08: Driving Base 2 Parking Zone
You need: a LEGO SPIKE Prime set and the SPIKE App Python editor.
Goal
Make a driving robot park inside a target zone near a wall or bin.
Build
In the SPIKE App, open Build > Driving Base 2. Follow the Driving Base instructions, then the Tools and Accessories instructions through the distance-sensor attachment (Steps 19-22). Keep the sensor facing forward with a clear view of the wall. The parking program uses the two wheel motors and the distance sensor; it does not operate the grabber or force sensor.
Ports for this build (left and right as the robot faces forward):
- left wheel motor: Port C
- right wheel motor: Port D
- distance sensor: Port F
Match the wires to these ports and to the variables in your code. Keep this build assembled for Project 09.
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
- Use two cutoff numbers: too close and too far.
- Make the robot move forward, reverse, or stop based on distance.
- Stop the loop with
breakwhen the robot succeeds.
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: Print Distance While Paused
Open a new Python project named Parking Zone.
Start with this short code:
from hub import port
import runloop
import distance_sensor
distance_port = port.F
async def main():
while True:
distance = distance_sensor.distance(distance_port)
print(distance)
await runloop.sleep_ms(500)
runloop.run(main())
Run the program and test the distance sensor.
Observe: You should see distance numbers in the console.
Step 2: Add The Motors
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_pair
import distance_sensor
left_wheel = port.C
right_wheel = port.D
distance_port = port.F
too_close = 80
too_far = 160
async def main():
motor_pair.pair(motor_pair.PAIR_1, left_wheel, right_wheel)
while True:
distance = distance_sensor.distance(distance_port)
print(distance)
if distance == -1:
motor_pair.stop(motor_pair.PAIR_1)
print("no reading")
elif distance > too_far:
motor_pair.move(motor_pair.PAIR_1, 0, velocity=200)
elif distance < too_close:
motor_pair.move(motor_pair.PAIR_1, 0, velocity=-200)
else:
motor_pair.stop(motor_pair.PAIR_1)
print("parked")
break
await runloop.sleep_ms(100)
runloop.run(main())
Run the program with a wall, box, or bin in front of the robot.
Observe: The robot should move until it is between the two cutoff numbers.
Step 3: Add A Success Signal
Add this import:
from hub import light_matrix, port
Replace the old from hub import port line with that one.
Add this before break:
else:
motor_pair.stop(motor_pair.PAIR_1)
print("parked")
light_matrix.show_image(light_matrix.IMAGE_YES)
break
Run the program.
Physical Challenge: Park In The Zone
Make a parking zone with two LEGO beams or tape marks.
- Level 1: stop before hitting the wall
- Level 2: stop inside the zone
- Level 3: start farther away and still stop in the zone
- Level 4: park successfully three times in a row
Tune one value at a time:
too_closetoo_far- forward velocity
- reverse velocity
Extra setup idea: Use the Parking Wall setup from the Challenge Setups sheet.
Teacher Check
Show your teacher:
- the distance sensor reading
too_closetoo_far- the robot stopping inside the zone
Save this working program. Keep Driving Base 2 assembled for Project 09: Parking Approach Alarm, which starts with a fresh code outline.
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.