Ultrasonic Sensor Interfacing With Arduino

Interfacing an Ultrasonic Sensor with Arduino

Components Required:

A) 1 Arduino UNO

B) Computer with Arduino IDE

C) USB to Arduino connecting Cable

D) Jumper Wires

E) Ultrasonic Sensor

Description:

In this experiment, we learn how to interface and take values from an ultrasonic sensor by getting the distance from an object.

Circuit Diagram:

Ultrasonic-Interfacing-Sensor with-Arduino

Steps To Follow::

A) Establish the given circuit.

B) Open Arduino IDE.

C) Click on File.

D) Go to New. – A new File will open

E) Remove the Pre-Written code..

F) Paste the code given in the “CODE Section”

G) Check whether your UNO has been detected by the ID by going to Tools.

H) Click on Port – Select the COM-Port on which Arduino UNO is detected.

I) Verify the Sketch by Clicking on the TICK icon.

J) Once, the sketch is verified, Upload the sketch using the ARROW icon.

K) Open the Serial Monitor(Magnifier Symbol > Top Right) to view distance values.

Code:

// defining the pins
const int trigPin = 9;
const int echoPin = 10;
// 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);
}

Leave a comment