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:
  • 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”.

  • Continuous measurement command:
  • This is the default mode, where the sensor continuously makes new measurement with the specified interval.

  • Event capture command:
  • 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 + "   ");

  } 
}
GD Star Rating
loading...
GD Star Rating
loading...
Using Multiple Ultrasonic Sensors, 7.0 out of 10 based on 3 ratings

Tags: , , , , , , , ,

The Best of the Best, in LEGO Mindstorms NXT Publications for Your Bookshelf!

 

Free counters!