Abdul_iot_4
Abdul_iot_4
Experiment 4
1. Aim:
Objective
An ultrasonic Sensor is a device used to measure the distance between the sensor and an
object without physical contact. This device works based on time-to-distance conversion.
Input/Equipment Used-
Procedure-
Connect HC-SR04:
VCC to 5V.
GND to GND.
Trig to Pin 9.
Echo to Pin 10.
Connect LCD:
VCC to 5V.
GND to GND.
SDA to A4.
SCL to A5.
DEPARTMENT OF
Connections:
1. Connect the Echo pin of the sensor to the D2 pin of the Arduino.
2. Connect the Trig pin of the sensor to the D3 pin of the Arduino.
3. Navigate to Tools and select board and port.
4. Verify and compile the code, then upload the code to the Arduino Uno R3 board.
5. Monitor the output in the Serial monitor (Set the baud rate as 9600). To open Serial
monitor Tools>Serial Monitor or (Ctrl+Shift+M).
CODE
#define ECHO_PIN 2
#define TRIG_PIN 3
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
void loop() {
float distance = readDistanceCM();
delay(100);
Result
Conclusion
The project successfully demonstrates distance measurement using an ultrasonic sensor and an Arduino with an
LCD display. By connecting the HC-SR04 sensor to the Arduino and interfacing it with a 16×2 LCD via I2C, the
system measures the distance to an object. The ultrasonic sensor emits sound waves, which reflect off the object
and return, allowing the Arduino to calculate the distance based on the time of flight. The calculated distance is
then displayed on the LCD in centimeters. This setup provides a simple and effective means to measure and
display distances in real-time.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING