Arduino with Ultrasonic Sensor HC-SR04
The HC-SR04 ultrasonic sensor uses SONAR to determine the distance
of an object just like the bats do. It offers excellent non-contact range
detection with high accuracy and stable readings in an easy-to-use package from
2 cm to 400 cm or 1” to 13 feet.
The operation is not affected by sunlight or black material,
although acoustically, soft materials like cloth can be difficult to detect. It
comes complete with ultrasonic transmitter and receiver module.
Follow the circuit diagram
and make the connections as shown in the image given below.
This code returns the
distance in Inches... I think it's more accurate than other code I have seen
online and returns more usable results.
Write below program
const int t1
= 7;
const int p1
= 8;
void setup()
{
Serial.begin(9600);
pinMode(p1, INPUT);
pinMode(t1, OUTPUT);
}
void loop()
{
long duration1, cm1;
giveSignal(t1);
duration1 = pulseIn(p1, HIGH);
cm1 = microsecondsToCentimeters(duration1);
Serial.print(cm1);
Serial.println("cm1 ");
delay(1000);
delay(1000);
}
long
microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
void
giveSignal(int pingPin)
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
}
Now upload your program in arduino but much time ultrasonic sensor give a some fluctuation in distance measure...
so we can make more efficient code for to get some exect values....
}
Now upload your program in arduino but much time ultrasonic sensor give a some fluctuation in distance measure...
so we can make more efficient code for to get some exect values....

Comments
Post a Comment