Aditi Iot 4
Aditi Iot 4
Experiment - 4
2. Objective:
• Learn about interfacing.
• Learn about IoT programming.
3. Hardware Used:
• Arduino Uno R3 board
• Ultrasonic sensor (HC-SR04)
• Jumper Wires
4. Introduction :
• Arduino: It is an open-source electronics platform. It consists ATmega328 8-bit Micro
controller. It can be able to read inputs from different sensors & we can send instructions to
the micro controller in the Arduino. It provides Arduino IDE to write code & connect the
hardware devices like Arduino boards & sensors.
• Ultrasonic Sensor: 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-todistance
conversion.
5. Procedure:
• Connect the Echo pin of the sensor to the D2 pin of the Arduino.
• Connect the Trig pin of the sensor to the D3 pin of the Arduino.
• Navigate to Tools and select board and port.
• Verify and compile the code, then upload the code to the Arduino Uno R3 board.
• Monitor the output in the Serial monitor (Set the baud rate as 9600). To open Serial monitor
Tools>Serial Monitor or (Ctrl+Shift+M).
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Code:
# define echoPin 2
# define trigPin 3
7. Result:
In the code, the “duration” variable stores the time taken by the sound wave travelingfrom the
emitter to the receiver. That is double the time to reach the object, whereas the sensorreturns the
total time including sender to object and object to receiver. Then, the time taken to reach the
object is half of the time taken to reach the receiver. so we can write the expression as,
Distance = Speed of Sound in Air * (Time Taken / 2)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
8. Learning outcomes:
Understand how to configure ultrasonic sensor pins using #define for easier pin management.
Learn how to use pinMode() to set pins as input or output for sensor communication.
Implement timing functions (pulseIn, delayMicroseconds) to measure signal duration and
calculate distance.
Use Serial.print() to display real-time sensor data (distance) on the serial monitor.