Programming the Ultrasonic sensor


I’m trying to get the ultrasonic sensor to work, but I don’t know where to put the block for it or what to do with it at all. I only have the most basic things down for programming the mBot, I can get it to move around and such. If anyone has any information it would be extremely helpful.


Which robot are you using? mBlock has a block for Ultrasonic Sensor on [Port 3] distance that will return the distance in centimeters. You’d need to create a variable to capture the value or display the value by dropping it into a say block if you want to see it. You can also use the value for mathematical or logical operations as well.

Hope this helps.


I wanted to use this to find out what the sensor was seeing as I wanted to see why my “beep when you get close to the sensor” code wasn’t working but when it says “say” where does the “readout” of the values actually appear?


Add a delay after the ‘say’ to see the message. It should appear in a ballon over the panda.


I have much better success with the Orion Program code start block than I do with the start flag. I’d write it like this:

Does that help?


Yes, I have better luck uploading a program than running the sprite as well.


I’ve had the same issue when running through the mBlock environment. The serial lag time is enough to throw off accurate readings. :slight_smile:


Hi Guys,

Am running to problem and I just can’t figure it out.

My Ultrasonic sensor is not working. However, it is connected on port 3. Any suggestions?


Can you post the code you are using to test the ultrasonic sensor?


Hi Chuckmcknight!

I figured it out. I needed to reboot my computer so it could work -.- sorry!



Actually, I need your help. The ultra-sensor was working for a few but now it is not working. I have connected the cable on port 3. If you can help me, I will be so thankful!


I’ve found that when I have sensors acting up it is usually time to go through the Upgrade Firmware + Reset Default Program cycle to clear out anything that might be hanging around in memory. After you do that, try running your program again.


how did you get set distance?


from control? its not there


this happens


Hi Ahmet,

I noticed from your screen shot that the robot does not appear to be connected to mBlock.

You can only use the Scratch block ‘say’ when you are either tethered via USB cable or if you are connected via WiFi or Bluetooth. You have to start the program with a when Clicked hat block from the Events palette, not an mBot program hat block from the Robots palette.

Also note that the shape of the ultrasonic sensor block indicates that it is a ‘reporter’ block which will return the current value of the ultrasonic sensor. You can create a variable, or if you use the when Clicked block as mentioned above, the say block from the Looks menu.

The error dialog you are getting indicates that the Orion hat block (which indicates that the program needs to be compiled to Arduino code and uploaded to the robot) does not support the ‘say’ block. This should be expected because the robot does not have a screen.

Keep in mind that an Arduino system is not a computer, but rather is a microcontroller-based system. When you are tethered via cable (or WiFi or Bluetooth), the program operates on your computer and special commands are sent to the robot to set or get data which can then be displayed on the computer screen.


so how do i program the ultrosonic sensor?


If you are ‘tethered’ to the computer with the USB cable or via WiFi or Bluetooth, you can use the ultrasonic reporter like this:

This program will report the reading from the ultrasonic sensor on the Scratch stage.

If you are writing an untethered program that runs by itself, you would do something like this:

This program will make the robot move forward until it gets within 5cm of an obstacle.


Hello,i can upload this progam but my robot does nothing.(Robot starter kit,MeOrion)


Maybe try < 10 instead of <2.5 … does it work then?