DEPARTMENT OF BVOC AUTO
EXP NO: Arduino Interfacing with Ultrasonic Sensor
AIM
Measures the distance to an object using ultrasonic Sensor and Arduino uno.
COMPONENTS AND EQUIPMENTS REQUIRED
• Arduino uno
• Data cable
• BreadBoard
• Ultrasonic Sensor
• Connecting wires
What is Ultrasonic Sensor
Ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound
waves. the ultrasonic sensor uses a transducer to send and receive ultrasonic pulses that relay back
information about an object’s proximity. High-frequency sound waves reflect from boundaries to
produce distinct echo pattern.
The ultrasonic sensor (or transducer) works on the same principles as a radar system. ultrasonic sensor
can convert electrical energy into acoustic waves and vice versa. The acoustic wave signal is an
ultrasonic wave traveling at a frequency above 18kHz. The HC-SR04 ultrasonic sensor generates
ultrasonic waves at 40kHz frequency, which is much higher than the audible range of human hearing.
The generated ultrasonic waves travel in the air to the target and reflect back to the ultrasonic
sensor.Typically, a microcontroller like Arduino is used for communication with an ultrasonic sensor.
FAROOK COLLEGE
DEPARTMENT OF BVOC AUTO
PROGRAM
#define echoPin 6
#define trigPin 7
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
CIRCUIT DIAGRAM
FAROOK COLLEGE
DEPARTMENT OF BVOC AUTO
CONNECTIONS
Arduino side Ultrasonic Sensor Side
5v pin VCC
GND pin GND pin
Pin 6 ECHO pin
Pin 7 TRIG pin
PROCEDURE
Step 1: Make connections as per the circuit diagram given above.
Step 2: Open Arduino IDE and type the above code.
Step 3: Using a USB cable, connect your Arduino board to the computer.
Step 4: Select the correct port and the board you are using.
Step 5: Upload the code
RESULT
FAROOK COLLEGE