Hi,
I'm using the SR04 sonar to measure distance to different objects.
Here is my code:
#include <NewPing.h>
int trig = 4;
int echo = 5;
float distance = 0, ping = 0;
float speedOfSound = 0.34;//m/ms
//NewPing sonar(trig, echo, 100);
void setup(){
Serial.begin(9600);
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
}
void loop(){
distance = getSonar();
Serial.print("|DISTANCE: ");
Serial.println(distance);
}
float getSonar(){
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
ping = pulseIn(echo, HIGH);
ping = ping/1000;
ping = ping /2;
return speedOfSound*ping*100;
}
I also used NewPing's sonar.ping_cm() function.
This works fine when measuring distances to objects like walls, but doesn't work when measuring distances to humans. The measured distance to a human is 3200 with my code and 0 with NewPing.
I'm guessing that the sensor never receives the echo because clothing absorbs the soundwaves instead of reflecting it. I didn't find any info about this on the internet, which is not what I expected. I though alot of people probably had the same issue?
Do you guys have any experience with this sonar sensor?