HCSR04-Ultrasonic Sensor | Code | Circuit | Pin configuration

ULTRASONIC SENSOR: HC-SR04

It is a 4 pin Ultrasonic device. Ultrasonic sensors are sound sensors they can operate at the frequency of above human hearing,  It is used to measure the distance by using Ultrasonic waves. This module includes an ultrasonic transmitter and receiver unit.  By using the radar-based echo signal technology it can measure the distance. It transmits the ultrasonic waves in the air, the transmitted signal hits any objects the signal will reflect back (Echo signal), In this way it calculates the time between the trigger signal to reflected signal, this time period will convert into the distance.

Model Number: HCSRO4

Other names: Ultrasonic sensor, Obstacle detection sensor, Distance find sensor   

Other Related Device:  3-pin ultrasonic PING sensor.





Technical Specifications

Module

HCSR04

Operating voltage

5v

Working current

15ma

Working frequency

40 HZ

Maximum Range

400cm

Minimum Range

2cm

Trigger signal

10us Time period TTL Pulse

Echo pulse width

Proportional  to Time

Measuring angle

30 ͦ

Resolution

0.3cm

Dimensions

45mm x 20mm x 15mm


Pin configuration

  • Vcc: 5v (positive terminal)
  • Trig: Trigger output signal
  • Echo: Echo input signal 
  • Gnd: Ground (negative terminal)


what is the use of an ultrasonic sensor 

  • It is used to find the distance using ultrasonic waves, 
  • Obstacle detection for blin people
  • Automobile to control accidents.
  • Measure the water level
  • Flow measurement water pipes

How to calculate Distance?

The Timing diagram is shown below. You only need to trigger a 10µS pulse to start the ranging and then the module will automatically send out an 8 cycle burst of ultrasonic waves at 40 kHz and raise its echo. When the sensor detects any object the receiver sets the Echo pin to high for a period of time (width) which is proportional to the distance.


To calculate the distance measure the width Ton of the echo pin.

Time = width of the echo pulse in microseconds

Distance =us/58 centimeters

Distance =us/148 inch

Ultrasonic-Sensor-Timing-Diagram-TechnoElectronics44
Ultrasonic Sensor Timing Diagram


                ultrasonic sensor -Timing diagram






How to interface Ultrasonic sensor with Arduino

Hardware Requirement

Interface

Ultrasonic sensor

Arduino Uno

Vcc

Power supply(positive pin)

Trig

Data pin(2)

Echo

Data pin(3)

GND

Ground (Negative pin)


Ultrasonic-Sensor-Arduino-Interfacing-TechnoElectronics44
Ultrasonic-Sensor-Arduino-Interface



Circuit working

Ultrasonic sensor sends trigger signal using trig pin when the obstacle is detected sensor echo pin is getting high, so we need to calculate the time interval between the trigger signal and received echo signal. Using the mathematical operations we need to convert this time interval into the distance as shown below.



Arduino Code with Ultrasonic sensor



Code Explanation

Note:  Arduino compiler is case sensitive.

First we create a header file for serial communication, this serial communication is used for communication between Arduino controller and laptop.

Next, we create a variable name for trigger and Echo pins as shown below, trigger pin is connected to Arduino pin-2 and echo pin is connected to Arduino pin-3.

                          

and also we create some variable names to store  distance and calculation as shown below

in the setup() function we need to write the one-time execution instructions, such as to create a baud-rate 9600, declare the ultrasonic sensor pins as input or output, trigger pin acts as the output pin and echoes pin acts as the input pin.

Next, we initialize the loop()  function that is called as a infinite loop, once the program cursor entered into the loop it will never terminated.

In this infinite loop, we have to activate the Trigger pin with 10us (10-microseconds)  high pulse  to receive the echo signal.


after sent the trigger signal we have to wait for echo signal, echo pin is activated only when the obstacle is detected,

when the echo pin is activated we can measure the duration in microseconds using “pulseIn(echoPin, HIGH)”  function.

 

and we need to convert duration in to distance in centimeters as shown below

distance = (traveltime/2) x speed of sound

The speed of sound is: 343m/s = 0.0343 cm/uS = 1/29.1 cm/uS

Or

 in inches: 13503.9in/s = 0.0135in/uS = 1/74in/uS.

   is converted duration into distance in centimeters



DOWNLOAD CODE


NOTE:  Here we are not used any display unit to monitor the distance we are using a Serial-Port so check the output in Serial monitor.


Applications:

  • Obstacle detection and alert systems
  • Distance calculation
  • Automobiles
  • Used as a level  sensor

DATASHEET



Other Articles





                                        >>>HOME-TechnoElectronics44>>>

Post a Comment

3 Comments