Arduino

Getting Started with a Digital Compass for Arduino Robot Navigation

Navigation is a huge topic in robotics, and there are so many things we can consider. For example, you want your robot to turn from a given angle, or go in a straight line. One component that is very helpful in robot navigation is a digital compass. This component measures the orientation of the robot compared to the Earth magnetic field. We can for example use it to determine the angular orientation of the robot at any given point in time.

In this article, we will use this kind of component on an Arduino-based robot. We will use a digital compass to precisely make the robot turn of a given angle. Of course, the principles you will learn in this article can be used in several other situations, for example to make your robot move in a straight line. Let's dive in!

What You'll Need

ComponentQty
DFRobot MiniQ 2 wheels v2.0 robot chassis1
4 AA 1.2V rechargeable batteries1 set
LSM303 accelerometer + compass chip (alternative)1

Step by Step

1

Hardware & Software Requirements

<p>There are many Arduino-compatible robot chassis out there that we could use for this project, but for this project we decided to simplify things and use the latest version of the <a target="_blank" rel="noopener noreferrer nofollow" href="https://www.amazon.com/Wheels-miniQ-Balancing-Robot-chassis/dp/B01CCZHQFC/ref=sr_1_2?ie=UTF8&qid=1494932379&sr=8-2&keywords=DFRobot+MiniQ+2+wheels+robot+chassis">DFRobot MiniQ 2 wheels robot chassis</a>. This chassis already comes with an onboard Arduino system, so out of the box you can make the robot move around without any other components required.</p><p>This chassis also has many sensors on board, and especially what we are interested in here is that there is already a digital compass onboard: the HMC5883 3-axis digital compass. This is the one we will be using in this project.</p><p>However, if your robot chassis doesn't have an onboard compass, no worries: there are many external components you can use for that. For example, we recommend using the <a target="_blank" rel="noopener noreferrer nofollow" href="https://www.amazon.com/Acceleration-Development-Triple-axis-Accelerometer-Magnetometer/dp/B0149KK6SW/ref=sr_1_1?ie=UTF8&qid=1495096549&sr=8-1&keywords=LSM303+accelerometer">LSM303 accelerometer + compass chip</a>. Adafruit made a nice breakout board for this chip.</p><p>The only other thing that you will need is a set of 4 AA batteries to power the robot. We chose a set of <a target="_blank" rel="noopener noreferrer nofollow" href="http://a-fwd.com/asin-com=B00CWNMR5Y&com=openhomeautomation-20&ca=openhomeauto-20&uk=openhomeaut09-21&de=openhomeauto-21&fr=leblosed-21&it=openhomeaut08-21&es=openhomeau028-21&sc=w">4 AA rechargeable batteries</a> for this project.</p>

2

Hardware Configuration

<p>The hardware configuration for this project is really simple if you are using the <a target="_blank" rel="noopener noreferrer nofollow" href="https://www.amazon.com/Wheels-miniQ-Balancing-Robot-chassis/dp/B01CCZHQFC/ref=sr_1_2?ie=UTF8&qid=1494932379&sr=8-2&keywords=DFRobot+MiniQ+2+wheels+robot+chassis">DFRobot MiniQ 2 wheels v2.0 robot chassis</a>: basically all you need to do is to plug the batteries into the chassis and you are done. This is the compass chip that we will be using in this project.</p><p>If you are using an external digital compass for this project, refer to the datasheet of your sensor to know how to wire it to the Arduino microcontroller.</p>

3

Writing the Code - Setup

<p>We are now going to write the code for our project. Note that the code will be slightly different if you are using another compass than the HMC5883 chip, but the principles are the same. Usually other digital compass chips also use the I2C bus, so the code will be nearly the same as for the HMC5883 chip.</p><p>It starts by importing the correct libraries and creating an instance of the HMC5883 sensor:</p><p>After that, we define the pins on which the motors are connected. Note that this can vary depending on your robot chassis:</p><p>We also need to define some variables for the function we will use to make our robot turn:</p><p>We also define some shortcuts for the left or right directions.</p><p>In the setup() function, we define the motor pins as outputs and initialise the digital compass. Still in the setup() function, we need to initialise the variables to make the robot turn, as we didn't move the robot yet.</p>

4

Writing the Code - Main Loop

<p>Now, in the loop() part of the robot, we can use the push buttons on the robot to make the robot perform a 90 degrees turn (left or right):</p><p>Of course, if your robot chassis doesn't have such buttons, you can simply hard-code the behaviour into the code, for example to make the robot turn left, turn right, and then stop.</p><p>We are now going to see the details of this turn(angle, direction) function that we use to make the robot turn from a given angle using the digital compass. It starts by getting the initial heading of the robot, and calculating to which angle we want to turn.</p><p>Then, we continuously make the robot turn by small intervals, and check if the target heading has been reached. This is done by measuring the heading again, calculating the heading difference, and checking if the target has been reached.</p>

5

Testing the Project

<p>It's now time to finally test the project! Upload the code to your robot via the Arduino IDE, and then disconnect the USB cable. If you chose to test the behaviour of the robot like in this project (via the push of a button), you can just put the robot on a flat surface.</p><p>Then, press a button: you should see that the robot is turning from approximately 90 degrees, step by step, and then stopping. You can do the same by testing the behaviour of the robot when turning in the other direction.</p>

Wrapping Up

In this tutorial, we used a digital compass to precisely control the movement of a small 2 wheeled Arduino-based robot. We implemented a simple behavior using this digital compass: making the robot turn on itself by a given angle.

Of course, there are many other things you can do based on this project. You can for example modify the code to improve the behaviour of the robot. You can use the digital compass to implement other kind of movements, for example always moving in a straight line.

What about you, already used a digital compass for the navigation of a mobile robot? What behaviours did you build with it? Share in the comments!