Can't figure this out...ultrasonic sensor/button


Honestly, I wish there were some tutorials out there to see how to do some of the mblock programming.
I’m trying to get my ultrasonic sensor to work through programming mblock. I figure I have to make a variable and set it to the “ultrasonic sensor distance” block but I can’t do that since that block is an oval and the set variable block has a number in a rectangle which won’t accept the sensor distance block. The programming seems stiffled by various limitations like this and I can’t figure out a work around.
I have similar problems with using the button on the robot. I’m using a computer without bluetooth and I’d like to start a program with the button, but nothing seems to work with it. I’d like to use this in a classroom but I’m getting pretty frustrated.


Ok, I don’t know what I did, but after half an hour of trying things, I was able to get the oval peg into the rectangle hole. I’m still rather frustrated but should be able to move on with this one. Still wondering about the button function


My first question would be whether you are attempting to use the ultrasonic sensor through the mBlock IDE or if you are attempting to upload the program to the mBot. If you are attempting to run the program through the mBlock IDE, you need to do the following:

The program shown above will set the variable Dis to the current value of the ultrasonic sensor. The next block merely “says” the value from the Panda on the stage. You will likely want to put this into a loop from the Control palette to continuously display the value of Dis (see below).

Using the ultrasonic sensor for an uploaded program would be:

Please note that if you are uploading programs to the mBot, you will likely need to be connected via the USB cable. I’ve heard some folks claim they can use Bluetooth to upload programs, but my Bluetooth module hasn’t arrived yet so I can’t confirm whether or not that is true.

Another point about mBlock programming is that if you are aiming at uploading programs to the mBot you will be better off going into Arduino mode (under the Edit menu). This action has the effect of graying out any blocks that the code generator will not generate and will save your students a great deal of frustrations.

I’m working on an eBook that goes into a lot of these details that I hope to get out in late March / early April. Having just taught a one week camp over mid-winter break here, I’ve had the opportunity to get some great feedback from the kids (11-14 year olds) on what worked, what didn’t, etc., that will go into the book as well.

Just be patient and keep asking questions. We are all learning together! :slightly_smiling:


Thank you @chuckmcknight for the help. I was uploading it using a USB and I did get it working.
Just curious, what is IDE? Is that using it when it’s still connected to the computer?
Great tip on the arduino mode as well. That will help a lot as I plan on getting my middle school students trying it out. I look forward to the eBook. Any idea where I’ll be able to find it? I’d say that’s definitely something someone needs to make to help people like myself out.


Hi @cpautler1,

My bad, IDE stands for Integrated Development Environment. This is the mBlock programming environment.

With regard to the eBook, I’ll post a notice when it is available. Likely it will either be on Gumroad or Lulu.


This is a fabulous thread. I have been trying to figure out how to read this ultrasonic sensor for a while. If it wasn’t for me wanting to teach my kids, I would probably go straight to the Adrino. I totally understand where cpautler1 is coming from. The documentation issue starts with the Scratch program itself (base of the Mblock). Basically the MIT students who designed it really only did 80% of the work… developing this graphical face to the software coding, but no documentation.


so I just ran this simple code from chuckmcknight. No luck. I am thinking my sensor may be bad. The power light is on on the back of the sensor. Is there another light on the back of the sensor that changes or flickers when there is something in front of the sensor? Or is there somewhere I can put a meter on the ultrasonic sensor and see a voltage change to check to see if it is actually working? I really wish this thing came with a schematic.


Hi @Dorsa,

Interesting, may I assume that you were running the program through mBlock? If so, did you make sure that the mBot was connected to mBlock via the Connect menu? It should say connected at the top of the window if you are connected. I should also mention that the ultrasonic sensor will occasionally return a zero (0). In that case, you should immediately take another reading. One other thing to check is that the cables are firmly seated (there should be a snap as they seat) and to make sure that your ultrasonic sensor block is set to use the same port that your ultrasonic sensor is connected to.

Hope this helps.


I am a complete beginner and have been trying to get the ultrasonic to work on the Tank starter kit, I have been following many threads with no results, I may not be understanding the dialog. I will give this a go, but have been so frustrated with it I have virtually given up, so fingers crossed
Thanks to all you clever people who help :slightly_smiling:


I am following this thread to help resolve my similar problem.
Your comment regarding switching to Arduino mode, has flagged up “Unsupported block found, remove to continue” the block it’s referring to is the blue “say Dis” block, what will removing it do to the programme?


The say [message] block only works if you are running the program from within the mBlock environment.

There are blocks that are not supported when you use the Orion Program header. mBlock uses a code generator to translate the Scratch blocks into Arduino code that can be compiled and run directly on the robot. You are likely using the say [message] block to see what values are being returned by the ultrasonic sensor by displaying those on the screen.

Also, be aware that the ultrasonic sensor “jitters” and may return a value of zero. Normally, I will immediately re-read the ultrasonic sensor to get a good value to work from and just ignore the zero.

Below is a sample program that should work (I say should because I don’t have an Orion board yet).


That is just what I hoped for and I’m sure it will help a lot of people, judging by other threads.
Thank you so much @chuckmcknight for taking the time to help, really appreciate it :grin:


Fascinating, thanks. I am really looking forward to your eBook. I hope to be using Mbot to teach young children (12-14) and i have to say certain code/sensor/block availability inconsistencies bother me as they make things look more difficult than they should be.


First… I apologize for the slow response. I do not get much time to sit and play with this. And certainly appreciate your help.

Here is a screen shot of what I have for code… exact word for word of your earlier post. Also per your questions…I am connected, checked all the cables, verified seating and port connections… etc. When I run the program, I move my hand in front of the sensor and I see no change in the value. Only zero. (shown in screen shot) I have set up ultrasonic sensors in my past life and they tend to be sensitive. So, I expect the value to bounce around a lot even when facing an open space. I really doubt the board itself has a filter on it. So either my code is bad (which the bottom of the Adrino window (not sure what you call that) looks to be showing information read back from the processor… so I am assuming that is basically working) or my sensor is bad. Not sure…

Now I noticed with your next post you had the individual select the Me Orion board. I do have that board. But once selected, I only saw additional sensors in the list. What does that do for me? I also noticed in that post you used a read ultrasonic sensor command. But when I selected the Me Orion Board, I did not find the read ultrasonic sensor command. Where would that command be found? And when would I use it over a set command?


Could it be that your problem isn’t the sensory specifically, but rather generally that the mBot is not responding to the computer? Are other mBlock instructions successfully executing? (E.g., the “Run forward” or the “Set LED” blocks?).

I ask because I’ve found, with my mBot, that after I’ve run an Arduino program on the mBot (i.e., an mBlock sketch that starts with the “mBot Program” block, transferred to the mBot via USB in Arduino mode using “upload to Arduino” button), then any mBlock sketch running on the computer (i.e., an mBlock sketch that starts with the “when GreenFlag clicked” block) will only work after I’ve reset things by clicking “Connect/Reset Default Program”. The Default Program seems to contain the code that allows the mBot to communicated with the computer and successfully respond to its commands.


So @Dorsa, what robot do you have?

If it is the mBot, you could use the code I posted before. If it’s an Orion board, you can do the same thing. The key is that a variable named Dis was created on the Data&Blocks palette. If you do that first, it will provide the set block used to set Dis to the reading provided by the ultrasonic sensor [Port] distance block. Looking at your screen shot, that block is under the button pressed block in the Robots palette.

The code should run on the mBot, or any of the Orion board robots (Starter Robot, Ultimate Robotic Kit, etc.). Also, if the light on the ultrasonic sensor is on, then it’s getting power from the board. The questions I have are:

  1. Which port is the ultrasonic sensor plugged into?
  2. Try moving your hand very slowly toward the sensor while in front of it. Does that make a difference?

Of course, it could be a defective sensor, but that would be the last thing I would suspect.

@CoolPink also makes a good point that when you start seeing weirdness, try resetting the default program (I usually upgrade the firmware then reset the default program just to make sure).



Good thoughts. I have had very basic motion profiles work in Mblock based on time. But I went ahead and reloaded the test code to read the sensor to try out your suggestion. Unfortunately, the Reset Default Program is grey. So, I am unable to test out your thinking. I did find the points on the sensor board to put a meter to for checking sensor response. No change. I wanted to double check what I metered, so I also loaded the sample Adrino program for testing the Ultrasonic Sensor. (I really should have done this in the beginning) The screen shot below of the serial monitor kind of confirms what I am finding. I am getting straight zeros back from the sensor. I should see some bounce/fluctuation in the signal.


Just saw your post.

  1. The sensor is plugged into port 3
  2. Speed of my hand does not make a difference. I also loaded the Adrino test code for the ultrasonic sensor and the serial monitor shows a flat zero coming back with no fluctuation. see screen shot below. I did find the points on the sensor board labeled sig, 5v, and gnd. So I took my meter across the sig and gnd to see if there was any change in the 0-5v. no change.


If the “Connect/Reset Default Program” is grey, then I think that means you are not succesfully connected through the USB connection from your computer to the robot. Is “Connect/Upgrade Firmware” also grey? Under “Conect/Serial Port” do any options show?



You are right. I double checked and those items are grey when not connected. But selecting the connect/reset default program does not fix my problem. I did try again. Thank you for your help though. I went ahead and emailed tech support. I am fairly certain that the problem lies with the sensor. Which actually turns out to be a good thing… it forced me to find the test points on the board to check for the signal… so once I get a working sensor, I can show my little ones the 0 to 5 volt swing of the sensor using a multi-meter.