SPIKE Prime Turns: Gyro Turns vs Encoder Turns for Precise LEGO Robots (Python)
For most LEGO robots, the best way to make precise turns is a gyro turn: the robot keeps reading its heading from the hub's built-in sensor and slows down as it gets close to the target angle. Encoder turns, which spin each wheel a calculated number of degrees, are faster to write and work well for short, gentle turns, but small wheel slips add up. Below we explain both, show working SPIKE Prime Python code for each, and share how our coaches help kids decide which one to use.
Why turns go wrong
A robot that drives straight but turns badly will still miss its target. A turn that is off by just 3 degrees puts the robot several centimeters off course after a meter of driving. In FIRST LEGO League robot runs, that is often the difference between a mission that scores and one that doesn't.
Most turning errors come from three places:
Wheel slip. Tires skid a little on the mat, especially when starting or stopping quickly.
Momentum. The robot keeps rotating for a moment after the motors are told to stop.
Measurement. The distance between your wheels is never exactly what you think it is.
Encoder turns: simple math, fast to write
Every SPIKE motor has a built-in encoder that counts how many degrees it has turned. To spin the robot in place, one wheel goes forward and the other goes backward. The math is:
wheel degrees = robot turn degrees × track width ÷ wheel diameter
Track width is the distance between the centers of your two drive wheels. With 56 mm wheels and a 112 mm track width, the ratio is 2, so a 90-degree robot turn needs each wheel to spin 180 degrees.
Encoder turns are good when:
The turn is small or the robot is moving slowly.
You want a quick turn that doesn't depend on the hub being calibrated.
Students are just learning how wheel size and distance relate (it's great real-world math).
The weakness: the encoders only know what the wheels did, not what the robot did. If a tire slips, the robot doesn't know.
Gyro turns: the robot checks its own heading
The SPIKE hub has a built-in motion sensor that measures yaw, the direction the robot is facing. A gyro turn keeps turning until the yaw reading matches the target. Because it measures the robot's actual rotation, wheel slip doesn't fool it.
A good gyro turn uses proportional control, the same idea from our gyro straight-driving guide: speed equals a constant (kp) times the remaining error. Far from the target the robot turns fast; close to it, it slows down so it doesn't overshoot. We also add a minimum speed so the robot doesn't stall just before it arrives.
The code (SPIKE Prime Python)
This program works in the SPIKE App 3 Python editor. We tested the logic in a simulator with stubbed hardware before posting. Assumptions: left drive motor on port A, right on port B, 56 mm wheels, 112 mm track width. Measure your own robot and change the constants.
from hub import port, motion_sensor
import motor, motor_pair, runloop
# Assumptions: left drive motor on port A, right on port B,
# 56 mm wheels, about 112 mm between wheel centers (measure yours!).
WHEEL_DIAMETER_MM = 56
TRACK_WIDTH_MM = 112
YAW_SIGN = -1 # set to 1 if your hub's yaw grows when the robot turns right
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
def heading():
# tilt_angles() returns (yaw, pitch, roll) in tenths of a degree
return YAW_SIGN * motion_sensor.tilt_angles()[0] / 10
async def encoder_turn(robot_degrees, speed=300):
# Wheel degrees each wheel must spin for the robot to rotate in place
wheel_degrees = int(abs(robot_degrees) * TRACK_WIDTH_MM / WHEEL_DIAMETER_MM)
direction = 1 if robot_degrees > 0 else -1 # positive = turn right
await motor_pair.move_tank_for_degrees(
motor_pair.PAIR_1, wheel_degrees, direction * speed, -direction * speed)
async def gyro_turn(target, max_speed=400, min_speed=80, kp=6, tolerance=1):
# Turn in place until heading() is within `tolerance` degrees of target
while True:
error = target - heading()
if abs(error) <= tolerance:
break
speed = kp * error
speed = max(min_speed, min(max_speed, abs(speed)))
if error < 0:
speed = -speed
motor_pair.move_tank(motor_pair.PAIR_1, int(speed), int(-speed))
await runloop.sleep_ms(10)
motor_pair.stop(motor_pair.PAIR_1, stop=motor.BRAKE)
async def main():
motion_sensor.reset_yaw(0)
await runloop.sleep_ms(200)
await gyro_turn(90) # face 90 degrees right of the start
await gyro_turn(0) # back to the start heading
await encoder_turn(-90) # quick 90 degree left turn by encoders
runloop.run(main())A few notes on the code:
tilt_angles() returns yaw in tenths of a degree, so we divide by 10.
Depending on how the hub is mounted, yaw may get bigger or smaller when the robot turns right. Turn your robot by hand while printing heading() and flip YAW_SIGN if needed.
gyro_turn(target) turns to an absolute heading, measured from where you last reset the yaw. Turning to 90 and then back to 0 returns the robot to its starting direction, which stops errors from piling up across a run.
Tune kp first. If the robot overshoots and wiggles, lower it. If it creeps slowly at the end, raise min_speed a little.
Which turn should your child use?
Our coaches usually suggest this approach:
Start with encoder turns so kids understand the math and see where errors come from.
Switch to gyro turns for any turn of about 45 degrees or more, and for anything that has to line up with a mission model.
Reset the yaw at the start of each run, and turn to absolute headings instead of "turn 90 more."
Test each turn 10 times in a row and write down the results. Consistency matters more than speed.
For more on planning runs around reliable moves, see our FLL robot game strategy guide.
Common questions
Do gyro turns work on the new LEGO Education Computer Science & AI kit? The same ideas apply to any robot with a heading sensor or wheel encoders. The code above is written for SPIKE Prime. LEGO Education retired SPIKE on June 30, 2026, but existing SPIKE kits and the SPIKE App keep working, and SPIKE is still allowed in FIRST LEGO League this season.
Why does my gyro turn stop a little short every time? The robot is probably stopping within the tolerance but drifting after the brake. Try a slightly lower max_speed, or a tolerance of 1 degree, and make sure the hub sits flat and still when you reset the yaw.
My child is new to Python. Where should they start? Our SPIKE Prime Python course walks through motors, sensors and control step by step, at your child's own pace.
Keep building
Precise turns are one of the skills that make a robot feel reliable, and kids feel proud when their robot hits the same spot every time. If your child wants to go further with Fusion Robotics Education:
Try the free self-paced courses in our online course catalog.
Unlock every course with All-Access for $39/month, with a 7-day free trial.
Want a coach? Remote 1-on-1 live lessons are $149/month. Text (619) 786-5731 to set one up.
In San Diego? Our weekend group sessions in Scripps Ranch are 1.5 hours a week for ages 6–18, max 10 kids, and every child works on their own project. The first session is free. Text (619) 786-5731 to book it.



Comments