Professional Documents
Culture Documents
Umar 2 Practical
Umar 2 Practical
Objective
To interface the HR-SO4 module with Arduino in order to measure the distance
Apparatus
HR-so4
LCD16x 2
Arduino UNO R3 board
LDR
bread Board
Jumper wires
Theory:
The ultrasonic sensor uses sonar to determine the distance to an object. This sensor reads from
2cm to 400cm (0.8inch to 157inch) with an accuracy of 0.3cm (0.1inches), which is good for
most hobbyist projects. In addition, this particular module comes with ultrasonic transmitter and
receiver modules. The ultrasound transmitter (trig pin) emits a high-frequency sound (40
kHz).The sound travels through the air. If it finds an object, it bounces back to the module. The
ultrasound receiver (echo pin) receives the reflected sound (echo).
Circuit Diagram:
Code
const int Trig =5;
const int Echo =6;
float Time;
int Distance;
#include<Wire.h>
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16,2);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(Trig,OUTPUT);
pinMode(Echo,INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("ultrasonicsensor");
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(Trig,LOW);
delayMicroseconds(2);
digitalWrite(Trig,HIGH);
delayMicroseconds(10);
digitalWrite(Trig,LOW);
Time=pulseIn(Echo,HIGH);
Figure 17: Ultrasonic Sensor connected with Arduino
//Serial.print(time);
Serial.println(Distance);
lcd.setCursor(2,1);
lcd.print("Distance=");
lcd.print(Distance);
delay(100);
}
Observation and Calculation:
Graph:
Calibration Graph
Reading
20 by Meter Rule
18
16
14
12
10
8
6
4
2
0
0 2 4 6 8 10 12 14 16 18 20
Reading by Sensor