How to Mount and Use an I2C Compass on your Raspberry Pi/Arduino Project

Location #2 Next to the PiCamera Pan/Tilt

How to Mount and Use an I2C Compass on your Raspberry Pi/Arduino Project

This article talks about the considerations for mounting a I2C electronic compass on your Raspberry Pi or Arduino project.

What is SunRover?

IMG_6050SunRover is a tracked solar powered robot designed to move around and explore the area while sending back reports, tracking weather, managing a power budget tightly and providing a platform for testing new sensors and equipment as they become available.

SunRover Block Diagram
SunRover Block Diagram

The Block Diagram

The major subsystems are shown to the right.   It takes a lot of thought to put together a good block diagram.  You make a lot of design decisions that might be difficult to change later in the design.   Here is a list of the major system decisions:

  • – Use a prebuilt Track and Motor controller system
  • – Solar Powered system
  • – Solar Panels will be shifted from one subsystem to another according to demand and task set
  • – Two major computers:  Raspberry Pi 2 for higher levels of control and an Arduino for power management and turning devices and sensors on and off
  • – Dual WiFi communication Paths
  • – SunRover control panels using RasPiConnect [ www.milocreek.com]

Putting the Compass on the Robot

We would say the most critical piece of navigation hardware currently in SunRover is the magnetic compass.  And of course, it’s the sensor We have had the most trouble with.

Earth has a magnetic field.   It is quite weak  (roughly 25 to 65 microTeslas (uT)) and is roughly 10 degrees offset from the geographic poles (the rotational axis of the earth).  Because of the proliferation of drones on the market, over the past few years there has been a great increase in the number of inexpensive electronic compass I2C modules on the market.   These units basically come down into two types:

  1. Magnetic field sensors
  1. Tilt compensated magnetic field sensors

The magnetic fields sensors, such as the HMC5883L are very inexpensive.  You can pick one up on E-bay for less than $5.00.   We first purchased one of these to use in SunRover.  We quickly found two issues.  One, SunRover tilts.  It is on treads and if it halts suddenly (which is the way the motors usually are shut off), it will tilt.   We found the HMC5883L quite sensitive to this tilt and would vary the compass readings by 15 or 20 degrees magnetic in some cases.

Considering we want to be able to point SunRover to +/- 5 degrees, this was a huge source of error.  We did some research, and for about $20 we could get an LSM303D 6 -axis accelerometer and compass.   When this came in we were able to hook it up quickly (we  love that I2C Grove connector).  With that the Tilt issue was fixed.

Next, we noticed that sometimes the compass would lock (at 278 degrees for some reason) when, you guessed it, when we ran the motors.  Of course, when we need the heading of the robot the most (when it is moving),we would lock the compass.   Looking at the data, we surmised that it was primarily the wires running across the unit, with influence from the motors.    Until we realized what was going on, the robot would occasionally just sit there and spin until the maximum time expired.

#1 Location of Compass (under the top of the equipment bay box)
#1 Location of Compass (under the top of the equipment bay box

The location #2 was about 125mm above the motor platform.  We then moved it up to next to the Pi Camera pan/tilt mount,

Location #2 Next to the PiCamera Pan/Tilt
Location #2 Next to the PiCamera Pan/Tilt

putting it up about 150mm above the motor platform.   Well, it looked great, but it would lock up even more regularly because, silly us, it was next to the servo motors driving the PiCamera pan/tilt mount.

 

 

 

 

 

 

 

 

Location #3 on Side of SunRover
Location #3 on Side of SunRover

OK, even though it looked really cool, we had to move it again.  This time we put it out the side of the robot in a plastic bubble and again it looked great, but it would still lock up occasionally though not as much as in location 2.

 

 

 

This led to the building the “great tower of magnetic isolation” to put the compass well away from wires and 300mm away

Location #4 - High on a Tower
Location #4 – High on a Tower

from the motors and the PiCamera.   Sharp observers will notice that this is the same pylon design used to isolate the Lightning Detector from the computer electrical noise in the lightning detection project.

 

We do think we understand what is going on here, but we’d like a little more science behind what we just did.  So we decided to get an inexpensive (~$100) magnetic field data logger and measure the magnetic currents at all four of the locations. We purchased a NEULOG Magnetic Field Data Logger to get some dynamic readings (and to log them automatically).  As of this writing, the NEULOG logger on the MacBook is not quite ready for prime time. We had issues, which we are working with their excellent customer service to fix.   More on the NEULOG loggers in a future SwitchDoc Labs blog.   It has a resolution of 0.001 milliTeslas (mT) or in other words 1 microTesla (uT) so this will allow some really good readings (remember the earths magnetic field is about 25 – 60 microTeslas).   Note that the difference (the delta) between static and motors running is the important thing as the magnetic fields may not be oriented in the same direction.  You also have to remember to keep the sensor at the same angle to the ground for all measurements.

Measuring the Magnetic Field at Location #2
Measuring the Magnetic Field at Location #2

So what does this data mean?  It means we were wrong in our assumption that it was motor magnetic noise that was hosing the compass.  Doesn’t fit the data.   What it does mean is that the problem is the shielding coming from the wires in the robot in locations #1 and #3 (think of a Faraday Cage approximated by the wires and power supplies for the robot inside the box).   The number of the static value of location #1 is larger than expected (50uT) probably because the magnetic field sensor wouldn’t fit under the wires where the compass was and the box wasn’t shut tightly because of the USB cable for the sensor.

Location #2 is obvious as it is right next to the servo motors.   187uT!  Three times the earth’s magnetic field.

In terms of anecdotal data (We didn’t keep the statistics), the locations locked the compass in the following declining order:  #2, #3, #1 and #4 not at all.

Compass Location Static Value (uT) Peak Absolute Value of DELTA During Motoring (uT)
Control Location Away From Robot 60uT N/A
Location 1 50uT 9uT
Location 2 187uT 4uT
Location 3 15uT 4uT
Location 4 70uT 1uT

Conclusion

Take away?  Mount the compass away from the all the robot wiring and metal.  The “great tower of magnetic isolation” is a good idea. The motors are well shielded in SunRover, so it seems to be the shielding effect of the wiring in the robot itself.