NOTE: Be careful! You
can damage your NXT Brick
if you apply the wrong
signals to the wrong pins!

R-SCL

R-SDA

1 VCC

Green C4 of
NXT cable

3 SDA

The interface provides a
standard I2C expansion bus.
For the discussion that
follows, you must connect
pull-up resistors between
VCC (pin 4) and both SCL
(pin 5) and SDA (pin 6).
Only one set of resistors is
needed for multiple ICs, but
each diagram shows the
resistor connections for
completeness.

Blue C6 of NXT
cable

Yellow C5 of NXT
cable

2 SCL

NOTE: R-SCL and R-SDA
are 75K to 82K values

Devantech R117-
Compass

9 VSS

RobotC programming
software for the NXT

The software to
control the interfaces is
written in RobotC, the programming language for the
NXT. RobotC was developed and is sold by Robomatter,
Inc., in conjunction with the Carnegie Mellon University
Robotics Academy.

I won’t describe the attributes of RobotC (which are
many) in detail but it is worth mentioning that it comes
with debugger tools including an I2C test debugger.

Black C2 of
NXT cable

SCHEMATIC 1.
Connections for the
Devantech R117
compass operating
with an I2C interface.

A summary of steps in the flowchart are:

• Set NXT port S4 and connect the cable into this port.

• Define in the software the hardware address and other
variables used in the program.

• Build the I2C message to “wake up the compass.”

DEVANTECH R117

ELECTRONIC COMPASS

The Devantech R117 electronic compass by
Acroname has many ways of obtaining its current reading.
There is a PWM output and an I2C interface, which is
shown in Schematic 1. When using the I2C interface, the
resolution of the output can be selected by the user.
A 0-255, eight bit value from register 1 can be obtained
which gives a 360/256 = 1.4 degree resolution (not
accounting for other errors). Also, a 16 bit value can be
obtained from registers 2 and 3 with a raw value of 0-3599.
(In this article, we will use the 0-255 ‘raw value,’ eight
bit output.)

The software communicates with the selected NXT
port, configures the compass output (in this case, eight bit),
communicates with the compass over the I2C bus, converts
the output of the compass to 0-359 degrees, and the
displays the value on the NXT display.

The I2C addresses for this device are 0xC0 and 0xC1.
Address 0xC0 is used to initiate a read of the compass
and 0xC1 is the return value. If this is confusing, you
have to remember that the LSB of the I2C address encodes
the type of access: 0 to send/write a command, 1 to read
a value. Program flow is explained through Flowchart 1.

A partial listing of the software code in RobotC is
shown in Program Listing 1. The program listing shows the
I2C subroutine and calls.

• Send the wake up message over the I2C link.

• Build the I2C return data from the compass message.

• Send the I2C message and capture the compass reading.

• Use transfer functions to get the compass reading to be
0-359 degrees.

• Display the compass reading on the NXT display.

• Loop again.

FLOWCHART 1—READ AND DISPLAY
COMPASS VALUE ON NXT DISPLAY

Here’s how the program works:

• The first section of code shows how to define the NXT
port to be used which is S4, and to define the compass
wake-up address as 0xC0.

• The next section of code is the I2C subroutine which
manages inserting or retrieving a message from the I2C bus.

• The next section of code is the Main program. In RobotC,
tasks are listed before the main() function.