Ultrasonic sensor HC-Sr04 and Arduino tutorial
Hardware needed:
- Ultrasonic sensor HC-sr04
- Arduino Uno
- Jumper wires
- Breadboard
Working:
The Ultrasonic sensor emits an ultrasound at a frequency of
40,000 Hz. This sound travels through the air
medium and if there is an obstacle or an object in its path, it will bounce
back to the module. This sensor considers the total travel time and the speed
of the sound viz, 340 m/s. After successfully determining the total travel
time, we can then calculate the distance between the ultrasonic sensor and the
obstacle/object.
- You can observe 4 pins on an Ultrasonic module (HC-sr04). These 4 pins are ground, VCC, Trig and Echo. The ground and the VCC pins of the module are for the power supply whereas the trig and echo pin is for data.
- In order to generate the ultrasound, you need to set the Trig on a High state for 10us. That will send out an 8 cycle sonic burst which will travel at the speed of sound and it will be received at the echo pin. The echo pin will give the output in time that too in microseconds the sound wave traveled.
For example, if the object is 10 cm away from
the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave
will need to travel about 294 microseconds.
But what you will get from the Echo pin will be double that number because the
sound wave needs to travel forward and bounce backwards.
So in order to get the distance in cm, we
need to multiply the received travel time value from the echo pin by 0.034 and
divide it by 2.
Code:
int trigPin = 80.;
int echoPin = 7;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
// this clears the trigpin
delayMicroseconds(2);
// Sets the trigPin on HIGH
state for 10 micro seconds so that ultrasound is sent out through trig pin
//on ultrasonic module
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin,
returns the sound wave travel time in microseconds
duration = pulseIn(echoPin,
HIGH);
distance = (duration/2) /
29.1;
// Prints the distance on
the Serial Monitor
Serial.print("Distance:
");
Serial.println(distance);
}
The information that you shared in your blog is useful for any genre of people. Though you have focused on teenagers still adult can also take the benefit out of it.
ReplyDeleteultrasonic drilling