I'm using a basic trig/echo Ultrasonic Sensor with an Arduino Uno. I get accurate readings until I cover the sensor at which point I receive very large numbers. Why is this?


int trigPin = 8;
int echoPin = 9;
float pingTime;
float targetDistance;
const float speedOfSound = 776.5; // mph

void setup() {

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);


void loop() {
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

  pingTime = pulseIn(echoPin, HIGH);
  pingTime /= 1000000; // microseconds to seconds
  pingTime /= 3600; // hours
  targetDistance = speedOfSound * pingTime; // miles
  targetDistance /= 2; // to from target (averaging distance)
  targetDistance *= 63360; // miles to inches

  Serial.print("distance: ");


Example Output

I moved my hand from 10" away until I cover the sensor

10.20 distance: // my hand is 10" away from the sensor
10.01 distance:
9.51 distance:
8.71 distance:
7.85 distance:
6.90 distance:
5.20 distance:
4.76 distance:
3.44 distance:
2.97 distance:
1.65 distance:
1211.92 distance: // my hand is now pressed up against the sensor
1225.39 distance:
1197.16 distance:
1207.43 distance:
1212.66 distance:
1204.60 distance:


I changed the amounts from inches to milimeters to get a more precise reading. I held the sensor ~100mm from a granite counter-top and quickly lowered it until the tabletop covered the front of the sensor.

distance: 103.27 // 100mm from tabletop
distance: 96.50
distance: 79.84
distance: 76.72
distance: 62.66
distance: 65.78
distance: 54.85
distance: 47.04
distance: 44.95
distance: 38.71
distance: 28.81
distance: 25.69
distance: 27.08
distance: 25.17
distance: 27.77
distance: 22.04 // sensor continues toward table but values start to increase when they would logically decrease ??
distance: 23.95
distance: 26.73
distance: 28.81
distance: 46.52
distance: 2292.85 // sensor is now flush against tabletop
distance: 2579.59
distance: 2608.75
distance: 2595.56
distance: 2591.57
distance: 2583.75
distance: 2569.87
distance: 2570.91
distance: 2600.07
distance: 30579.64 // extreme high & low values with sensor is same place against tabletop
distance: 37.66
distance: 30444.43
distance: 37.66
distance: 30674.23
distance: 38.71
  • $\begingroup$ What output do you get when you measure distance to a solid flat object, like a book or piece of cardboard? $\endgroup$ – JSycamore Jun 26 '16 at 3:07
  • $\begingroup$ Ideally these tests would be run in an anechoic chamber and you would examine the returns (signal at the receiver) with an oscilloscope. Ultrasonics are not as "clean" as is usually assumed. There are often issues with surface hardnesses, echos all over the place, beam-spread, external interference, etc., etc. $\endgroup$ – Tut Jun 27 '16 at 12:11
  • $\begingroup$ @Jacksonkr, you listed the measurement values, but not the ground truth distance. If you repeat the procedure, but use a ruler to measure the actual distance while the sensor takes a measurement, you can begin to build a simple model for your sensor, which will allow you to account for noise and bad measurements. Out of curiosity, what is your application? $\endgroup$ – JSycamore Jun 27 '16 at 12:28

You said it yourself: you're using an echo-based ultrasonic sensor.

Imagine this: you are a sensor looking at a piece of fiber optic cable. From far enough away, you see the cable as an object. You can determine how far away it is because you are looking at it.

The cable gets closer and you continue looking at the object until, at some point, it becomes coupled to you and you now begin to look through the cable.

I would imagine the same thing is happening with your hand. In time of flight sensors there's usually some "turn on" time that delays turning on the receiver to prevent the transmitter from directly driving the receiver. This turn on time corresponds with some minimum distance, inside which an object cannot be detected.

At your skin there is an interface, where some of the sound is reflected and some of the sound is absorbed. This is like a window or a two-way mirror. Depending on some properties (acoustic impedance mismatch), the percentage of sound energy that gets absorbed varies, but some always gets absorbed.

So, your hand gets closer, and the sensor sees your hand as an object. At some point you pass the minimum threshold, and the sensor no longer detects the reflection at the air/skin interface. It still listens for an echo, though, but now all the remaining sound energy is inside you.

You are now akin to a (crappy) fiber optic cable - the sound is free to bounce around inside you, reflecting off of whatever doesn't have the same acoustic impedance as your skin. Every time it hits an interface (skin/muscle, muscle/bone, etc.), some energy is absorbed and some is reflected. This is exactly how a medical ultrasound works.

At some point the sound waves scatter back to the point where they entered, and pass back through your skin and that's when the echo is recorded. The scattered path length is the distance you are seeing.

I would bet the value would change based on what part of your arm you use (not that I would recommend doing it).

Also, FYI, ultrasound jelly exists to help match acoustic impedances to minimize reflection and get more sound energy into the body.

| improve this answer | |
  • $\begingroup$ Instead of using different parts of the arm, different objects could be brought into direct contact with the sensor: a block of metal, wood, a cardboard box, foam. I also guess that the results will be different. $\endgroup$ – Bending Unit 22 Jun 25 '16 at 18:03
  • $\begingroup$ I was convinced of your logic but the results came out different. I added the details in an EDIT at the bottom of my original post. $\endgroup$ – Jacksonkr Jun 26 '16 at 14:48
  • $\begingroup$ @Jacksonkr - Granite isn't homogeneous, like water or Jell-o or pudding. It's got a crystalline structure, so again, like bones and muscle, there are things inside of it that can direct sound waves other than the back side of the material. You could try laying the sensor on its back, putting a cup on it, then filling the cup with varying levels of water, though there is the cup/water interface after the sensor/cup interface. You could also try metal rods of varying length, but the minimum distance for that would change because the speed of sound in metal is considerably higher than air. $\endgroup$ – Chuck Jun 26 '16 at 16:05
  • $\begingroup$ Yikes. I tried my laptop from the aluminum casing, solid oak table, and a wall (3/4" dry wall) with the same results. The table and wall I can understand a bit but the aluminum laptop (mbp '15) I don't quite understand. As far as I can tell I'm stuck with having to measure at 20mm+ for many things. $\endgroup$ – Jacksonkr Jun 26 '16 at 16:09
  • 2
    $\begingroup$ @Jacksonkr what exact sensor are you using? What does the datasheet say about limits to the measured value? Sensor usually operate within a certain range of values. $\endgroup$ – Bending Unit 22 Jun 26 '16 at 19:15

You should consider building a measurement model for your sensor--this will enable you to use your measurements in an effective probabilistic manner. The problem is that beam sensors are noisy, ultrasonic sensors especially so; furthermore, this sensor noise is not typically uniformly distributed.

Sebastian Thrun covers this topic quite well in Chapter 6 of Probabilistic Robotics, in fact, he considers an ultrasonic sensor explicitly. Thrun suggests that the probability distribution of measurements is combination of four different distributions characterized by beam sensor response.

Beam Sensor Model

The image above shows the four distributions and is taken from Thrun's text. The upper left is a Gaussian distribution centered on the ground truth distance. The upper right is a truncated exponentially decaying distribution for measurements that are too close to the sensor. The bottom graphs depict the uniform noise of the sensor and the max value measurement. These four distributions are combined and normalized to give a single measurement model; to do this properly, you need to build a model for every distance.

There are two takeaway points:

  1. Ultrasonic sensors are noisy/not accurate, so you probably aren't doing anything wrong; it looks like you found the minimum sensing distance of the sensor
  2. You need measurement models to inform your predictions about the robot's pose, which is important for things like control and navigation
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.