How to calibrate your gyro

Gyro is widely use for all kinds of robots these days, and it provides a reliable feedback for micromouse when the mouse needs to turn with a accurate angle or angular velocity to continue the search.

A analog 1 axis gyro usually has 2 outputs, one is called outZ refers to the angular velocity output in Z-axis, outZ provides angular velocity output when you mouse rotate at through Z-axis. When mouse is not spinning, the voltage output of OutZ remains half of the supply voltage, and voltage decrease when spin to the right and voltage increase when spin to the left. There is another pin is called Vref refers to reference voltage at Zero point. This voltage is basically same as the Outz voltage output when mouse is not spinning(also called zero point)

Since Gyro outputs angular velocity instead of angle, the way to determine you actually angular velocity is to use the ADC value of Outz to subtract the ADC value of Vref. So the actually angular velocity equal to zero when gyro stay still. And the actual angular velocity is positive when spin to left, and vice versa, the actual angular velocity is negative when spin to right.

If you want get angle value of a particular turn for your micromouse, since the angle is the integral of angular velocity, we can simply do estimation by accumulating angular velocity at every unit time. I accumulate angular velocity every 1 millisecond to get angle.

Here is the sample code:

void readGyro(void)


                int curt = micros(); //record starting time in micro seconds

                Vref = read_Vref;//read ADC for Vref

                Outz = read_Outz;//read ADC for OutZ

                aSpeed = (Outz – Vref);//get angular velocity

                angle += aSpeed;//accumulate angular velocity for angle

                while((micros()-curt)<1000);//use up rest of the time until 1000us(1ms) to make sample  time even at each time, you don’t need this line when you run this function in PID 1ms Interrupt service routing



The code above is obviously the ideal case. Since Gyro is sensitive and noisy, you will need to do some oversampling to make the output more stable. You can just simply take lots ADC samples then do average. Or if you use STM32, you can choose how many samples the ADC components will take to make oversampling hardwarely. The option is available in ST library.

There is also a common problem for gyro is called “zero point drift issue”. Ideally the angular velocity ADC output is zero when gyro stay untouched, however, in real world, when you subtract OutZ with Vref, the value is not always zero. It might be something above zero or below. What you need to do is to find the offset value and add or subtract from your angular velocity to make the angular velocity output equal to zero when the gyro is not spinning. Otherwise, since we keep accumulating the angular velocity to angle, even if you are not touching your gyro, your angle will keep increasing or decreasing at certain rate even though your actual angle is supposed to be zero. A small drift is tolerable since your turn movement goes really fast, but you should still try to make your gyro output as less noisy as possible without sacrifice too much sensitivity.

Notice more sample will make your output more stable but at the same time, you will salvage sensitivity and time to achieve this. You need to make decision about how to make a balance between them. Of course, a good circuit design will reduce lots of unnecessary noise for gyro to make your life easier.

2 thoughts on “How to calibrate your gyro

  1. Hi,
    This readGyro () function is different from the project futura readGyro () function. Why does not you use the outVref variable in the project futura readGyro () function? When I try these functions, I get an increasing angle value as the micromouse stays constant. How can I read the angle of micromouse rotation?

    • I found Vref is less stable than I expected, so I chose not to use it.
      I use the Zout value to capture the zero point instead of using Vref.
      angle is the accumulated angular velocity value reads from the gyro over time.
      You need to re-calibrate the zero point value for your gyro on your robot, otherwise it will keep drifting.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.