HC-SR04 based Distance Finder gives the distance of an obstacle in centimeters. It has a range of 2cms to 400cms. The project is built around AT89S52 which is a microcontroller of the 8051 family. The distance is shown in seven segment screens.
Components:
- AT89S52
- Three of 7 sec. Disp. (Type of common anode)
- HC-SR04 (ultrasonic sensor module)
- Connectors, PCB, etc.
The code is written in the Assembly and is easily understandable.
The project can be used in obstacle avoidance robots, etc.
Working:
The operation of the ultrasonic sensors is quite simple and they are easy to connect with the microcontroller. The sensor module has 4 pins of which Pin-1 and Pin-4 are respectively + Vcc and Gnd respectively. Pin-2 is Trigger and Pin-3 is Eco pin. The operation of the sensors can be described from the following figure:
When a 10us High pulse is applied to the TRIG pin, the ultrasonic transmitter sends 8 consecutive 40kHz frequency pulses. When the eighth pulse is sent, the ECHO pin on the sensor becomes HIGH. Now, when the ultrasonic waves are reflected from any surface and are received by the Receiver, the ECHO pin becomes LOW. The time it takes to exit and return to the sensor is used to find the distance from the reflecting surface.
Distance in centimeters = (Time / 58) cms
In inches = (Time / 148)
The distance can also be calculated taking into account the speed of sound (= 340m / s).
Explanation of CODE:
• The data lines of the seven segment screens are interconnected in Port-0
• Port-1, Pin-0,1 and 2 are selection line for SSDs
• P3.0 is connected to Trig
• P3.1 is connected to the Echo pin of the sensor module
• In the main part of the program, the timer-1 is initialized in mode 2 (8-bit automatic recharge)
When P3.0 is set high, the DELAY1 subroutine is called. After 10us P3.0 is reset to 0
• P3.1 is now checked for a high signal. As P3.1 becomes high, timer 1 starts and every time it overflows, register A increments.
• The count value loaded must be 58 (mentioned in the data sheet). So after 58 cycles A increases once.
But since the delay caused by the instructions has to be compensated, the count value I used is 45 (=> 255D-210D).
• The value stored in A is used to extract the measured distance. This is done by the Division instruction.
• The 8-bit data value is sent sequentially to the respective 7-segment screen selected through the selection lines.