ULTRASONIC SENSOR
What is ultrasonic waves
⧫ They are normal sound waves which
humans cannot hear, but some animals can
⧫ They behave like normal sound wave and
reflects when it strike on any surface.
Ultrasonic Sensors
The production of
ultrasonic waves requires a
transmitter that transmits
wave pulses, which are then
received by a [Link]
itself computes the distance
to the target based on the
time-span between emitting High-frequency sound
the signal and receiving the pulses
echo.
Ultrasonic Sensors
● The path time of the echo pulse starting from
trigger to receiving of echo pulse is roughly 20ms.
● The sensor is controlled by digital signals which is
produced by micro controllers or processors.
HC-S04-
Pinout
Working of Ultrasonic
Sensor
Working of Ultrasonic
Sensor
● We need to transmit trigger pulse of at least 10
μs to the HC-SR04 Trig Pin.
● Then the HC-SR04 automatically sends eight 40
kHz sound wave and wait for rising edge output
at Echo pin.
● When the rising edge capture occurs at Echo
pin, start the Timer and wait for falling edge on
Echo pin.
● As soon as the falling edge is captured at the
Echo pin, read the count of the Timer.
How to calculate
distance
DISTANCE = SPEED * TIME
TOTAL DISTANCE = [343 m/s * time of high(Echo) pulse (in
µs) ]
2
= 171.5 * 102 cm s-1 * time* 10-6 s
= 0.01715 * time (in cms)
Speed of sound wave(air)-
343m/s
1s=1000000 µs
To display distance on a serial
monitor
Set trig pin as output and echo pin
as input
Set trig pin low for 2 microseconds
and Set trig pin high for 10
microseconds
Monitor pulse from echo pin
Calculate distance by
applying formulae
Print result on Serial
monitor
const int trigPin = 3; // define pin
numbers
const int echoPin = 2;
long duration; // defines
variables
int distance
void setup() {
pinMode(trigPin, OUTPUT); //
Sets the trigPin as O/P
pinMode(echoPin, INPUT);
// Sets the echoPin as I/P [Link](9600);
// Starts the serial communication
}
void loop() {
digitalWrite(trigPin, LOW); // Clears the trigPin
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Sets trigPin on HIGH state for 10 μ
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns
the sound wave travel time in
μs
distance= duration*0.034/2; // Calculating the distance
[Link]("Distance: "); // Prints the distance
[Link](distance);
}
IF Conditions
&
Logical
Operators
Conditional Statements
Syntax Example
Syntax: Example:
if(condition){ if( state == LOW ){
digitalWrite(led,LOW
);
Statements…
[Link](“OFF”);
}
}
Conditional Statements
Syntax Example
if( condition ){ if( state == LOW ){
statements... [Link](“Offline”);
} }
else{ else{
statements.... [Link](“Online”);
} }
Conditional Statements
Syntax Example
if( condition ){ if( a == 1 ){
statements... [Link](“One”);
} }
else else if( a == 2 ){
if( condition ){ [Link](“Two”);
statements... }
} else{
:::::::::: [Link](“Some
else{ number”);
statements... }
LOGICAL OPERATORS
OPERATOR DESCRIPTION
&& Logical AND
|| Logical OR
! Logical NOT
TRUTH TABLE : Logical AND
Condition1 &&
Condition2
Condition 1 Condition 2 Result
False False False
False True False
True False False
True True True
TRUTH TABLE : Logical OR
Condition1 || Condition2
Condition 1 Condition 2 Result
False False False
False True True
True False True
True True True
TRUTH TABLE : Logical NOT
! Condition1
Condition 1 Result
False True
True False
OBSTACLE AVOIDER-PROJECT
Things you
need
● Chassis with wheels and
motors
● Motor driver to control motors
● Arduino Board
● Ultrasonic sensors
● Jumper wires for connection
● Arduino code to control your bot
Algorithm
1. Set a threshold distance as dist
2. Read distance from the left ultrasonic sensor as
ldist
3. Read distance from the right ultrasonic sensor as
rdist
ldist >= dist rdist >= dist Forward
ldist < dist rdist >= dist Right
ldist >= dist rdist < dist Left
ldist < dist rdist < dist Stop
int trig1 = 6, trig2 = 3;
int echo1 = 5, echo2 = 2;
float duration;
float distance1, distance2;
const int leftMotorN=8,leftMotorP=7;
const int rightMotorN=4,rightMotorP=9;
void setup(){
pinMode(trig1,OUTPUT);
pinMode(echo1,INPUT);
pinMode(trig2,OUTPUT);
pinMode(echo2,INPUT);
pinMode(leftMotorN, OUTPUT);
pinMode(leftMotorP, OUTPUT);
pinMode(rightMotorN, OUTPUT);
pinMode(rightMotorP, OUTPUT);
[Link](9600);
}
void loop(){
digitalWrite(trig1,LOW);
delayMicroseconds(2);
digitalWrite(trig1,HIGH);
delayMicroseconds(10);
digitalWrite(trig1,LOW);
duration = pulseIn(echo1,HIGH);
distance1 = duration*0.017;
digitalWrite(trig2,LOW);
delayMicroseconds(2);
digitalWrite(trig2,HIGH);
delayMicroseconds(10);
digitalWrite(trig2,LOW);
duration = pulseIn(echo2,HIGH);
distance2 = duration*0.017;
[Link](distance1);
[Link]("\t");
[Link](distance2);
[Link]("\t");
if(distance1 > 100 && distance2 >100){
[Link]("Forward");
digitalWrite(leftMotorP,HIGH);
digitalWrite(leftMotorN,LOW);
digitalWrite(rightMotorP,HIGH) ;digitalWrite(rig
htMotorN,LOW);
}
else if(distance1 >=100 && distance2
<100){
[Link]("Left");
digitalWrite(leftMotorP,LOW);
digitalWrite(leftMotorN,LOW);
digitalWrite(rightMotorP,HIGH) ;digitalWrite
(rightMotorN,LOW);
}
else if(distance1 <100 && distance2
>=100){
[Link]("Right");
digitalWrite(leftMotorP,HIGH);
digitalWrite(leftMotorN,LOW);
digitalWrite(rightMotorP,LOW) ;digitalWrit
e(rightMotorN,LOW);
}
else{
[Link]("Stop");
digitalWrite(leftMotorP,LOW);
digitalWrite(leftMotorN,LOW);
digitalWrite(rightMotorP,LOW) ;digital
Write(rightMotorN,LOW);
}
delay(500);
}
Circuit Diagram
Thank you