Mbot ranger ultrasonic sensor stays at 400


Using the built in firmware for the mbot ranger the ultrasonic sensor works fine. When I try using my own code it only returns a value of 400. As a test I put this code in the setup routine and tried moving my hand close to the sensor but the value never changes.

float distance=0.0;
MeUltrasonicSensor *us = NULL;

if(us == NULL)
us = new MeUltrasonicSensor(3);
else if(us->getPort() != 3)
delete us;
us = new MeUltrasonicSensor(3);
{distance = distance = (float) us->distanceCm();


Think your port is wrong. Port 3 is a motor driver on the ranger. Try plugging it into Port 10 and running this:

#include <MeAuriga.h>

MeUltrasonicSensor ultrasonic(PORT_10);

void setup()

void loop()
Serial.print("distance(cm) = "); // Print the results to the serial monitor
Serial.println(ultrasonic.distanceCm()); // Distance value from 3cm - 400cm
delay(50); // Wait 50 milliseconds before next measurement


This document might help you further:


That solved the problem, thank you.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.