0% found this document useful (0 votes)
37 views

Lecture 13

The ultrasonic sensor HC-SR04 can measure distance by emitting ultrasound and measuring the reflection. It works by sending a pulse from the trigger pin and measuring the time it takes for the echo to return on the echo pin. The distance can then be calculated using the speed of sound and the pulse duration. The Arduino code uses the pulseIn() function to read the echo pin duration and calculates distance by multiplying the duration by 0.034/2. It prints the distance to the serial monitor and prints "Out of range" if the distance is less than 2cm or greater than 400cm.

Uploaded by

Azam Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Lecture 13

The ultrasonic sensor HC-SR04 can measure distance by emitting ultrasound and measuring the reflection. It works by sending a pulse from the trigger pin and measuring the time it takes for the echo to return on the echo pin. The distance can then be calculated using the speed of sound and the pulse duration. The Arduino code uses the pulseIn() function to read the echo pin duration and calculates distance by multiplying the duration by 0.034/2. It prints the distance to the serial monitor and prints "Out of range" if the distance is less than 2cm or greater than 400cm.

Uploaded by

Azam Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Sensors

Ultrasonic Sensor
• Ultrasonic Sensor HC-SR04 is a sensor that can measure distance. It emits
an ultrasound at 40000 Hz (40kHz) which travels through the air and if there is an object or
obstacle on its path It will bounce back to the module. Considering the travel time and the
speed of the sound you can calculate the distance.

• The configuration pin of HC-SR04 is VCC (1), TRIG (2), ECHO (3), and GND (4). The supply
voltage of VCC is +5V and you can attach TRIG and ECHO pin to any Digital I/O in your
Arduino Board.

OUTP
UT
input

2
Working of Sensor

3
Pulse working of Sensor

4
Calculating Distance through Sensors
If the object is 20 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs
the sound wave will need to travel about 588 microseconds. But what you will get from the Echo pin
will be double that number because the sound wave needs to travel forward and bounce backward.
So in order to get the distance in cm we need to multiply the received travel time value from the echo
pin by 0.034 and divide it by 2.

Arduino Sentax
distance = (duration / 2) * 0.0343;
5
pulseIn()

6
Code
const int trigPin = 9;
const int echoPin = 10;

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.println(distance);
}

7
Question

• Make code for distance measure, if distance is greater than equal to


400 cm or less than equal to 2 cm print “Out of range”

8
4

You might also like