RobotC: Using Multiple Ultrasonic (Sonar) Sensors

PDF Printer    Send article as PDF   
When using Multiple Ultrasonic (Sonar) Sensors together, you need to configure the Sensors to use ‘Single Shot Mode’, so only ONE Sensor is taking a measurement at any time. If you have all the Ultrasonic Sensors on at once they interfere with each other and give incorrect readings. To get around these issues, you need to take advantage of the LEGO Sonar Sensor’s Mode 1: Single Shot Mode.

Sonar Sensor

In this mode, the ultrasonic sensor will only make a new measurement every time the command byte is send to the sensor. The sensor will measure distances for up to 8 objects and save the distances within the “Read measurement byte 0 – 7”.


The full range of Sonar Sensor Modes are available as a result of the fact that the LEGO Mindstorms NXT Ultrasonic Sensor is a ‘Digital Sensor’ and uses the I2C Communication Protocol. The sensor uses the low-speed communication protocol which is used for communicating with the NXT Ultrasonic Sensor. Not all of the commands are accessible within the standard software’s, but the Ultrasonic Sensor itself supports these commands and functionalities.


Continuous measurement command:

In ‘Continuous measurement command’, which is the default mode, the sensor continuously makes new measurement with the specified interval.

Event capture command:

Using Event capture command the sensor will measure whether any other ultrasonic sensors are within the vicinity. With this information a program can evaluate when it is best to make a new measurement which will not conflict with other ultrasonic sensors.

3x Sonar Sensors

Third Party Driver Suite:

The standard RobotC comes with a lot of built in drivers for a wide variety of sensors. However, the standard RobotC doesn’t expose all of the functions that some of the sensors have available. To use the Sonar Sensor in ‘Single Shot Mode’ with RobotC, you will need a copy of Xander Soldaat’s ‘Third Party Driver Suite‘.

I also so suggest you download a copy of Xander Soldaat’s ‘RobotC Third Party Driver Suite Tutorial‘ which is an excellent resource. For more information relating to Xander Soldaat’s  ‘Third Party Driver Suite‘, visit his blog, ‘Bot Bench‘.

The following RobotC Code shows an example of how to code for Multiple Ultrasonic Sensors:

Download Source Code



If you wish to use the HiTechnic Sensor Mux (HTSMUX) to connect the Sonar Sensors to the LEGO Mindstorms NXT Brick? Use the code below:

The following RobotC Code shows an example of how to code for Multiple Ultrasonic Sensors:

Download Source Code



Previous Post

PS2 Controlled Holonomic Platform Using RobotC

Yes, another Holonomic Platform. My son, Anthony requested a Holonomic robot to take to school for show-and-tell. So here it is with a few scholarly extras, including the [robotc] code. ... Read more

Next Post

RobotC: Autonomous Holonomic Platform using 4x Sonars

This Autonomous Tri-Drive Holonomic Platform is fitted with [rotacaster] and uses 4x Ultrasonic Sensors for navigation. The RobotC Code is based on my previous post on using multiple Sonar Sensors ... Read more

Short URL:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do NOT follow this link or you will be banned from the site!