Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

EX-5

Ultrasonic sensor
INTROUCTION:

Arduino - Ultrasonic Sensor: The HC-SR04 ultrasonic sensor uses SONAR to

determine the distance of an object just like the bats do. It offers excellent
non contact range detection with high accuracy and stable readings in an easy-
touse package from 2 cm to 400 cm or 1” to 13 feet.

The operation is not affected by sunlight or black material, although acoustically,


soft materials like cloth can be difficult to detect. It comes complete with
ultrasonic transmitter and receiver module.
Technical Specifications

• Power Supply − +5V DC

• Quiescent Current − <2mA

• Working Current − 15mA

• Effectual Angle − <15°

• Ranging Distance − 2cm – 400 cm/1″ – 13ft

• Resolution − 0.3 cm

• Measuring Angle − 30 degree

Components Required

You will need the following components −

• 1 × Breadboard

• 1 × Arduino Uno R3

• 1 × ULTRASONIC Sensor (HC-SR04).


The projects

Proj 1: Design MCU system to find distance of an object and print the distance on

LCD with 3 seconds update period?

#include <LiquidCrystal.h>

int trigger=8;

int echo=9;

LiquidCrystal lcd(12,11,5,4,3,2);

float time,distance;

void setup()

{
lcd.begin(16,2);

pinMode(trigger,OUTPUT);

pinMode(echo,INPUT);

lcd.print(" Ultra sonic");

lcd.setCursor(0,1);

lcd.print("Distance Meter");

delay(2000);

lcd.clear();

lcd.print(" Circuit Digest");

delay(2000);

void loop()

lcd.clear();

digitalWrite(trigger,LOW);

delayMicroseconds(2);

digitalWrite(trigger,HIGH);

delayMicroseconds(10);

digitalWrite(trigger,LOW);

delayMicroseconds(2);

time=pulseIn(echo,HIGH);

distance=time*0.034/2;
lcd.clear();

lcd.print("Distance:");

lcd.print(distance);

lcd.print("cm");

lcd.setCursor(0,1);

lcd.print("Distance:");

lcd.print(distance/100);

lcd.print("m");

delay(1000);

Proj 2: Design MCU system to find distance of an object and print the distance on
LCD, when the distance is less than 20 cm, a green led is turn on and when the
distance become less than 12 cm, a red led should be turned on and when it is
less than 10 cm, a warning sound generated from buzzer is turned on.
include <LiquidCrystal.h>

LiquidCrystal lcd(11,10,9,8,7,6);

float d,t;

void setup()

lcd.begin(16,2);

pinMode(13,OUTPUT);

pinMode(12,INPUT);

pinMode(5,OUTPUT);

pinMode(4,OUTPUT);

pinMode(3,OUTPUT);

void loop()

digitalWrite(13, LOW);

delayMicroseconds(2);

digitalWrite(13, HIGH);

delayMicroseconds(10);

digitalWrite(13, LOW);

t = pulseIn(12, HIGH);

d = (t/2) / 29.1;

lcd.clear();
lcd.setCursor(0,0);

lcd.print("Distance: ");

lcd.print(d);

if(d<20 && d>12)

digitalWrite(3,HIGH);

digitalWrite(4,LOW);

digitalWrite(5,LOW);

else if(d<12 && d>10)

digitalWrite(3,LOW);

digitalWrite(4,HIGH);

digitalWrite(5,LOW);

else if(d<10)

digitalWrite(3,LOW);

digitalWrite(4,LOW);

digitalWrite(5,HIGH);

else
{

digitalWrite(3,LOW);

digitalWrite(4,LOW);

digitalWrite(5,LOW);

delay(100); }

____________________________________________________________

Code to Note:

The Ultrasonic sensor has four terminals - +5V, Trigger, Echo, and GND

connected as follows −

• Connect the +5V pin to +5v on your Arduino board.

• Connect Trigger to digital pin 7 on your Arduino board.

• Connect Echo to digital pin 6 on your Arduino board.

• Connect GND with GND on Arduino.

In our programs, we have displayed the distance measured by the sensor in

inches and cm via the serial port.

Result

You will see the distance measured by sensor in inches and cm on Arduino serial

monitor.
How Does it Work?

The ultrasonic sensor uses sonar to determine the distance to an object.

Here’s what happens:

1. The ultrasound transmitter (trig pin) emits a high-frequency sound (40

kHz).

2. The sound travels through the air. If it finds an object, it bounces back

to the module.

3. The ultrasound receiver (echo pin) receives the reflected sound

(echo).

You might also like