US-100 Accuracy Issues

Hello all, and thank you in advance!
I'm having an issue with my output of my new US-100 sensor. I don't have any experience using these devices, but I imagine my problem is in my code (below).
I'm using an Arduino Due connected to a US-100 Ultrasonic Sensor. I'm using 3.3v/GND, and have checked and double checked my Trigger/Echo pins. I've removed the jumper from the back, in order to use the trigger and echo function similar to an HC-SR04. When I do this the results vary -5% to +12% of the static 200mm object. I modified the code to capture hourly min/max values. I've ran the sketch for 20 hours, refreshing the hourly min/max and have posted the results below as well. Is there a way to dial this in, to make it more accurate? If it helps, my project will range from 100mm to 300mm when the sensors are implemented. Any advice is appreciated!

const int trig = 5;
const int echo = 6;
long time1, dist;
long maximum = 0;
long maximum1 = 0;
long minimum = 1000;
long minimum1 = 1000;
int Time = 0;
int Timestart = 0;
int Hour = 0;
void setup()
{ Serial.begin(9600);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
}
void loop()
{ digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  time1 = pulseIn(echo, HIGH);
  dist = (time1 / 2) / 2.91;
  if (dist > 500 or dist == 0)
    Serial.println("Out of Range");
  else {
    Time = millis() - Timestart;
    if(Time > 3600000) 
    {
      Hour++;
      Serial.print("Hour ");
      Serial.println(Hour);
      maximum = 0;
      minimum = 1000;
      Timestart = millis();
    }
    if (dist > maximum)
    {
      Serial.print("Min = ");
      Serial.print(minimum);
      Serial.print(" mm Max = ");
      Serial.print(dist);
      Serial.print(" mm ");
      Serial.println(Time);
      maximum = dist;
    }
    if (dist < minimum)
    {
      Serial.print("Min = ");
      Serial.print(dist);
      Serial.print(" mm Max = ");
      Serial.print(maximum);
      Serial.print(" mm ");
      Serial.println(Time);
      minimum = dist;
    }
    if(maximum > maximum1)
    {
      Serial.print("All time HIGH of ");
      Serial.print(maximum);
      maximum1 = maximum;
    }
        if(minimum > minimum1)
    {
      Serial.print("All time LOW of ");
      Serial.print(minimum);
      minimum1 = minimum;
    }
  }
  delay(500);
}
Min = 1000 mm Max = 201 mm 8
Min = 201 mm Max = 201 mm 8
All time HIGH of 201Min = 201 mm Max = 202 mm 514
All time HIGH of 202Min = 193 mm Max = 202 mm 3044
Min = 193 mm Max = 214 mm 6586
All time HIGH of 214Min = 192 mm Max = 214 mm 553066
Min = 191 mm Max = 214 mm 3335560
Hour 1
Min = 1000 mm Max = 200 mm 3600198
Min = 200 mm Max = 200 mm 3600198
Min = 191 mm Max = 200 mm 2024
Min = 191 mm Max = 204 mm 5060
Min = 191 mm Max = 213 mm 7590
Hour 2
Min = 1000 mm Max = 200 mm 3600190
Min = 200 mm Max = 200 mm 3600190
Min = 200 mm Max = 204 mm 1012
Min = 191 mm Max = 204 mm 3036
Min = 191 mm Max = 213 mm 3542
Hour 3
Min = 1000 mm Max = 200 mm 3600190
Min = 200 mm Max = 200 mm 3600190
Min = 191 mm Max = 200 mm 1518
Min = 191 mm Max = 213 mm 9108
Hour 4
Min = 1000 mm Max = 191 mm 3600190
Min = 191 mm Max = 191 mm 3600190
Min = 191 mm Max = 200 mm 506
Min = 191 mm Max = 201 mm 5060
Min = 191 mm Max = 213 mm 8096
Hour 5
Min = 1000 mm Max = 213 mm 3600190
Min = 213 mm Max = 213 mm 3600190
Min = 200 mm Max = 213 mm 506
Min = 191 mm Max = 213 mm 2530
Hour 6
Min = 1000 mm Max = 200 mm 3600191
Min = 200 mm Max = 200 mm 3600191
Min = 191 mm Max = 200 mm 1518
Min = 191 mm Max = 201 mm 4048
Min = 191 mm Max = 204 mm 9614
Min = 191 mm Max = 213 mm 32384
Hour 7
Min = 1000 mm Max = 191 mm 3600190
Min = 191 mm Max = 191 mm 3600190
Min = 191 mm Max = 201 mm 506
Min = 191 mm Max = 204 mm 11638
Min = 191 mm Max = 213 mm 24794
Hour 8
Min = 1000 mm Max = 200 mm 3600190
Min = 200 mm Max = 200 mm 3600190
Min = 200 mm Max = 201 mm 1012
Min = 200 mm Max = 204 mm 2024
Min = 200 mm Max = 213 mm 4048
Min = 191 mm Max = 213 mm 5566
Hour 9
Min = 1000 mm Max = 191 mm 3600190
Min = 191 mm Max = 191 mm 3600190
Min = 191 mm Max = 200 mm 506
Min = 191 mm Max = 201 mm 1012
Min = 191 mm Max = 204 mm 1518
Min = 191 mm Max = 213 mm 29348
Hour 10
Min = 1000 mm Max = 204 mm 3600190
Min = 204 mm Max = 204 mm 3600190
Min = 200 mm Max = 204 mm 506
Min = 200 mm Max = 213 mm 2530
Min = 191 mm Max = 213 mm 5566
Min = 191 mm Max = 221 mm 1917765
All time HIGH of 221Min = 191 mm Max = 222 mm 3119926
All time HIGH of 222Min = 191 mm Max = 224 mm 3183249
All time HIGH of 224Hour 11
Min = 1000 mm Max = 204 mm 3600291
Min = 204 mm Max = 204 mm 3600291
Min = 204 mm Max = 205 mm 1013
Min = 199 mm Max = 205 mm 2535
Min = 199 mm Max = 216 mm 4056
Min = 191 mm Max = 216 mm 17234
Min = 191 mm Max = 221 mm 23820
Min = 191 mm Max = 222 mm 66885
Min = 191 mm Max = 224 mm 146463
Hour 12
Min = 1000 mm Max = 206 mm 3600394
Min = 206 mm Max = 206 mm 3600394
Min = 205 mm Max = 206 mm 1014
Min = 205 mm Max = 208 mm 1521
Min = 205 mm Max = 210 mm 7098
Min = 205 mm Max = 222 mm 8619
Min = 199 mm Max = 222 mm 12168
Min = 199 mm Max = 224 mm 18251
Min = 197 mm Max = 224 mm 2635384
Min = 197 mm Max = 225 mm 3207750
All time HIGH of 225Hour 13
Min = 1000 mm Max = 205 mm 3600125
Min = 205 mm Max = 205 mm 3600125
Min = 205 mm Max = 207 mm 507
Min = 205 mm Max = 219 mm 1520
Min = 197 mm Max = 219 mm 2026
Min = 197 mm Max = 224 mm 20277
Min = 197 mm Max = 225 mm 700998
Hour 14
Min = 1000 mm Max = 197 mm 3600485
Min = 197 mm Max = 197 mm 3600485
Min = 197 mm Max = 203 mm 507
Min = 197 mm Max = 213 mm 6074
Min = 197 mm Max = 224 mm 14678
Min = 189 mm Max = 224 mm 3560894
Hour 15
Min = 1000 mm Max = 203 mm 3600362
Min = 203 mm Max = 203 mm 3600362
Min = 202 mm Max = 203 mm 507
Min = 197 mm Max = 203 mm 1014
Min = 197 mm Max = 219 mm 4556
Min = 189 mm Max = 219 mm 19230
Min = 189 mm Max = 224 mm 32893
Hour 16
Min = 1000 mm Max = 207 mm 3600297
Min = 207 mm Max = 207 mm 3600297
Min = 206 mm Max = 207 mm 507
Min = 205 mm Max = 207 mm 2535
Min = 205 mm Max = 222 mm 11154
Min = 203 mm Max = 222 mm 11660
Min = 197 mm Max = 222 mm 13684
Min = 197 mm Max = 224 mm 20276
Hour 17
Min = 1000 mm Max = 222 mm 3600163
Min = 222 mm Max = 222 mm 3600163
Min = 206 mm Max = 222 mm 507
Min = 205 mm Max = 222 mm 1014
Min = 197 mm Max = 222 mm 28391
Min = 197 mm Max = 224 mm 30923
Min = 197 mm Max = 225 mm 271710
Min = 191 mm Max = 225 mm 471944
Hour 18
Min = 1000 mm Max = 203 mm 3600314
Min = 203 mm Max = 203 mm 3600314
Min = 203 mm Max = 213 mm 1013
Min = 202 mm Max = 213 mm 2026
Min = 197 mm Max = 213 mm 3040
Min = 197 mm Max = 219 mm 7092
Min = 197 mm Max = 224 mm 19753
Min = 197 mm Max = 225 mm 220332
Min = 191 mm Max = 225 mm 902226
Hour 19
Min = 1000 mm Max = 200 mm 3600427
Min = 200 mm Max = 200 mm 3600427
Min = 191 mm Max = 200 mm 1518
Min = 191 mm Max = 201 mm 8096
Min = 191 mm Max = 213 mm 10120

Don't imagine things, verify things. Start by Serial.print() the raw values you are getting from the sensor. Forget the calculations until you know more about what the sensor is doing. Do the raw values change with the reflector distance? Are they linear changes with linear distance changes?

That seems about right. There might be an offset, which can be corrected by calibrating the sensor against some known distances.

Average some number of values to reduce the noise (the noise is reduced by a factor of 1/sqrt(N), where N is the number of values averaged).

Thanks Paul_KD7HB, I've modified the original Serial.print() of the raw values in hopes to see how much it changes over time. As shown in the attached serial log, it would appear that the accuracy issues are constant and the min/max values are reached in the first 10 seconds. I'm more concerned about the values returned of a static object. If I can solve that, everything is a piece of pie. FYI, the first test I did was linear distance changes, and they returned linear values, albeit with accuracy issues. :worried:

Thanks Jremington,
I had a feeling I was going to have to average out the noise. I was just hoping I could mitigate it somehow without it. I think this might be the only way to guarantee a close to almost perfect distance.

If you average the readings, do the difference go away?

Yes, if I were to average them they would certainly get better. Originally I was hoping to confirm it wasn't a programming oversight. It would appear that the sensor outputs have a noise component that needs to be averaged out. Thanks for the help guys!

All sensors have noise. Careful calibration and averaging will always improve the quality of the data collected, at the expense of measurement rate.

I've been using a HC-SR04 Ultrasonic for some time. Its in my garage monitoring the vehicle height (for which vehicle or no vehicle is in that bay).

I found the best method to filter is:

  • Take an odd number of readings:
  • Sort the readings.
  • Ignore the highest and lowest reading(s).
  • Either average the rest or in my case I picked the median value.

Didn't you miss the "All time LOW"s ?

Good catch. I noticed that copy and paste error shortly after posting.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.