Introduction
Ultrasonic HC-SR04 Module
The HC-SR04 ultrasonic module operates on the principles of SONAR and RADAR.
This module features an ultrasonic transmitter, receiver, and control circuitry all integrated onto a single board. It has four pins: Vcc, Gnd, Trig, and Echo. When a pulse of 10µs or longer is sent to the Trig pin, the module generates eight pulses at 40 kHz. Following this, the control circuit sets the Echo pin high.
The Echo pin remains high until it receives the echo signal of the transmitted pulses back. The duration for which the Echo pin stays high indicates the time taken for the generated ultrasonic sound to travel to the object and return. By using this time measurement along with the speed of sound in air, the distance to the object can be calculated using a simple formula relating distance to speed and time.
For more information about the HC-SR04 ultrasonic module and its applications, refer to the “Ultrasonic Module HC-SR04” section in the sensors and modules category.
To measure time, we can utilize the built-in timer of the 8051 microcontroller. For details on the timers and their operation within the 8051, refer to the “8051 Timers” topic in the 8051 Inside section.
Interfacing Diagram
HC-SR04 Ultrasonic Module Interfacing with 8051
Example
Here let’s design an application in which we will find a distance to an object by interfacing ultrasonic module HC-SR04 with 8051(here AT89S52 used) and display the distance on 16×2 LCD.
Steps of Programming
- 8051 microcontroller needs to transmit at least 10 us trigger pulse to the HC-SR04 Trig Pin.
- After getting a trigger pulse, HC-SR04 automatically sends eight 40 kHz sound waves and waits for rising edge output at the Echo pin.
- When the rising edge capture occurs at the Echo pin which is connected to the input of 8051, start Timer of 8051 and again wait for the falling edge on the Echo pin.
- As soon as the falling edge is captured at the Echo pin, the microcontroller reads the count of the Timer. This time count is used to calculate the distance to an object.
Calculation (distance in cm)
Distance= Sound Velocity*Time/2
Where:
Sound Velocity = 34,300 cm per second
For the AT89S52 (8051) microcontroller, with an oscillator frequency of 11.0592 MHz, the timer frequency is 921.6 kHz. This means that the time taken to execute one instruction is approximately 1.085 µs.
As a result, the timer increments after a time interval of 1.085 µs has elapsed.
=34300*Time Count*1.085*10^-6/2
Program
/*
Find distance of an Object by interfacing Ultrasonic HC-SR04 module with 8051(AT89S52)
http://www.electronicwings.com
*/
#include<reg52.h>
#include <stdio.h>
#include <LCD_8_bit.h>
#include <math.h>
#define sound_velocity 34300 /* sound velocity in cm per second */
#define period_in_us pow(10,-6)
#define Clock_period 1.085*period_in_us /* period for clock cycle of 8051*/
sbit Trigger_pin=P2^6; /* Trigger pin */
sbit Echo_pin=P2^7; /* Echo pin */
void Delay_us()
{
TL0=0xF5;
TH0=0xFF;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
void init_timer(){
TMOD=0x01; /*initialize Timer*/
TF0=0;
TR0 = 0;
}
void send_trigger_pulse(){
Trigger_pin= 1; /* pull trigger pin HIGH */
Delay_us(); /* provide 10uS Delay*/
Trigger_pin = 0; /* pull trigger pin LOW*/
}
void main()
{
float distance_measurement, value;
unsigned char distance_in_cm[10];
LCD_Init(); /* Initialize 16x2 LCD */
LCD_String_xy(1,1,"Distance:");
init_timer(); /* Initialize Timer*/
while(1)
{
send_trigger_pulse(); /* send trigger pulse of 10us */
while(!Echo_pin); /* Waiting for Echo */
TR0 = 1; /* Timer Starts */
while(Echo_pin && !TF0); /* Waiting for Echo goes LOW */
TR0 = 0; /* Stop the timer */
/* calculate distance using timer */
value = Clock_period * sound_velocity;
distance_measurement = (TL0|(TH0<<8)); /* read timer register for time count */
distance_measurement = (distance_measurement*value)/2.0; /* find distance(in cm) */
sprintf(distance_in_cm, "%.2f", distance_measurement);
LCD_String_xy(2,1,distance_in_cm); /* show distance on 16x2 LCD */
LCD_String(" cm ");
delay(100);
}
}