STARLEAP / SPIKE PRIME PYTHON / 2026–27
Build 2026.09.22-090630-0400
On this page
- Step 1 - Turn Once
- Step 2 - Change The Amount
- Step 3 - Come Back
- Step 4 - Pause At The Far End
- Step 5 - Name The Amount
- Step 6 - Name The Speed
- Step 7 - Prepare One Slow Movement
- Step 8 - Beep After The Movement Finishes
- Step 9 - Try The Motor Without Await
- Step 10 - Put Await Back And Compare
- Step 11 - Bring Back The Return Movement
- Physical Challenge And Teacher Check
Project 02: One Motor Mini Machine
You need: a LEGO SPIKE Prime set and the SPIKE App Python editor. Connect one motor to Port A. Attach a small pointer that can turn freely.
Make a pointer move out and back. Then use a beep to hear what await does. Start with an unloaded motor and keep fingers clear of moving parts. Stop the program before changing the attachment.
Step 1 - Turn Once
Open a new Python project named Mini Machine. Type the starter. Code inside main() has four leading spaces.
from hub import port
import runloop
import motor
pointer_motor = port.A
async def main():
await motor.run_for_degrees(pointer_motor, 360, 300)
runloop.run(main())
pointer_motor = port.A gives the port a meaningful name. Keep this variable at the top, after the imports. The movement line uses pointer_motor so you can read which part will move. If the wiring changes, update this one assignment.
Run the program. The motor should make one full turn and stop. The 360 sets motor degrees; 300 sets velocity in degrees per second.
Step 2 - Change The Amount
Replace only the movement line. Gray lines are already in your program.
async def main():
await motor.run_for_degrees(pointer_motor, 180, 300)
runloop.run(main())
Run the program. The motor should move half as far. Keep the speed unchanged so you can compare the amount of movement.
Step 3 - Come Back
Add the dark line after the first movement, with the same indentation.
async def main():
await motor.run_for_degrees(pointer_motor, 180, 300)
await motor.run_for_degrees(pointer_motor, -180, 300)
Run the program. The pointer should move out, then return. A negative degree value reverses the movement when the speed stays positive.
Step 4 - Pause At The Far End
Add a pause between the two moves.
async def main():
await motor.run_for_degrees(pointer_motor, 180, 300)
await runloop.sleep_ms(1000)
await motor.run_for_degrees(pointer_motor, -180, 300)
Run the program. The pointer should wait one second before coming back. Change only the pause to 500, then run the program again.
Step 5 - Name The Amount
Add a variable after the imports and before main().
pointer_motor = port.A
turn_degrees = 180
Replace both degree values with the name. Keep the minus sign on the return move.
async def main():
await motor.run_for_degrees(pointer_motor, turn_degrees, 300)
await runloop.sleep_ms(500)
await motor.run_for_degrees(pointer_motor, -turn_degrees, 300)
Run the program. The amount should stay the same. Change only turn_degrees = 180 to turn_degrees = 90, then run the program again. Both movements should now be quarter turns.
Step 6 - Name The Speed
Add the dark assignment beside turn_degrees, above main(). Keep both at the left edge:
turn_degrees = 90
speed = 200
Replace 300 in both movement lines with speed:
async def main():
await motor.run_for_degrees(pointer_motor, turn_degrees, speed)
await runloop.sleep_ms(500)
await motor.run_for_degrees(pointer_motor, -turn_degrees, speed)
Run the program. The pointer should move more slowly over the same amount.
Fix it: if nothing moves, check Port A, the motor import, and the final start call. If it cannot return, check the minus sign and make sure the attachment has room to move.
Step 7 - Prepare One Slow Movement
Save your working out-and-back program. For the next few tests, use a small pointer with room for a half turn. We will bring back the return movement in Step 11.
Change your amount and speed variables to these values. Keep pointer_motor unchanged. Do not add a second copy of either assignment:
turn_degrees = 180
speed = 90
Replace only main() with this version. Keep the imports and variables above it and the final runloop.run(main()) below it:
async def main():
await motor.run_for_degrees(pointer_motor, turn_degrees, speed)
await runloop.sleep_ms(3000)
motor.stop(pointer_motor)
Run the program. The pointer should make a slow half turn, then stay still during a three-second observation pause. The final stop tells the motor to stay stopped.
Keep these numbers unchanged through Step 10. The pause keeps the program running long enough to watch what happens in the comparison. It is a fixed amount of time, not a check that the movement has finished.
Step 8 - Beep After The Movement Finishes
Change the first import to include the hub's speaker:
from hub import port, sound
Add one beep immediately after the motor movement and before the pause:
async def main():
await motor.run_for_degrees(pointer_motor, turn_degrees, speed)
await sound.beep(700, 200, 100)
await runloop.sleep_ms(3000)
motor.stop(pointer_motor)
The beep comes from the hub. 700 sets its pitch, 200 sets its length in milliseconds, and 100 sets its volume.
Predict: Will the beep start while the pointer is turning or after it stops?
Run the program. Watch the pointer and listen.
Observe: The motor finishes its half turn, then the beep starts. await on the motor line makes main() wait for that movement command to finish before starting the next line. The await on the beep line waits for the beep to finish before the observation pause begins.
Fix it: If you hear nothing, check that sound is in the first import and that the beep line is inside main(), four spaces in.
Step 9 - Try The Motor Without Await
Remove only await from the motor movement line. Keep the four leading spaces. Keep await on the beep and the pause, and keep the final stop:
async def main():
motor.run_for_degrees(pointer_motor, turn_degrees, speed)
await sound.beep(700, 200, 100)
await runloop.sleep_ms(3000)
motor.stop(pointer_motor)
Predict: The numbers and line order are unchanged. When will the beep start this time?
Run the program. Watch the pointer and listen.
Observe: The beep starts near the start of the turn, while the motor is still moving. In the SPIKE App, this motor command starts its movement even without await. main() continues straight to the beep instead of waiting for the motor to finish. The motor and beep can overlap.
The beep is now too early to mean "movement finished." The remaining beep and pause give the motor time to continue, and the final stop ends any remaining movement. Do not remove them in this test.
What changed? Removing await changed when the next instruction could start. It did not change the requested degrees or speed. This behavior belongs to SPIKE's awaitable motor command; do not assume every Python function behaves this way.
Step 10 - Put Await Back And Compare
Put await back on that same motor line. Change nothing else:
async def main():
await motor.run_for_degrees(pointer_motor, turn_degrees, speed)
await sound.beep(700, 200, 100)
await runloop.sleep_ms(3000)
motor.stop(pointer_motor)
Run the program again. The beep should follow the completed turn again.
| Motor line | When the beep starts |
|---|---|
With await | After the movement command finishes |
Without await | Near the start, without waiting for completion |
The three-second pause is after the beep in both versions. It cannot make an early beep wait for the motor. Keeping await on the motor line connects the beep to motor completion instead of to a guessed delay.
Show the two versions to your teacher and explain which single word changes their timing. Keep the version with await before continuing.
Step 11 - Bring Back The Return Movement
Now use the beep as a marker in your mini machine. Keep the imports and variables. Replace main() with this version, which shortens the pause and restores the awaited return move:
async def main():
await motor.run_for_degrees(pointer_motor, turn_degrees, speed)
await sound.beep(700, 200, 100)
await runloop.sleep_ms(500)
await motor.run_for_degrees(pointer_motor, -turn_degrees, speed)
await sound.beep(400, 200, 100)
motor.stop(pointer_motor)
Run the program.
Observe: The pointer moves out, makes a higher beep, pauses, moves back, and makes a lower beep. Each motor move is awaited, so its beep follows its completion. Changing the second beep's first number changes pitch; its length and volume stay the same.
Physical Challenge And Teacher Check
Make a pointer, arm, or gate move out and back without striking its stops. Choose a degree value that fits your attachment's travel. Keep await on both motor movements.
Show your teacher:
- the port, amount and speed variables, and one change you tested
- the beep after each completed movement
- which word you removed in Step 9 and how that changed the beep's timing
- why a pause after the beep cannot make that beep wait for the motor
For a stretch, display a hub image during the pause.
Save your program and stop it before changing the build.