Ultrasonic Distance

How to add the Ultrasonic Distance robot skill

  1. Load the most recent release of ARC (Get ARC).
  2. Press the Project tab from the top menu bar in ARC.
  3. Press Add Robot Skill from the button ribbon bar in ARC.
  4. Choose the Ultrasonic Distance category tab.
  5. Press the Ultrasonic Distance icon to add the robot skill to your project.

Don't have a robot yet?

Follow the Getting Started Guide to build a robot and use the Ultrasonic Distance robot skill.

How to use the Ultrasonic Distance robot skill

Ultrasonic sensors use sonar (sound navigation ranging) to determine the distance to an object or wall, the same way some animals like bats and dolphins do. They offer excellent range and accuracy in an easy-to-use package. Unlike infrared sensors, their operation is not affected by sunlight or black materials. However, they have trouble with soft fabrics; the sonar echo works ideally when bouncing off hard surfaces.

When an HC-SR04 or equivalent Ultrasonic Distance Sensor is connected to an EZB, this skill will display the distance of a detected object/wall in front of the sensor. This skill does not cause a robot to steer or avoid obstacles; it merely displays a distance value. For object avoidance and steering use the Radar skill.

This skill is integrated with scripting and will update when GetPing() is called (with matching port configured) and the PAUSE checkbox is checked. That way, you can have this skill on your project, but it won't use any communication resources unless it's called from a script with GetPing(). You can leave the PAUSED checkbox checked; this skill will be updated when GetPing() is called.

It's worth noting that the distance displayed is not a unit of CM or Inches. The distance value is an arbitrary number related to the sensor type, voltage, detected material, and temperature. If a specific distance unit is required, use a tape measure to identify the distance value.

Main Window

1. Ultrasonic Distance Value
This value between 0-255 corresponds to the detected distance sent by the ultrasonic sensor.

2. Bar Display
Displays a visual representation of the detected ultrasonic distance value.

3. Pause Checkbox
This checkbox pauses the capture of the ultrasonic distance value.


1. Title Field
This field contains the title of the skill. You can change it if you'd like. *Note: Changing the title will also change the title in the controlCommand() associated with this skill.

2. Board Index Drop-down
Select the EZB index number that your ultrasonic sensor is connected to.

3. Trigger & Echo Port Drop-downs
Select the digital port that your ultrasonic trigger wire is connected. Also, select the digital port that your ultrasonic echo wire is connected. *Note: This port must be the same as the trigger port when using a 3-wire Ultrasonic sensor.

4. Interval Drop-down
This drop-down selects the interval rate in milliseconds at which the ultrasonic sensor will be sampled. The range is 100-60000 milliseconds. The default setting is 250.

5. Variable & Multiplier
A variable will be set with the most recent distance value. This saves your code from calling GetPing, which adds needlessly to the communication channel.

The multiplier value will convert the raw distance value to a unit of measurement. For example, the default value of 1.25 will convert the distance into CM.

6. NMS (Navigation Messaging System)
The distance data can optionally be configured to push detected distances into the NMS (Level #3 Group #1) to be used by navigation robot skills, such as The Navigator. Please read the NMS manual and question marks associated with the options for better detail.

Wiring Diagram

Ultrasonic Distance Sensors come in 2 versions. A 4-wire type with/without an inline voltage regulator and three wires with/without a built-in voltage regulator. If an Ultrasonic has a voltage regulator, it will need +6V or greater Voltage input; if it doesn't, it will need +5V.

3-wire Wiring (with built-in regulator)
  • Ground = Black wire to GND
  • Power = Red wire to Vin
  • Trigger/Echo = white wire to Digital pin
4-wire Wiring (with regulator)
  • Ground = Black wire from regulator to GND
  • Power = Red wire from regulator to Vin
  • Trigger = white wire to Digital pin
  • Echo = green wire to a second Digital pin

Fix Resolution

If you use an EZ-Robot ultrasonic distance sensor, there may be false positives from the ping sound echoing within the casing. It is easy to fix with a few cotton balls inserted in the case around the sensors. Watch this video for detailed instructions.

Tech Details


Synthiam has a ultrasonic hardware reference design here.


Upgrade to ARC Pro

ARC Early Access will give you immediate updates and new features needed to unleash your robot's potential!

#85   — Edited
Yeah, that's it. So the knockoff sensors are super slow and timeout. That's a good thing we discovered because the ping sensor is a blocking function on microcontrollers and will pause all communication while waiting for the echo pulse to complete. Those sensors would make any robot super duper slow because there would be a >2ms delay for every reading. Wow, those are terrible sensors. Two milliseconds in electronics is forever, specifically when blocking communication or other I/O commands. A slow sensor would result in robots driving into walls because it can't send communication fast enough while it waits for the sensor readings to complete.

I'm guessing the microcontroller programmers of the knockoff sensors didn't know how to write the code efficiently. So their code takes too long to execute and blocks for >2ms. 

While Arduino's "pulseIn()" on the Arduino EZB Firmware will wait up to 3 minutes, it would work with the knock-off sensors. However, I would not recommend it because the blocking time waiting for an echo will cause significant performance issues.
What am I welcome for?
For thanking me for finding the issue. I told you that you would need to hire me one day .:)
This was not an issue.
Guys, so what brand of ping sensor should I buy for my EZ-Bv4? I tried an Elegoo and that didn't work.
#92   — Edited
The EZ-B v4 natively supports the EZ-Robot ping sensor because it is their sensor. As for the current sensors you are referring to, you can use the Arduino EZB firmware. However, if you use the Arduino EZB firmware with those knock-off sensors, you will experience communication delays.
Smraza, HiLetgo, Elegoo, EpLzon, 10Gtek, Aoicrie, Aceirmc, and dozens of others seem to be made by the same factory. Is there some specification that I need to look for?
I see. OK. So I need to buy sensors from EZ-Robot for $27 USD each. Is that what you are saying?
EZ-Robot has products that are known to work with their EZB controllers. So, I would recommend those if using an EZ-Robot controller. You can use the sensors you mentioned with different EZB firmware, such as the Arduino EZB firmware. 

I do not work for EZ-Robot and can't speak on their response - but as a user like yourself, I would assume they would prefer you use their sensors for a positive experience because they guarantee they work. As you have experienced, many knock-off sensors are available that do not conform to the ping HC-SR04 standards.

I got it. I guess I'll stick with my first solution of using the Arduino with Arduino code to monitor the sensors and then feed that to the EZ-B. I want to keep the EZ-B with my current project so that I can plug in a camera, output sound, and have all of those 24 digital I/O ports. Yes, I know I could use an Arduino Mega or some larger version of Arduino with a wireless camera and Bluetooth speakers. 

I suppose altering the EZ-B to allow the cheaper ping sensors would be a large & costly upgrade for EZ-Robot and probably out of the question. Yes?

I don't know of a larger Arduino with wireless, but an ESP32 would work. The ESP32 firmware doesn't currently support HC-SR04, but I have added it to be added in a future release. All ESP32 Arduino's are wifi enabled and come in various form factors. There are an unfathomably high number of ESP32 form factors, so you'll have to decide what has the appropriate I/O for your needs.
#99   — Edited
While I would first and foremost recommend the EZ-Robot Ultrasonic, thank you @DJ and @Nomad, as it is tested to work 100% with the EZ-B. I can see it being quite expensive if you are trying to use an array of these sensors on a DIY robot.

Here's what you need to know when purchasing HC-SR04 ultrasonics. All of the new style sensors are removing components to make more profit. It looks like the removal of these components causes these sensors to be slower (>2mS wait times per pulse). The older version sensors you want still have the load caps by the crystal and still have transistors on board.

User-inserted image

If these components are missing you have a new and NOT-improved sensor. 

It's not in my best interest to send you back to amazon, as we would love everyone to continue to support EZ-Robot with your hardware purchases, but I also don't like to be a gatekeeper so I'll say this: These sensors are still available online, I found some myself a few minutes ago that are $4CAD each, you have to wait a week to get them though.

Sorry Thomas, changing the EZ-Bv4 to support a sensor that we don't sell isn't in the cards.
#100   — Edited
@Dj and @Jeremie, I for one appreacheat all the time and effort you two put into this issue. It shows that you want to keep your product's quality high and you really care about us as users. I've never been disappointed in the support you all have offered. In fact you're commitment to keeping your products the best in the business and your customers on all levels up and running has alway been and continues to amaze me. I've never seen anything like this in any other company out there. 
Thank you!:)

@TMesserschmidt, I hope you write about that in one of the articles you author.
hi dave 

i agree with you . use parts that belong to a brand .
dj&jeremie well done masters .
If I were to use EZ-Robot's sensors, it would be an expensive project because I need 10 ping sensors.:(
I'll take a look for the ping sensors that Jeremie is suggesting. 

Thanks to everyone who assisted with this issue. 


maybe you get a discount if you by 10 sensors from ez robot .:)
if you would by 10 random sensors , howmany arduino would you need to ,
get them all to work ?