mBot with gyro-sensor control


Turning the mBot by gyro-control

The aim was to turn mBot for a specific angle, e.g. 90°.
Turning the bot by driving the motors for a specific time and power is very inaccurate, because of different friction of the ground and capacity of batteries.
A good solution is to use a gyro-sensor and to integrate the angle-velocity over the time until the specified angle is reached.
The Me-3-axis-Gyrosensor can use to do this job.
Get sure, that you use the z-axis to get the right turning angle!
I got best results, when I determined an angle of less 5 degrees, e.g. for a wished 90° turn I gave it 85°.
I think it’s because of the inertia of mass and slipperyness of ground.
You can download Arduino-program under the following link:

Arduino program

Get sure, that the ports of motors and gyro sensor in the program correspond to your mBot configuration.

Have fun!


mBot Video


Thanks for sharing. Interesting to see that it works so well. I am going to give it a try in the near future.

Greetings, PinkyPepper (https://www.youtube.com/channel/UCtVRNnOm-70dMkJt9vUSGWA)


Well done Sigtrygg !

I’ve done a lot of experimentation about gyro control in oder to turn for a specific angle.

I can share information with you about that
I had a look to your code, you have defined a approximative angle to assume a exact turn, for example : 84°<=> 90° or 39° <=> 45° … this come from the naturel drift of the value of the device.
In fact the Gyro Z-Value come from integration of the movement during a particular time. If you do nothing after calling primitive fonction gyro-begin() , the value is going to increase alone. more time is spending to turn and more the value will be different to the real value… so you can accelerate the move to reduce the drift but in this case the primary value will be less accurate.
In your example, you have assumed that for 120 rpm speed , the drift can be estimated to a few degrees and you have substract them to the aim angle. It can work but it depends to the speed and the power of batteries …
Another approch is to check more often and to call the gyro-begin() function each delta time (where delta is very frequent) in order to well predict the drift.

  • I can say that a exact left turn is more easy to do because the drift is going to appear in the same direction. By the way, you can turn exactly to the aim (exact degree) and complete your turn with a little more movement. The value to add depend to the number of seconds that you have used to turn.

  • The right turn is more complicated because you have to stop before your arrived :wink: In this case you have to predict the exact value with time, angle and speed …
    In my oppinion, the best way to reduce the drift of the device is to well power the gyro and to first initialize the device when it is immobilised.

Best regards


hi can we see an example for your suggusted solution

I apply Sigtrygg code, but the turns are not accurate my speed is 60, not 120

Me Gyro - Piece of junk

I’m keen to get mBots to make more precise turns. I see you have posted information on using the gyro sensor as well as a separate post using the compass sensor. Could either be used in performing turns? From Lego EV3 experience, gyro sensors are often affected by drift and compass sensors are often affected by electromagnetic interference. Neither appears perfect - is that your opinion for the Makeblock products as well?

From your experience, would you recommend using one or the other? Is it possible or even useful to use both in a project?


I can’t give advice about compass but I guess sensor might be affected by electromagnetic interference … I can tell that gyro sensor is really affected by drift symptom … so there is no ideal solution for this kind of issue


Oh what a great job. say that a exact left turn is more easy to do because the drift is going to appear in the same اجاره ون


Since my last post I bought compass module and … It’s not bad at all !!! I was kindly surprised by the result. It’s necessary to configure compass after building robot structure and the configuration is valid for a place but it is quite efficient to make a control turn … more efficient than the Gyro module for turning < 20°


TestBoussole.sb2 (198.2 KB)

You can try this example (Auriga mount)… don’t be affraid by the rendering on screen :wink: it won’t be complicated to wired the robot : sonar sensor + compass + embeded gyro


I’m also trying to achieve this X deg turn with my mBot, using the mBlock app (on a tablet). so I tried writing the code in block, which turned out to be something like that:

but - I’m not getting the correct turn at all! sometimes it’s much wider (like 180 deg instead of 90) and sometimes much smaller turn (like 10 deg).

I’ll be glad to get some help!


Have you tried to upload the program to mBot and test? As online performance may have delay.


I could try, but I’m using mBlock on my Android tablet. is there a way to use upload mode from tablet?



I’ve used the gyro recently on an mBot and got it to work reasonably well. I am not going to be near my mBot for a few days so cannot verify this exactly but a few ideas might help.

Sorry if this is obvious but have you got the gyro sensor mounted correctly on the mBot so the z-axis is horizontal? If not, this will be an issue. Otherwise, read on.

Suggest you change your program to display the gyroscope value throughout the turn to debug it e.g. use a repeat instead of a wait and display the gyroscope value inside the repeat loop. Use this to observe what happens. For example, does the gyro reset to zero each time you click the flag or does it carry on the current value? I can’t remember but suspect the latter. My guess is it is only reset to zero on startup.

Next, the gyro reads from -180 to 180, where positive is to the left of zero and negative is to the right (that is from memory, so you will need to check yourself). So, looking at your code, if you start at -90 and turn to the left then you wouldn’t stop until you are +91 i.e. around 180 degrees. However, if it is +80 to start with then you will stop at +91 i.e. around 10 degrees.

As I said, display your gyro as you turn and verify whether what I have said makes sense.


I am afraid on mobile devices cannot upload codes for mBot. And it seems that no plan to add this feature yet.