Skip to content

Robotics

How to turn a robot exactly 90° with a gyro

A gyro measures turn rate, in degrees per second, not which way the robot faces. To turn exactly 90°, integrate: on every pass of the loop, add rate × the time since the last pass to a running heading, and stop when the heading reaches 90. Keep the heading a float, and subtract the gyro's reading at rest, or the turn never ends and the heading drifts.

Rate × time is angle

heading = heading + rate × dt

180° /s for 0.3 s is 54°. 90° /s for 0.5 s, then 30° /s for 1 s, is 45 + 30 = 75°. The loop does the same sum in tiny slices, and adding up slices like this is integration.

Here the gyro is read on A3, giving 512 at rest and one count per degree a second: above 512 is turning right, below is turning left.

float heading = 0;
unsigned long last;

void setup() { last = millis(); }

void loop() {
  unsigned long now = millis();
  float dt = (now - last) / 1000.0;
  last = now;
  heading = heading + (analogRead(A3) - 512) * dt;
  if (heading < 90) { analogWrite(9, 255); analogWrite(10, 0); }   // pivot right
  else { analogWrite(9, 0); analogWrite(10, 0); }                  // stop
}

A real gyro chip such as the MPU-6050 is read over I²C instead, but the sum is the same.

Keep the crumbs

A pass of loop() takes well under a millisecond, so at about 120° /s each slice adds about 0.12°. An int heading throws that fraction away every time and adds 0 + 0 + 0 for ever: the robot spins without stopping. Likewise (now - last) / 1000 in whole numbers is 0 on every short pass, so divide by 1000.0.

Live experiment

Add up the gyro, turn 90°

The rover pivots right, adding the gyro's rate × time to its heading every millisecond, until the heading reaches 90°. With an int heading each 0.12° slice is thrown away and it spins for ever; a float stops it at 90. Try half speed: it takes twice as long, to the same 90°.

Try it yourself: free lesson

“Spin by the gyro” is lesson 2 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 15 before it first.

Drift

Integration adds up mistakes too. A gyro that reads 510 at rest reports 2° /s of turning that isn't happening, and after 30 s sitting still the heading is 60° out. Measure the rest reading at start-up and subtract that instead of 512, and keep turns short. For a heading that holds for minutes, blend in a compass or an accelerometer with a complementary filter.

Holding the heading

Once round, set a turned flag and never pivot again, then steer to hold the angle:

err = heading − 90 · left = 200 − 4 × err · right = 200 + 4 × err

Knocked 10° right, err is 10: left 160, right 240, and the nose swings back. Without the flag, a knock back to 89° would start a whole new hard pivot.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Spin by the gyro” is lesson 2 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 15 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3GYRO reads 452. The rover is turning…
    Show the answer

    Answer: 60° per second, to the left. 452 − 512 = −60 counts, and each count is 1° /s. Negative is turning left. It's a rate: how fast, not how far.

    • Why not “60° per second, to the right”? Below 512 is turning left. Readings above 512 are turns to the right.
    • Why not “60° to the left, in total”? A gyro measures rate, not angle. 60 counts off means 60° every second, not 60° in all.
    • Why not “452° per second”? 452 is the raw reading. Turning is measured from the at-rest 512: 452 − 512 is −60.
  2. Question 2 of 3At rest the gyro reads 510 instead of 512. After 30 s sitting still, the heading says…
    Show the answer

    Answer: 60° to the left. 510 − 512 = −2° /s of turning that isn't happening. Over 30 s that adds up to −60°: drift. Real robots measure the rest reading at start-up and subtract that instead.

    • Why not “2°”? 2 is the error per second. Added up over 30 seconds it makes 60°.
    • Why not “0°”? Nothing is turning, but the sum assumes 512 means still, so it adds −2° every second.
    • Why not “60° to the right”? 510 is below 512, which the code reads as turning left, not right.
  3. Question 3 of 3Why the turned flag, instead of just pivoting whenever the heading is under 90?
    Show the answer

    Answer: A knock back under 90° would start a whole new pivot instead of a gentle correction. Once round, the job is holding, not turning. Without the flag, a knock to 89° makes the rover spin hard on one wheel again.

    • Why not “It saves memory”? A flag is a single byte. It's there for behaviour, not to save memory.
    • Why not “It runs faster”? One extra true-or-false check makes no real difference to speed. It changes what the rover does after a knock.
    • Why not “The heading can't go below 90”? The heading can go below 90: a knock to the left takes it there, which is exactly the case the flag handles.
Keep practising: free lesson

“Spin by the gyro” is lesson 2 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 15 before it first.

Questions people ask

Why does my gyro heading drift?

The gyro's reading at rest isn't exactly zero, and integration adds that small error up on every pass: 1° /s of offset becomes 60° in a minute. Measure the rest reading at start-up and subtract it, and for long runs correct the heading with a compass or an accelerometer.

How often should I read the gyro?

As often as you can, and always measure the real time between reads with millis() or micros() instead of assuming it. Hobby robots typically integrate 100 to 1000 times a second.

Why does my robot turn past 90°?

The wheels take a moment to stop after the code says stop, so the robot coasts a few degrees past the target. The gyro sees that too, so a holding loop afterwards can steer it back, or you can start braking a few degrees early.

Every topic