Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Distance Finder Based on AT89S52 and Ultra-Sonic Sensors HC-SR04

By Deepesh Verma July 12, 2022

 

HC-SR04 based Distance Finder gives the distance from an obstacle in centimetres. It has a range of 2cms to 400cms. The project is build around AT89S52 which is a 8051 family microcontroller. The distance is displayed on Seven Segment Displays.

Components:  

     AT89S52

     Three 7-Seg. Disp. (Common Anode type)

     HC-SR04 (Ultrasonic sensor module)

     Connectors, PCBs etc.

     The code is written in Assembly and is easily understandable. The project can be used in obstacle avoidance robots, etc.

Working:

The working of the ultrasonic sensors is quite simple and they are easy to interface with the microcontroller. The sensor module has 4-pins out of which Pin-1 and Pin-4 are +Vcc and Gnd respectively.Pin-2 is Trigger and Pin-3 is Echo pin.The working of sensors can be described from the below figure :

When a High pulse of 10us is applied at TRIG pin, the ultrasonic transmitter sends 8 consecutive pulses of 40kHz frequency. As the Eighth pulse is sent the ECHO pin of the sensor becomes HIGH. Now when the ultrasonic waves reflect from any surface and are received by the Receiver,  the ECHO pin becomes LOW. The time it takes to leave and return to sensor is used to find the distance from the reflecting surface.

Distance in centimetres = (Time/58) cms

In Inches = (Time/148)                      

Distance can also be calculated by taking into account the speed of Sound (=340m/s)

HC-SRO4

Explanation of the CODE:

     ·   The Data lines of for Seven Segment Displays are interfaced at Port-0

     ·   Port-1’s, Pin-0,1 and 2 are select line for the SSDs

         ·   P3.0 is connected to Trig

         ·   P3.1 is connected to Echo pin of the sensor module

         ·   In the main part of Program, firstly Timer-1 is initialized in mode2 (8-bit auto reload)

             When P3.0 is set high, subroutine DELAY1 is called. After 10us P3.0 is reset to 0

         ·   Now P3.1 is checked for a high signal. As P3.1 becomes high, Timer 1 starts and every time it overflows Register A is incremented.

         ·  The Count Value loaded is to be 58 (mentioned on the datasheet). So that after 58 cycles A gets incremented once. But since the delay           caused due to instructions have to compensated, the count value I used is 45 (=> 255D-210D).

         ·   The value stored in A is used to extract the Distance measured. This is done by Division instruction.

         ·   The 8-bit data value is sequentially sent to respective 7-Segment Display being selected through the selection lines.

Distance Finder Based on AT89S52 and Ultra-Sonic Sensors HC-SR04

 

Distance Finder Based on AT89S52 and Ultra-Sonic Sensors HC-SR04

 

 

You may also like:


  • What are the top development boards for AI and ML?

  • How does LoRa modulation enable long-range communication?

  • What is the role of embedded software in electric vehicles?

  • What are the components of robotic arms and industrial robots?

  • What is the Modbus protocol and how does it work?

  • What drone parts you need to build a quadcopter?

Project Source Code

###


ORG 00H
MOV DPTR,#SSDisplay // moves the address of LUT to DPTR
MOV P1,#00000000B // sets P1 as output port
MOV P0,#00000000B // sets P0 as output port
CLR P3.0 // sets P3.0 as output for sending trigger
SETB P3.1 // sets P3.1 as input for receiving echo
MOV TMOD,#00100000B // sets timer1 as mode 2 auto reload timer
MAIN: MOV TL1,#210D // loads the initial value to start counting from
      MOV TH1,#210D // loads the reload value
      MOV A,#00000000B // clears accumulator
      SETB P3.0 // starts the trigger pulse
      ACALL DELAY1 // gives 10uS width for the trigger pulse
      CLR P3.0 // ends the trigger pulse
HERE: JNB P3.1,HERE // loops here until echo is received
BACK: SETB TR1 // starts the timer1
HERE1: JNB TF1,HERE1 // loops here until timer overflows (ie;48 count)
      CLR TR1 // stops the timer
      CLR TF1 // clears timer flag 1
      INC A // increments A for every timer1 overflow
      JB P3.1,BACK // jumps to BACK if echo is still available
      MOV R4,A // saves the value of A to R4
      ACALL DLOOP // calls the display loop
      SJMP MAIN // jumps to MAIN loop
 
DELAY1: MOV R6,#2D // 10uS delay
LOOP1: DJNZ R6,LOOP1
RET     
 
DLOOP: MOV R5,#50D // loads R5 with 100D
BACK1: MOV A,R4 // loads the value in R4 to A
       MOV B,#100D // loads B with 100D
       DIV AB   // isolates the first digit
       SETB P1.0 // activates LED display unit D1
       ACALL DISPLAY // calls DISPLAY subroutine
       MOV P0,A // moves digit drive pattern for 1st digit to P0
       ACALL DELAY   // 1mS delay
       ACALL DELAY
       MOV A,B // moves the remainder of 1st division to A
       MOV B,#10D // loads B with 10D
       DIV AB   // isolates the second digit
       CLR P1.0 // deactivates LED display unit D1
       SETB P1.1 // activates LED display unit D2
       ACALL DISPLAY
       MOV P0,A // moves digit drive pattern for 2nd digit to P0
       ACALL DELAY
       ACALL DELAY
       MOV A,B // moves the remainder of 2nd division to A
       CLR P1.1 // deactivates LED display unit D2
       SETB P1.2 // activates LED display unit D3
       ACALL DISPLAY
       MOV P0,A // moves the digit drive pattern for 3rd digit to P0
       ACALL DELAY
       ACALL DELAY
       CLR P1.2 // deactivates LED display unit D3
       DJNZ R5,BACK1 // repeats the display loop 100 times
       RET
 
DELAY: MOV R7,#250D   // 1mS delay
LABEL2: DJNZ R7,LABEL2
        RET
 
DISPLAY: MOVC A,@A+DPTR // gets the digit drive pattern for the content in A
         RET
 
SSDisplay: DB 0C0H // Hex code to display DISPLAY 0
DB 0F9H // DISPLAY 1
DB 0A2H // DISPLAY 2
3

Project Video


Filed Under: 8051 Microcontroller, Electronic Projects, Tech Articles
Tagged With: 8051, at89s52, distance finder, hc-sr04, ultasonic sensor
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Multiple DC/DC converters and a single input source
  • Voltage mode pushpull is a nonsense SMPS?
  • High Side current sensing
  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • MOSFET thermal noise in Weak vs Strong inversion

RSS Electro-Tech-Online.com Discussions

  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts
  • Wideband matching an electrically short bowtie antenna; 50 ohm, 434 MHz
  • Curved lines in PCB design
  • using a RTC in SF basic

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe