The NXT ultrasonic sensor and the Arduino

This page is an experiment in measuring distances
ultrasonically with the Arduino platform. Only a stock LEGO NXT
ultrasonic sensor ($32) and a stock HC-SR04 Arduino ultrasonic module
($8 to $16) are considered here.

A LEGO NXT 9846 Ultrasonic Sensor: If you have one of these
which you can borrow from an NXT set then you will want to read
the developer kits available at LEGO Support.
And then to understand how it really works you should read
TKJ Electronics' discussion of work-arounds needed to use
the stock NXT ultrasonic sensor. Below I was able to use TKJ
Electronics' sample code, modified i2cmaster library and schematic
diagram pertaining to the ultrasonic sensor without making any
changes. First, be sure to follow the link on the TKJ Electronics
page to Jon
Ward's post of 07 Jun 2010 to understand one of the more
obscure idiosyncrasies of the LEGO sensor that requires wire-ORing
a digital output pin to the I2C SCL clock pin to be able to
briefly pull the clock down. Here is a list of the idiosyncrasies
that require some attention, in no particular order:

The measurement reported is from about the middle of the
sensor. If you want the measurement from the front of the
sensor, subtract 3 cm.

The schematic from LEGO looks like 5 Volts from a USB port
would be enough for Pin 1 of the connector. The screenshot below
of a test run shows a higher reading of 20 cm, dropping to
the same 18 cm as shown above using the NXT brick View function,
after Vin is supplemented with a 9 volt battery.

Sockets to fit the NXT plug with the asymmetric tab are somewhat
rare. As in these pictures a standard Leviton socket from
Home Depot etc. can easily have one side of the lip cut away
so the NXT plug will still lock in place.

The LEGO hardware documents say the I2C clock runs at 9600Hz.
TKJ Electronics found that the NXT brick actually uses 11494.253Hz
and has already modified the i2cmaster library accordingly.

The Wire I2C library does not allow for the repeat-start mode.
Since this seems mostly used when more than 1 master is
on the network and the NXT is a single master only setup, just
using the i2cmaster library instead (as recommended by
TKJ Electronics) was more than adequate for my test.

The small voltage divider to supply about 4.3 volts to Pin 4
of the connector (as shown on the TKJ Electronics schematic)
worked Ok.

Using Digital Pin 4 connected to Analog Pin 5 (SCL) to briefly
pull the clock low between write and read portions of the
I2C transmission (as recommended by TKJ Electronics) worked Ok.
In my test setup below with a Rev 3 Arduino Uno, Pin 4 is actually
connected to the duplicate SCL pin up near the Reset Button.

In summary, the software and hardware setup recommended by
TKJ Electronics worked without changes in my trial. If you did
not want to make your own adapter cord, the shield from TKJ
Electronics would be an option.

A little more detail:

Pin

Function

Cat 3

NXT

1

Vin+9v

white/green

white

2

Ground

white/orange

black

3

Ground

blue

red

4

+4.3v

white/blue

green

5

SCL

orange

yellow

6

SDA

green

blue

An HC-SR04 module for the Arduino platform: This module
requires connecting fewer wires, but writing a little more
software. However the software is simpler and no additional
library is required. The HC-SR04 is similar to, but cheaper than
the popular Ping sensor from Parallax. The HC-SR04 is easily
available on the Internet. The Ping sensor is also available
on the Internet, and in some local stores like Radio Shack.
(In the LTU area for example, Nicholas at the Tel-Twelve
Shopping Center Radio Shack, (248) 357-3190 can help you with some
Arduino parts.) There are many "starter" books on
the Arduino platform. One of my favorites is Arduino:
A Quick Start Guide by Maik Schmidt from The pragmatic
Bookshelf. The discussion of the 3-wire Ping Ultrasonic
Sensor and Maik Schmidt's example code are used here for the
4-wire HC-SR04 with very few changes. Pictures of the setup
for my test and the test code:

This sensor seems reasonably accurate. It
was right on from 2cm to 8cm and pretty close up to a meter.
At 132 cm it measured 128.25 cm. Four objects were tried.
The distances to the wide side of a glue bottle and to a
small paint can were measured more accurately than to a flat
piece of wood (about 9 mm too short at 15cm) or to the narrow
edge of the glue bottle (about 8 mm too long at 15cm.)

Converting sound travel time in
milliseconds to distance traveled in centimeters would give
the student a little better appreciation of making
measurements in the physical world. Adjusting the speed of travel
of ultrasound waves through air for the temperature of the air would
enhance this appreciation.