LISTING 3. An example of how to
bit bang an I2C Start condition which is
used to begin a byte transfer.
Recall that we covered these I2C
conditions which make up the serial
two-wire protocol in Part 1.
the pointer level). The key is that
the readings should be decimal
values between 0 and 360, and
should change depending on the
compass orientation. The pointer
will slowly keep trying to point to
zero degrees north and will move
accordingly using the proportional
If the value gets stuck, there
may be a wiring problem or the
electronic compass module may
not be working. If the wires start
getting tangled, switch the microcontroller off to avoid
damage to the compass. Notice that if you align the
optional quadrature encoder to start at 0 (north), it will also
return the compass direction in degrees.
This compass application insures that all electro-mechanical components (including the VEX gearbox) are
functioning correctly, and are continuously reading and
displaying the compass bearings.
Please note: Before powering up the VEX
microcontroller, make sure that there are no objects
(or fingers or hands) inside the compass rim. Also,
as mentioned previously, be sure to wear safety
eyeglasses and gloves. The compass pointer could
move inadvertently causing injury. Please keep a safe
distance from the pointer while it’s in use.
quadrature optical encoder so that it starts counting at 0
degrees (north). The optional bumper switch can be used
for this purpose.
Delays between motor commands in the firmware may
need to be adjusted slightly to move the compass pointer
correctly, depending on the pointer’s mass and rotational
friction. Also take into account the electrical wires since the
motor may not have enough torque to control the pointer.
A shorter delay decreases the torque, while a longer delay
Remember to keep the compass as far away from
magnetic metals as possible, so they don’t interfere with
In order to keep the compass pointing to true north,
the microcontroller performs a simple proportional control
loop written in Easy C Pro (shown back in Listing 2) to
keep the pointer angles synchronized with the readings.
This code can also be used to help robots stay on course by
Before starting the calibration
process, make sure the compass is
level. It is calibrated by aligning the
pointer to magnetic north and
adjusting for declination so that it
points to true north. You will need
to modify the application by adding
the magnetic declination for your
specific geographic location; you
can do this using the online NOAA
calculators described below. In
addition, you can align the
FIGURE 3. Screen capture of the
data generated by this application. Note
that we will probably get different
compass readings, depending on the
initial compass orientation. The key is that
the readings should be decimal values
between 0 and 360.
SERVO 02.2013 71