HC-RS04 Accuracy

Hi all,

I have a problem with the HC-RS04

When i use the Newping library the readings from the sensor are not accurate and jump so much

the sketch I'm using is:

#include <NewPing.h>
 
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 500
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
 
void setup() {
   Serial.begin(9600);
}
 
void loop() {
   delay(50);
   unsigned int uS = sonar.ping_cm();
   Serial.print(uS);
   Serial.println("cm");
   delay(500);
}

But when i don't use the library and just the pulseIn(); the data are pretty accurate and don't jump around.

The sketch is:

int trigPin = 12;    //Trig - green Jumper
int echoPin = 11;    //Echo - yellow Jumper
long duration, cm, inches;
 
void setup() {
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
 
void loop()
{
 
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  cm = (duration/2) / 29.1;
  inches = (duration/2) / 74; 
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(250);
}

Am I doing something wrong or this behaviour is normal?
If so, how to overcome the issue?

You seem to have ignored everything you have been told about naming variables. You're asking for help. It's unfair to burden us with totally misleading variable names like this one:

   unsigned int uS = sonar.ping_cm();

Please clean up your code before you post it here. Make it easier for us to help you.

In this particular I'm using the exact code that I found on the arduino web. And did not change anything just to test the sensor.

amirrajabifar:
In this particular I'm using the exact code that I found on the arduino web. And did not change anything just to test the sensor.

I went to the github library source page. I recommend that you do the same. The example sketch there isn't the same. It looks like this:

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

Thanks for the link :slight_smile: It fixed my problem.

But my question still stands as to why i get readings like this:

Ping: 0cm
Ping: 191cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 105cm
Ping: 0cm
Ping: 191cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 108cm
Ping: 0cm
Ping: 189cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 104cm
Ping: 0cm
Ping: 188cm

when i use the normal method, not the one that Paul Stoffregen has on gitHub?

What normal method? In other words, why would you consider it normal? Perhaps you should give a link to "the arduino web" you mentioned previously.

http://playground.arduino.cc/Code/NewPing

I copied the "Sample NewPing Sketch"

Ping: 0cm
Ping: 106cm
Ping: 0cm
Ping: 165cm
Ping: 192cm
Ping: 0cm
Ping: 109cm
Ping: 0cm
Ping: 193cm
Ping: 192cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 121cm
Ping: 0cm
Ping: 0cm
Ping: 108cm
Ping: 0cm
Ping: 192cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 107cm
Ping: 0cm
Ping: 190cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm

and that's what i get in my serial monitor

No. You changed line 15.

I did copied the sketch again with no change what so ever and that was the serial monitor results.

Well one makes uS signed and one unsigned. Maybe the range of the signed number was exceeded. Try printing the return result before it is divided by US_ROUNDTRIP_CM.

The real home of "NewPing" is here:- New Ping

Keep in mind that "NewPing" returns 0 for overrange readings, too, so you need to discard those.
(That appears to be your biggest problem.)

'ping_cm()' will usually return a good result, but if you're getting some bad readings, you could try 'ping_median()'. It averages a number of readings, (default = 5), and discards out-of-range readings, then returns the result in uS.
Then: 'convert_cm(echoTime)' will convert echoTime from microseconds to centimeters.

All fully documented on Tim Eckel's "NewPing" page.

If you're still having trouble, Tim has a "NewPing" thread in the "Sensors" section.
It's here:- NewPing Library Project Homepage
He usually responds to questions.

Thanks everyone!