0% found this document useful (0 votes)
14 views40 pages

Ultrasonic

The document provides an overview of ultrasonic sensors, explaining how they work by emitting and receiving ultrasonic waves to calculate distance. It includes details on the HC-SR04 sensor, its pinout, and programming instructions for Arduino to measure distances and control motors based on sensor input. Additionally, it outlines a project for creating an obstacle-avoiding robot using ultrasonic sensors and conditional statements.

Uploaded by

anm130204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views40 pages

Ultrasonic

The document provides an overview of ultrasonic sensors, explaining how they work by emitting and receiving ultrasonic waves to calculate distance. It includes details on the HC-SR04 sensor, its pinout, and programming instructions for Arduino to measure distances and control motors based on sensor input. Additionally, it outlines a project for creating an obstacle-avoiding robot using ultrasonic sensors and conditional statements.

Uploaded by

anm130204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

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 receiver.which
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 Serial.begin(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

Serial.print("Distance: "); // Prints the distance


Serial.println(distance);
}
IF Conditions
&
Logical
Operators
Conditional Statements

Syntax Example

Syntax: Example:

if(condition){ if( state == LOW ){


digitalWrite(led,LOW
);
Statements…
Serial.println(“OFF”);
}
}
Conditional Statements

Syntax Example

if( condition ){ if( state == LOW ){


statements... Serial.println(“Offline”);
} }
else{ else{
statements.... Serial.println(“Online”);
} }
Conditional Statements

Syntax Example
if( condition ){ if( a == 1 ){
statements... Serial.println(“One”);
} }
else else if( a == 2 ){
if( condition ){ Serial.println(“Two”);
statements... }
} else{
:::::::::: Serial.println(“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);
Serial.begin(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;

Serial.print(distance1);
Serial.print("\t");
Serial.print(distance2);
Serial.print("\t");
if(distance1 > 100 && distance2 >100){
Serial.println("Forward");
digitalWrite(leftMotorP,HIGH);
digitalWrite(leftMotorN,LOW);

digitalWrite(rightMotorP,HIGH) ;digitalWrite(rig
htMotorN,LOW);
}
else if(distance1 >=100 && distance2
<100){

Serial.println("Left");
digitalWrite(leftMotorP,LOW);
digitalWrite(leftMotorN,LOW);

digitalWrite(rightMotorP,HIGH) ;digitalWrite
(rightMotorN,LOW);
}
else if(distance1 <100 && distance2
>=100){
Serial.println("Right");
digitalWrite(leftMotorP,HIGH);
digitalWrite(leftMotorN,LOW);

digitalWrite(rightMotorP,LOW) ;digitalWrit
e(rightMotorN,LOW);

}
else{
Serial.println("Stop");
digitalWrite(leftMotorP,LOW);
digitalWrite(leftMotorN,LOW);

digitalWrite(rightMotorP,LOW) ;digital
Write(rightMotorN,LOW);
}
delay(500);

}
Circuit Diagram
Thank you

You might also like