How to test if an ultrasonic sensor is fine


Some users may have the issue that the robot fails to avoid obstacle as it should be, then after checking firmware information, connection and other factors, it is necessary to check whether the ultrasonic sensor is still working fine. Here I’ll include three methods to test the ultrasonic sensors, with mBlock 3 software, mBlock 5 software and Arduino IDE. By the way, here I take mCore (mBot) as an example.

  1. Test with mBlock 3. You can download here and install this software:

    Once installed, please upgrade firmware for the mBot first (refer to mBot FAQ1)
    Then run below online codes:

  2. Test with Arduino IDE. In mBlock 3, you can drag mBot program, the right click on it, choose “upload to arduino”:

Then it shows below picture, please choose edit with arduino ide:

Then go to files->examples->makeblockdrive->meultrasonicsensor->ultrasonic sensor test->open it:

Then choose board and port under tools:

If you have mBot, choose Arduino/genuino uno. Also change the codes as below:
Then press upload icon:

Once upload, choose the serial port monitor:

Then we’ll see below:

  1. Test with mBlock 5 the one that you installed.
    Connect USB from mbot to computer with USB cable, then update firmware:

Then code like below: (you need variable)

Or go to Sprite, code like this:

mBot Ultrasonic Sensor not working

Hi, I am also having the issue that the ultrasonic sensor is not working properly. I uploaded the factory settings and in obstacle-avoidance mode the bot just goes straight bumping into the next obstacle.

I build the small program above to test the sensor and there it did show me the distance in cm quite accurately. So how come the sensor works when being tested and not when in operation?


Hi mBotDaddy,

Since the distance values are accurate when tested online. The sensor should be okay.
For the issue that the obstacle avoidance mode doesn’t work in factory settings, could you reset default program for the robot again refer to this instruction.

If the obstacle avoidance mode still doesn’t work, I suspect the robot lacks of the power, please change totally brand new batteries or fully charged lithium battery for it have a try.

Mblock 'say' block not available

I have the opposite problem. I have mBot2 with ultrasonic sensor2. It always reports distance=0 and leds are always 100% bright.
In the sample obstacle avoidance mode it just turns left and right staying at the same place.

Is it a faulty sensor?


@slava How is your sensor connected to your mBot? Is the mBuild cable plugged in to the mBot Neo body and the sensor, and does the LED ring around the sensor’s eyes illuminate when it is powered on?


Hello, thanks for the fast reply.
Yes, I am using mBuild cable to connect the sensor with the mBot2 body according to the instruction. The LED rings are illuminate with blue light when it is powered on.


@slava Have you uploaded a test code? The code could be something like this:


Try that and let me know what you get. Thanks, and good luck!


Yes, I try something very similar and it always shows me 0.
I also tried to change the brightness of the LEDs. It doesn’t work too.


Hi, I reassemble the bot using other cable and not it works somehow! The funny thing is that LEDs are now off. Will play a bit with it.


@slava I wonder if your other cable was stretched or abused in any way, or just defective. This sometimes occurs. The change in illumination indicates that the cable was giving the sensor power but no communication.


Thank you very much for your assistance!


@slava Of course, no problem! :slight_smile: You can change the light with the blocks in the extension as shown:


Good luck!


I just bought an mBot and find that the ultrasound sensor isn’t working. I discovered this because the built-in obstacle avoidance demo doesn’t work: the robot just keeps going forward. The LED on the ultrasound board is red. I have tried swapping out the cable with that of the line sensor (which works) and the ultrasound still doesn’t work.

I followed the instructions above to read the sensor using Arduino. Here is the program:

#include "MeMCore.h"

MeUltrasonicSensor ultraSensor(PORT_3); /* Ultrasonic module can ONLY be connected to port 3, 4, 6, 7, 8 of base shield. */

void setup()

void loop()
  Serial.print("Distance : ");
  Serial.print(ultraSensor.distanceCm() );
  Serial.println(" cm");
  delay(100); /* the minimal measure interval is 100 milliseconds */

and the output on the serial monitor:

Distance : 400.00 cm
Distance : 400.00 cm
Distance : 400.00 cm
Distance : 400.00 cm

I am using online firmware 06.01.107. when i use the mblock software to set a variable from the ultrasonic_sensor on Port3, the variable also always reads 400.

do i have a defective sensor? if so, how would i go about replacing it?


@ali_rahimi Does the block code work? Try using other ports, and make sure that the sensor is plugged into the same one that is specified in the code? Let me know, and mention me as @Best_codes. Good luck!


@Best_codes, the block code also doesn’t work (“when i use the mblock software to set a variable from the ultrasonic_sensor on Port3, the variable also always reads 400”). i have tried switching the port on the hardware and on the software side to no avail. in particular, i’ve tried using the port that the line follower sensor uses (this sensor does work as expected), but the ultrasound sensor does not work on that port either.


Try a code like this:
For mBot (are you using mBot or mBot Neo / 2?).
Make sure that you upload the code, don’t use live mode for the test.

Good luck!