Interfacing Ultrasonic Sensor with Arduino

                          Ultrasonic Sensor


The Ultrasonic sensor is an electric device that measures the distance of a target object by emitting ultrasonic sound waves and converts the reflected sound into an electrical signal.

Ultrasonic waves travel faster than the speed of audible sound.

Ultrasonic sensors have two main components ;

1.the transmitter (which emits the sound using piezoelectric crystals )

2. the receiver (which encounters the sound after it has traveled from the target

in order to calculate the distance between the sensor and the object, the sensor measures the time taken between the emission of sound by the transmitter to its contact with the receiver 

Formula:-  D=1/2 T * C

D= distance , T= time , C= speed of sound wave (~343 m/s)


it has a total of four pins 

VCC > 5V

GND>GND

TRIG>(transmitter pin) 

ECHO>(receiver pin)






Interfacing Ultrasonic Sensor with Arduino




Circuit : 





VCC > 5V

GND>GND

TRIG>11 

ECHO>12


CODE :

 /*
* Ultrasonic Sensor HC-SR04 interfacing with Arduino.
*/
// defining the pins
const int trigPin = 11;
const int echoPin = 12;
// defining variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
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;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

Comments

Popular posts from this blog

Interfacing LDR with Arduino

interfacing fingerprint sensor with arduino