Ultrasonic Sensor Distance Calculation - BuddyKit

May 24, 2025

🧠 Concept Overview

🌊 How Ultrasonic Distance Measurement Works

  1. The ultrasonic sensor sends a 10 µs pulse via the TRIG pin.
  2. The ECHO pin goes HIGH while the signal travels to the object and back.
  3. The duration (in microseconds) that ECHO stays HIGH is proportional to the distance.
  4. Speed of sound ≈ 343 m/s or 0.0343 cm/µs.
  5. Since the sound wave travels to the object and back, the formula is:
    Distance (cm) = (Time (µs) × 0.0343) / 2

⚙️ Hardware Context

This calculation is based on:

  • Buddy51 works Timer in Mode 1 (16-bit)
  • A standard 11.0592 MHz crystal

⏱️ Timer Tick Time Calculation

In Timer Mode 1, the timer increments every machine cycle. With an 11.0592 MHz crystal:

  • One machine cycle = 12 / 11.0592 MHz ≈ 1.085 µs
  • Each timer tick ≈ 1.085 µs

📏 Converting Timer Ticks to Distance

  1. Let t = timer ticks (1 tick ≈ 1.085 µs)
  2. Time in microseconds = t × 1.085
  3. Distance (cm) = (t × 1.085 × 0.0343) / 2
  4. That simplifies to distance = t × 0.0186

Since floating-point math is avoided in embedded C, we use integer math:

📌 Summary

  • Timer tick ≈ 1.085 µs
  • Speed of sound = 0.0343 cm/µs
  • Round-trip distance, so divide by 2
  • Final formula:

This method gives you accurate ultrasonic distance readings using only integer arithmetic, making it ideal for 8051 microcontrollers and SDCC projects.

Leave a reply
Class 6 – Simple Digital Door LockClass 7 – Simple RPM Counter

Leave Your Reply