Skip to content

Circuit-Digest/Distance-Measurement-using-Ultrasonic-Sensor-and-Arduino

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Arduino Ultrasonic Sensor Distance Measurement

Arduino Distance Measurement using Ultrasonic Sensor

Project Overview

This Arduino Distance Measurement using Ultrasonic Sensor project demonstrates how to build a precise distance measurement system using the HC-SR04 ultrasonic sensor and Arduino. The system can accurately measure distances from 2cm to 400cm with ±3mm accuracy, making it perfect for various applications, including obstacle detection, liquid level monitoring, and robotics navigation.

The project uses the principle of ECHO - sound waves are transmitted and the time taken for the echo to return after hitting an obstacle is calculated to determine distance using the formula: Distance = (Time × Speed of Sound) / 2

Features

  • High Accuracy: Measures distances from 2cm to 400cm with ±3mm precision
  • Real-time Display: Shows distance measurements on a 16x2 LCD display
  • Dual Units: Displays measurements in both centimeters and meters
  • Cost-effective: Uses affordable and readily available components
  • Versatile Applications: Suitable for parking systems, liquid level monitoring, robotics, and more

Components Required

Component Quantity Description
Arduino Uno/Pro Mini 1 Main microcontroller board
HC-SR04 Ultrasonic Sensor 1 Distance measurement sensor
16x2 LCD Display 1 For displaying measurements
Breadboard 1 For circuit connections
9V Battery 1 Power supply
Connecting Wires As needed For circuit connections
Scale/Ruler 1 For calibration and testing

Circuit Connections

HC-SR04 to Arduino Connections

HC-SR04 Pin Arduino Pin Function
VCC 5V Power Supply
GND GND Ground Connection
Trig A4 (Pin 18) Trigger Signal Output
Echo A5 (Pin 19) Echo Signal Input

16x2 LCD Display Connections

LCD Pin Arduino Pin Description
VSS GND Ground
VDD 5V Power Supply
RS Pin 2 Register Select
EN Pin 3 Enable Pin
D4-D7 Pin 4-7 Data Pins (4-bit mode)

How It Works

  1. Trigger Phase: Arduino sends a 10μs high-level signal to the trigger pin
  2. Transmission: HC-SR04 automatically sends eight 40kHz ultrasonic pulses
  3. Echo Detection: Sensor detects the reflected sound waves
  4. Time Calculation: Arduino measures the time between transmission and reception
  5. Distance Calculation: Using the formula: Distance = (Time × 340m/s) / 2
  6. Display: Results are shown on LCD in both cm and meters

Code Implementation

#include <LiquidCrystal.h>
#define trigger 18
#define echo 19

LiquidCrystal lcd(2,3,4,5,6,7);
float time=0,distance=0;

void setup()
{
  lcd.begin(16,2);
  pinMode(trigger,OUTPUT);
  pinMode(echo,INPUT);
  lcd.print(" Ultra sonic");
  lcd.setCursor(0,1);
  lcd.print("Distance Meter");
  delay(2000);
  lcd.clear();
  lcd.print(" Circuit Digest");
  delay(2000);
}

void loop()
{
  lcd.clear();
  digitalWrite(trigger,LOW);
  delayMicroseconds(2);
  digitalWrite(trigger,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger,LOW);
  delayMicroseconds(2);
  
  time=pulseIn(echo,HIGH);
  distance=time*340/20000;
  
  lcd.clear();
  lcd.print("Distance:");
  lcd.print(distance);
  lcd.print("cm");
  lcd.setCursor(0,1);
  lcd.print("Distance:");
  lcd.print(distance/100);
  lcd.print("m");
  delay(1000);
}

Installation and Setup

  1. Hardware Assembly:

    • Connect the HC-SR04 sensor to Arduino as per the connection table
    • Wire the 16x2 LCD display in 4-bit mode
    • Ensure all power and ground connections are secure
  2. Software Setup:

    • Install Arduino IDE if not already installed
    • Copy the provided code into Arduino IDE
    • Select the correct board and port
    • Upload the code to Arduino
  3. Testing:

    • Power on the system
    • Place objects at various distances from the sensor
    • Verify readings against actual measurements

Applications

Industrial Applications

  • Automated Parking Systems: Vehicle detection and monitoring
  • Liquid Level Monitoring: Tank measurement in industrial plants
  • Conveyor Belt Control: Object detection and counting
  • Robot Navigation: Obstacle avoidance and mapping

Consumer Applications

  • Smart Home Automation: Automatic lighting and security systems
  • DIY Projects: Distance-based alarms and notifications
  • Educational Projects: Learning sensor interfacing and programming

Troubleshooting

Common Issues and Solutions

Problem Possible Cause Solution
Inconsistent readings Poor power supply Use stable 5V power source
No display on LCD Wrong connections Check LCD wiring and contrast
Inaccurate measurements Sensor misalignment Ensure sensor faces target perpendicularly
Temperature effects Environmental factors Implement temperature compensation

Tips for Better Accuracy

  • Ensure stable power supply (5V regulated)
  • Keep sensor clean and free from obstacles
  • Use flat, perpendicular surfaces for best results
  • Consider temperature compensation for precision applications
  • Avoid electromagnetic interference near the sensor

Technical Specifications

  • Operating Voltage: 5V DC
  • Operating Current: 15mA
  • Operating Frequency: 40kHz
  • Range: 2cm - 400cm
  • Accuracy: ±3mm
  • Measuring Angle: 15 degrees
  • Trigger Input: 10μs TTL pulse
  • Echo Output: TTL level signal, proportional to range

Enhancements and Modifications

Possible Upgrades

  • Add buzzer for audio alerts at specific distances
  • Implement data logging to SD card
  • Add wireless connectivity (WiFi/Bluetooth) for remote monitoring
  • Include multiple sensors for 360-degree detection
  • Integrate with mobile app for smartphone display

Code Modifications

  • Temperature compensation for higher accuracy
  • Moving average filter for stable readings
  • Calibration routine for custom applications
  • Multiple unit display options (inches, feet, etc.)

Safety Considerations

  • Ensure proper voltage levels (5V) to avoid component damage
  • Use appropriate gauge wires for connections
  • Avoid short circuits by double-checking connections
  • Keep the circuit away from moisture and extreme temperatures

References and Resources

Related Projects

Explore these related ultrasonic sensor projects:

License

This project is open-source and available for educational and commercial use. Please credit the source when using or modifying the code.

Support

For technical support and questions:

  • Visit the Circuit Digest Forum
  • Check Arduino community forums
  • Refer to component datasheets for detailed specifications

Project Difficulty: Beginner to Intermediate
Estimated Build Time: 2-3 hours
Cost: $15-25 USD (approximate)

Happy Building! 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages