Programming the Ultrasonic sensor


yes it works,thank you!


I am trying to help my11y/o son enhance his mbot ranger. his idea is to have the bot follow a person/pet using the motion sensor on its own. Any ideas?


Motion sensor is something different:
It can detect motion well, but not the direction.

I guess you want to use the ultrasonic sensor because you added to this thread.
However, this will not work very well because the sensor can only detect the distance to objects in front of it, but not motion.


It came with the Ultra Sonic sensor. He developed an obstacle avoiding robot using it within 48 hours of assembly. So now he wants to know what we need to make it more autonomous. So any ideas in that regard will help. I recently purchased the MeCompass and MePIR. Any suggestions?


Without having it tried myself:

Try limit the field of view of the MePIR sensor, for example by putting a small cardboard tube over it. Mount it on the front of the bot so it looks forward. If the bot detects movement in front of it using the MePIR sensor, drive forward until you get close to the moving object (measure distance using the ultrasonic sensor).

If there is no movement, turn the robot a bit to scan the next field of view. Then look for movement again, repeat.

You probably can not check for movement with the MePIR sensor when the bot is moving, so do that only when the bot stands still.


problem with ultrasonic sensor on ultimate 2.0
no matter where I positon the robot, the ultrasonic sensor distance is 0. I have tried port 6 and 7.
I did a firmware update and reset and no change.
I have a simple mblock program
repeat (10)
say (ultrasonic sensor (port 7) distance)
wait (3) secs
say (hello)
wait (2) seconds
end of repeat

The ultrasonic is the only thing plugged into ports 5-8


turned everything off and tried again. Now the distance value is always 400.



The 400 value indicates that nothing is in range. What robot are you using? I’ve just run the same code with an mBot and it seems to work fine.


I took the ultimate totally apart and rebuilt it. The ultrasonic sensor then worked.

I have noticed that the sensor expects a full face on contact with an object. A 45 degree angle and it reports a reading 10x the true value. With the tank making a true 90 degree turn is challenging.


Remember that the ultrasonic sensor uses high frequency sound waves that reflect off of a hard surface. When an angled surface is encountered, the sound waves will be reflected at an angle, much like rolling a ball at an angled corner, which has the effect of dissipating the strength of the reflected sound waves thus affecting the reading from the ultrasonic sensor.

True 90 degree turns, whether using tank treads or wheels on motors that do not have encoders is always challenging because the turns must rely on timing and this is affected by a number of factors (surface the robot is on, battery charge level, etc.). When I did my maze-following program for the mBot, I resorted to using dark lines to mark the center of the maze cells and then used the line following sensor to detect the 90 degree turn based on the lines crossing each other. Somewhat kludgy, but it ensures that the robot turns accurately and the use of the lines also allows robot to use the line following sensor to stay roughly in the center of each maze cell.


I noticed in a few of your replies you mention “motor encoders”. My ultimate 2 has two 25mm DC Encoder Motor 9V/185RPM and one 25mm DC Encoder Motor 9V/86RPM and four MegaPi Encoder/DC Motor Drivers.


Yes, a lot folks don’t use motor encoders when they are available. The encoders will tell you how many rotations the motor shaft has completed and this number can be used to calculate the distance that a wheel travels. However, even if one is using encoders other factors often come into play that preclude getting an exact turn. Speed is one of those factors where a robot moving at a slower pace tends to turn more accurately than a robot moving at a faster pace. You didn’t mention whether or not you were using the encoders so I did not assume that you were. :slight_smile:


Hi everybody! Have any tried to connect Ultrasonic ranging module HC-SR04 to mBot/mCare?


I connected HC-SR04 to Ultimate 2.0 robotic arm tank and they are working like a charm.
HC-SR04 has 4 pins: GND, Trig, Echo, Vcc. I recommend to connect together Trig and Echo. In this way you will use only one signal pin of the main board, whatever is in mBot or Ultimate. To be able to use this modified sensor you must include library NewPing.

#include <NewPing.h>

NewPing library ( can be downloaded from here.
Now you have to build a cable with 3 wires with a connector to fit HC-SR04 pins and at the other end you have to install a RJ25 male connector (this is similar with RJ11, telephone connector, but has 6 contacts). Look now to the RJ25 female connector from main board, where you intend to connect the sensor. Insert now the free end of the three wires cable in RJ25 male connector in such way to fit what is written on main board. The wire which is connected to Trig and Echo will go, on main board, to a signal pin which is marked with Ax (x is a number).
You can use the following code to test sensor functionality:

//this is a code for using a 4 pin hc sr04 sensor as a 3 pin ping sensor
#include <NewPing.h>
#define PING_PIN Ax // Arduino pin for both trig and echo
NewPing sonar(PING_PIN, PING_PIN );
void setup() {
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay
unsigned int uS =; // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // convert time into distance

This is all.


Thank you for well detailed answer. I’ll try it.


Programming the Auriga ultrasonic and 3-axis gyro.
In some cases, the ultrasonic sensor will not recognize some obstacles, e.g. In too steep an angle on obstacles drives or curtains he also has some problems. Therefore I programmed the x-axis and the y-axis of the 3-axis gyro sensor in addition to the ultrasonic sensor.
In addition, he changes the direction after 5 seconds driving straight ahead. I also donated a small music gimmick to him.
Have fun with it.sonic_v14_robo_xy_motion_5_sek_new_dir_mukke.sb2 (77.4 KB)