Makeblock compass drive


Hello community!

I have built a Makeblock Robot-Starter-Kit with a MeCompass. The robot drives first north, then west, then south, then east and finally it turns to north again. During its drive it corrects the heading automatically by comparing the current heading with the target heading and then turn right or left to come back to the target heading.
I am looking forward to test the robot outdoor when it has to drive a track with waypoints.

You can watch the Video at:

YouTube compass drive

For downloading the Arduino code (not scratch) follow the link at YouTube.



Consistent Turns for mBot

Hello community!

I have improved my robot compass driving program.
Now the program calculates the shortest way for rotation from current angle to target angle and it’s more accurate.
Of course you can remove the rows for RGBLed or light sensor and TFT-display, if you don’t have such sensors.
You can download the arduino sketch from the link which I put under the video on Youtube.
Let me know, if it works well for you or if you have any problems found.




SigTrygg - where did you get your MeCompass.h and .cpp? I am trying to compile and getting errors with the compass.begin (which my meCompass class does not have). By the way, nice job on the box navigation…




Yes, you used the old Library 2.0, I guess.

You have to got to and here to: Community_GitHub_Repositories_Makeblock-Libraries
Here you can download the zip-file of library 3.0

I don’t know, why Makeblock still offers the old library for download.

Good luck!




Thank you for responding so fast. That’s where I had started and there was no MeCompass.h or .cpp in the zip file so I went back to version 2.

I thought maybe you had made your own function or extended another class’ function to good effect.

I created a begin function in the meCompass class just so it would compile and am troubleshooting the rest (hoping that no code in the begin function is important which I believe to be inaccurate). By the way, I am just trying to replicate your work on the mBot.



Hello again!

I don’t know if I understand it right. You haven’t got the MeCompass.h in the the zip file? I added it to this answer. But you know, that you must copy the unzipped folder “makeblock” directly into your arduino library folder? And that you mustn’t copy the whole folder to your arduino library?
The MeCompass.h is called by the MeOrion.h. You need both header files. I think the begin-function is necessary for initialising the compass for I2C-connection.
Did you get the new arduino file? You can find it under my YouTube-video.
It is more accurate and let the robot take the shortest way to the target angle. At the moment I work on a program to let the robot drive a path. When it hit an obstacle it shall turn to the next angle, reaching the next waypoint (next obstacle) When weather will be a little more dry I will try it outdoor on my ground.
I hope I could help. I wish you a lot of fun with your project. Maybe you can show it, if it is ready?


Makeblock-Libraries-master (3).zip (588.5 KB)?


Thanks Sigtrygg.

I thought I was going to the same repository but it wasn’t. I downloaded a file by the same name that was reached via the “popular repositories” list with the same title and was after version 2 but did not have the src folder.

Thanks and I’ll run this through,


Thanks! I’ve been trying to do exactly this. Looking forward to digging into the code and figuring it out!


Hello! Thanks for the beautifully commented code. I’m trying to run on my Orion board (with just a few modifications, because I don’t have the TFT display). I appear to be having some calibration issues, though: the D2 calibration indicator on the module keeps blinking. Per the meCompass User Manual, I’ve tried rotating the robot 360 degrees along the compass axis and pressing the calibration button, but the module won’t exit calibration mode. Any code placed after Compass.begin() in the sketch doesn’t run. Any suggestions?



Yes, have had this issue, too! But the compass worked well, when I switched off the buzzer on Orion board. Have you tried this? The buzzer blocks the I2C recognition. There is a little switch on the side of the board.




Thank you! Manipulating the buzzer switch does change the behavior of my compass, but I’m still struggling to make it work. Now, with the buzzer off, the compass shows a solid blue light (measuring mode), which seems good! But it still won’t execute any code placed after Compass.begin(). Also, interestingly, pressing the key on the module won’t put the compass into calibration mode either. Any other tips? Thanks so much!



I can’t calibrate my compass either about the buton, but nevertheless it works very well.
Can you show your code? Maybe I can find, why the compass doesn’t work.




Hello darastrixsthyr!

If your compass still doesn’t work, you can try the library from Jeff Rowberg. I add a zip-file to this post.
It is for handling I2C device and the compass module HMC5883L, which is the same as the MeCompass.
You must put the unzipped libraries in your folder “Own documents” in Arduino/libraries/.
So you have the two folders with name “I2Cdev” and “HMC5883L” in your Arduinolibrary.
Please let me know, if it now works.

Bye, (22.8 KB)


Hello community!

We have Eastern and I had time to test my robot tank outdoor. It navigates using the compass sensor and ultrasonic sensor. You can look the video on youtube:

robot tank compass drive outdoor




Hi Sigtrygg,

The tank navigation looks good :slight_smile: . I have used some of your code to use with an mBot with Compass, but the output of the compass is not as I expect. Currently its only outputting a heading between 300 and 340 degrees.
I suspect that something was wrong with the calibration. Did you calibrate your compass? or was it already correctly calibrated out of the box?
I have verified that the calibration routine has been run and completed(EEprom updated), but think its been updated with garbage data.
Thanks for any pointers


Hello christer!

Did you mount the compass in the manner, that is showed in my video? It’s very important how you hold the compass.

To run the compass was very diffcult.
When I first tried to run the compass nothing worked! Then I got it, that something was wrong with the I2C interface in the header-file. When I switched off the buzzer, my compass was recognized, but it showed senseless data.
I calibrated it by pressing the button, turning 360 ° and so on, but there were still the senseless data.
Then I found, that the MeCompass is the same as the HMC5883L. In the internet I found a library and a sketch by Jeff Rowberg. I added the library for my orion board (arduino) for I2Cdev and HMC5883L and added the sketch by Rowberg to calculate the angle in degrees.

Suddenly it worked perfectly!

Did you get the current heading of compass by the following function by Jeff Rowberg It’s important to mount the compass as in the video! (myserial.print is for my LCD-screen, you have to change it to Serial.print for your monitor, if you don’t have a LCD-screen :

float getCurrentHeading() //return headingDegrees
head_X = Compass.getHeadingX();
head_Y = Compass.getHeadingY();
head_Z = Compass.getHeadingZ();
// Calculate heading
float heading = atan2(head_Y, head_X);

// comments by Jeff Rowberg:
// Set declination angle on your location and fix heading
// You can find your declination on:
// (+) Positive or (-) for negative
// For Kiel / Germany declination angle is 2’14E (positive)
// Formula: (deg + (min / 60.0)) / (180 / M_PI);
float declinationAngle = (2.0 + (14.0 / 60.0)) / (180 / M_PI);
heading += declinationAngle;

// Correct for heading < 0deg and heading > 360deg
if (heading < 0)
heading += 2 * PI;

if (heading > 2 * PI)
heading -= 2 * PI;

// Convert to degrees
float headingDegrees = round (heading * 180 / M_PI);
// Output
mySerial.print(“DS64(0,128,‘C-Heading: “);
mySerial.println(” deg’,17);”);


return headingDegrees;


I hope you get the compass running.

If I can help, let me know.



Thanks for replying,
I used the function you highlight above, taken from an example that you shared on the tank project “RobotStarterKit_compass_drive1.ino”. From that file it was not clear to me how you used any of the functions from the I2Cdev and HMC5883L libraries. From the code it looked to me like you only used the makeblock compass libraries. I will try to add those libraries and see if I have any success