Lawrence Technological University
College of Arts and Science
Department of Mathematics and Computer Science


The NXT ultrasonic sensor and the Arduino

by John M. Miller M.D.

   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.

  1. 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:
  2. 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.

  3. Strengths of the two ultrasonic range finder modules:
    and Accuracy
    Cost Hardware Software STEM
    NXT Ultrasonic Sensor similar more expensive 9 volt and 4.3
    volt power,
    I2C network and
    special connector needed
    digital answer
    available directly
    sensor details
    HC-SR04 Ultrasonic Sensor similar less expensive simple hardware
    digital answer
    distance = speed × time,
    speed = 331 M/S + 0.6 * °C and
    optionally floating point math

Revised February 3, 2012