Space In HIndi

 


Pages

Popular

Ultrasonic sensor interfacing with Ardiuno UNO

 Ultrasonic sensor interfacing with Ardiuno UNO 

What is Ultrasound? 

Ultrasound is high-pitched sound wave with frequencies higher than the audible limit of human hearing. 

Human ears can hear sound waves that vibrate in the range from about 20 times a second (a deep rumbling noise) to about 20,000 times a second (a high-pitched whistling). However, ultrasound has a frequency of over 20,000 Hz and is therefore inaudible to humans.

HC-SR04 Hardware overview At its core, the HC-SR04 Ultrasonic distance sensor consists of two ultrasonic transducers. The one acts as a transmitter which converts electrical signal into 40 KHz ultrasonic sound pulses. The receiver listens for the transmitted pulses. If it receives them it produces an output pulse whose width can be used to determine the distance the pulse travelled. As simple as pie! The sensor is small, easy to use in any robotics project and offers excellent non-contact range detection between 2 cm to 400 cm (that’s about an inch to 13 feet) with an accuracy of 3mm. Since it operates on 5 volts, it can be hooked directly to an Arduino or any other 5V logic microcontrollers. 

Ultrasonic sensors Basics:

Let’s take a look at its Pinout



Vcc is the power supply for HC-SR04 Ultrasonic distance sensor which we connect the 5V pin on the Arduino. Trig(trigger) pin is used to trigger the ultrasonic sound pulses. Echo pin produces a pulse when the reflected signal is received. The length of the pulse is proportional to the time it took for the transmitted signal to be detected. GND should be connected to the ground of Arduino

How does Ultrasonic sensor works? The famous HC SR04 ultrasonic sensor generates ultrasonic waves at 40kHz frequency. Typically, a microcontroller is used for communication with an ultrasonic sensor. To begin measuring the distance, the microcontroller sends a trigger signal to the ultrasonic sensor. The duty cycle of this trigger signal is 10µS for the HC-SR04 ultrasonic sensor. When triggered, the ultrasonic sensor generates eight acoustic (ultrasonic) wave bursts and initiates a time counter. As soon as the reflected (echo) signal is received, the timer stops. The output of the ultrasonic sensor is a high pulse with the same duration as the time difference between transmitted ultrasonic bursts and the received echo signal. The microcontroller interprets the time signal into distance using the following functions

The microcontroller interprets the time signal into distance using the following functions:


Theoretically, the distance can be calculated using the TRD (time/rate/distance) measurement formula. Since the calculated distance is the distance traveled from the ultrasonic transducer to the object and back to the transducer,it is a two-way trip. By dividing this distance by 2, you can determine the actual distance from the transducer to the object. Ultrasonic waves travel at the speed of sound (343 m/s at 20°C). The distance between the object and the sensor is half of the distance traveled by the sound wave.The following equation calculates the distance to an object placed in front of an ultrasonic sensor.


Let’s take an example to make it more clear. Suppose we have an object in front of the sensor at an unknown distance and we received a pulse of width 500 µS on the Echo pin. Now let’s calculate how far the object from the sensor is. We will use the below equation. 

Distance = Speed x Time

 Here, we have the value of Time i.e. 500 µs and we know the speed. 

What speed do we have? 


The speed of sound, of course! Its 340 m/s. 

We have to convert the speed of sound into cm/µs in order to calculate the distance. 

A quick Google search for “speed of sound in centimeters per microsecond” will say that it is 0.034 cm/µs. You could do the math, but searching it is easier. Anyway, with that information, we can calculate the distance! 

Distance = 0.034 cm/µs x 500 µs 

But this is not done! 

Remember that the pulse indicates the time it took for the signal to be sent out and reflected back so to get the distance so, you’ll need to divide your result in half.

 Distance = (0.034 cm/µs x 500 µs) / 2 Distance = 8.5 cm 

So, now we know that the object is 8.5 centimeters away from the sensor.

Ultrasonic sensor interfacing with Ardiuno UNO

Start by placing the sensor on to your breadboard. Connect VCC pin to the 5V pin on the Arduino and connect GND pin to the Ground pin on the Arduino.

 And trig and echo pin of the sensor is connect to any of the digital pin of Ardiuno. Here we connect digital pin 9 to trig and digital pin 10 to echo of sensor.



Programming

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04

 #define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04 

// defines variables long duration;

 // variable for the duration of sound wave travel int distance; 

// variable for the distance measurement 

void setup()

 {

 pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT 

 pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT 

 Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed

 Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor

 Serial.println("with Arduino UNO R3");

void loop() 

{

 // Clears the trigPin condition 

 digitalWrite(trigPin, LOW); 

 delayMicroseconds(2); // Sets the trigPin HIGH (ACTIVE) for 10 microseconds 

 digitalWrite(trigPin, HIGH); 

 delayMicroseconds(10); 

 digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds

 duration = pulseIn(echoPin, HIGH); // Calculating the distance

 distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back) 

 // Displays the distance on the Serial Monitor 

 Serial.print("Distance: ");

 Serial.print(distance); 

 Serial.println(" cm");

 }

No comments: