Using Multiple Ultrasonic Sensors
If you wish to use Multiple Ultrasonic Sensors on your NXT Robot, you need to program them 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 this you need to take advantage of the Sensors Mode 3: Event Capture Mode.

My Omni-Wheel Bot with 3x Ultrasonic Sensors for Object Detection
I was surprised to find that the was virtually no data on the NXT's Ultrasonic Sensor Modes available on the Internet. But with the help of the Lego Mindstorms NXT Hardware Developer Kit, Appendix 7: LEGO MINDSTORMS NXT Ultrasonic Sensor I2C communication protocol.pdf, I was able to solve it relatively easily. Consult the NXC API for using I2C Command & Control
Official Lego I2C Commands Extract for Ultrasonic Sensor:
- Single shot command:
- Continuous measurement command:
- Event capture command:
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”.
This is the default mode, where the sensor continuously makes new measurement with the specified interval.
Within this mode 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.
To use the Ultrasonic Sensor's "Mode 3" with NXC, you need to use I2CWrite(PORTnumber, 0×41, 0×03) to turn on and initialise the sensor. You can the then use SensorUS(PORTnumber) to read the distance to object. To turn the Sensor Off again, you need to use I2CWrite(PORTnumber, 0×41, 0×00). You need to repeat these commands for each Ultrasonic Sensor you are using. It pays to initialise all your Ultrasonic Sensors to the Off state when the program starts.
The following NXC Code shows an example of how to code for Multiple Ultrasonic Sensors:
Download Source Code
#define US1 S1 // Port Number
#define US2 S2 // Port Number
#define US3 S3 // Port Number
#define THRESHOLD 20
int Dist1, Dist2, Dist3;
string DIS1, DIS2, DIS3;
task main()
{
ClearScreen();
SetSensorLowspeed(US1);
SetSensorLowspeed(US2);
SetSensorLowspeed(US3);
I2CWrite(US1, 0x41, 0x00); // Set US1 Sensor Off
I2CWrite(US2, 0x41, 0x00); // Set US2 Sensor Off
I2CWrite(US3, 0x41, 0x00); // Set US3 Sensor Off
while (true)
{
I2CWrite(US1, 0x41, 0x03); // Set US1 Sensor in Event Capture Mode
Dist1 = SensorUS(US1);
Wait(50);
I2CWrite(US1, 0x41, 0x00); // Set US1 Sensor to Off
DIS1 = NumToStr(Dist1);
TextOut(0, LCD_LINE3, "US Sensor1 " + DIS1 + " ");
I2CWrite(US2, 0x41, 0x03); // Set US2 Sensor in Event Capture Mode
Dist2 = SensorUS(US2);
Wait(50);
I2CWrite(US2, 0x41, 0x00); // Set US2 Sensor to Off
DIS2 = NumToStr(Dist2);
TextOut(0, LCD_LINE4, "US Sensor2 " + DIS2 + " ");
I2CWrite(US3, 0x41, 0x03); // Set US3 Sensor in Event Capture Mode
Dist3 = SensorUS(US3);
Wait(50);
I2CWrite(US3, 0x41, 0x00); // Set US3 Sensor to Off
DIS3 = NumToStr(Dist3);
TextOut(0, LCD_LINE5, "US Sensor3 " + DIS3 + " ");
}
}
loading...
loading...

Download PDF format






Thanks for the helpful information; it’s just what I needed.
I’m not clear on what “8 objects” means in single shot mode. If it sends one ping, why does it not just return one distance.
Also, in continuous mode, what does the interval setting mean in practice.
loading...
loading...
I haven't used 'Single Shot Mode' personally as yet.
My understanding is that 8x individual readings are made of 8 objects within the US sweep, and there distances saved in 8 different registers. Basically, I2Cwrite(US1, 0×41,0×01) will instigate the 8x measurements which you then read I2Cread(US1, 0×41,0×01) to retrieve values. Checkout the NXT HDK:
Appendix 6-LEGO MINDSTORMS NXT Ultrasonic Sensor hardware schematic.pdf
Appendix 7-LEGO MINDSTORMS NXT Ultrasonic Sensor I2C communication protocol.pdf
loading...
loading...
does anybody know how to do this with the I2C Read and I2C Write block ? there used to be instructions in Nxtasy before it went down… thanks!
loading...
loading...
Have you tried the Mindboards Forum (http://mindboards.sourceforge.net/)?
loading...
loading...
Can the 'single shot' feature still be used, if the robot is being controlled from a desktop computer using C# and a library such as Aforge?? If yes, than how can it be done?
thanks
I imagine that should be possible. For more details on the subject & a younger brain, I suggest you contact Xander: Bot Bench. Xander programs for a living unlike me and uses C#.loading...
loading...
You need to give the Sensor time to read the value, plus you also need to allow for the I2C transaction between the Sensor and NXT.From memory the 50mS was the most stable. By all means play around with the value and let me know how you get on. You are going to need at least 10mS for the I2C read write exchange.
loading...
loading...