Comprehensive distance measurement system demonstrating ultrasonic sensing, real-time signal processing, hardwareβsoftware integration, and multi-platform interfacing using Arduino UNO.
β Perfect for academics, portfolio demonstrations, and embedded systems fundamentals
- π Project Overview
- β¨ Key Features
- π¬ Demo
- π¦ Hardware Requirements
- π Circuit Connections
- π§ Working Principle
- π» Software Setup
- π Quick Start Guide
- π§ Customization
- π Learning Outcomes
- π Use Cases
- π Troubleshooting
- π Technical Specifications
- π€ Contributing
- π Connect
|
Real-Time Sensing 10 Hz sampling rate with HC-SR04 |
Multi-Platform Arduino + Python integration |
Production Ready Clean, modular, documented |
This project implements a comprehensive distance measurement system featuring:
- π― Real-time distance calculation with visual LED feedback
- π₯οΈ Dual Python interface support (GUI + Audio Alert System)
- π Threshold-based object detection and triggering
- π‘ Serial communication protocol for cross-platform integration
Designed to be modular, deterministic, and interview-ready β optimized for academic excellence and portfolio demonstrations.
| Feature | Description |
|---|---|
| β‘ Hardware-Software Integration | Seamless Arduino-Python communication via UART |
| π¨ Multiple Output Modes | Compatible with GUI and audio alert systems simultaneously |
| π Real-time Processing | 10 Hz sampling rate with precise distance calculation |
| π‘ Visual Feedback | LED indicator for proximity alerts (10-40 cm range) |
| π§© Modular Design | Easy to extend and customize for specific applications |
| π Professional Code | Clean, well-documented, interview-ready implementation |
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β HC-SR04 β GPIO β Arduino UNO β UART β Python Host β
β Ultrasonic β ββββββ> β Microcontroller β ββββββ> β Application β
β Sensor β β β β (GUI/Audio) β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
βΌ
ββββββββββββββββ
β LED Visual β
β Feedback β
ββββββββββββββββ
Serial Monitor (9600 baud):
25.3
23.7
21.4
PLAY_AUDIO β Trigger sent when object in range
20.1
18.9
LED Behavior:
- π’ OFF: Distance > 40 cm or < 10 cm
- π΄ ON: Distance between 10-40 cm (object detected)
| HC-SR04 β Arduino UNO | ||
|---|---|---|
| HC-SR04 Pin | Arduino Pin | Description |
| VCC | 5V | Power supply (+) |
| GND | GND | Ground reference |
| TRIG | D9 | Trigger pulse input |
| ECHO | D10 | Echo pulse output |
| LED Circuit | |
|---|---|
| LED Terminal | Connection |
| Anode (+) [Long leg] | D3 (via 220Ξ© resistor) |
| Cathode (β) [Short leg] | GND |
βββββββββββββββββββββββββββββββ
β Arduino UNO β
β β
β 5V ββββββββββββββββββββββ>β VCC (HC-SR04)
β GND ββββββββββββββββββββββ>β GND (HC-SR04)
β D9 ββββββββββββββββββββββ>β TRIG
β D10 <βββββββββββββββββββββββ ECHO
β β
β D3 βββ[220Ξ©]βββ>β+LEDββββ>β GND
β β
β USB <βββββββββββββββββββββββ Computer
βββββββββββββββββββββββββββββββ
π Note: For visual circuit diagram, see circuit_diagram.png in repository
|
Step-by-Step Process:
|
Mathematical Foundation: Why divide by 2?
Measurement Range:
|
|
Arduino IDE Version 1.8.x or 2.x |
Language C/C++ (Arduino Core) |
Communication UART @ 9600 baud |
|
Python 3.8+ |
pyserial |
pygame |
Terminal |
Install Python dependencies:
pip install pyserial pygamegit clone https://github.com/aryan-r03/ultrasonic-distance-arduino.git
cd ultrasonic-distance-arduino- Open Arduino IDE
- Load
arduino_sketch_simple.ino - Connect Arduino UNO via USB
- Configure IDE:
- Tools β Board β Arduino UNO
- Tools β Port β Select correct port
- Windows:
COM3,COM4, etc. - macOS:
/dev/cu.usbmodem* - Linux:
/dev/ttyUSB0,/dev/ttyACM0
- Windows:
- Click Upload (β button)
- Wait for "Done uploading" message
Open Serial Monitor: Tools β Serial Monitor (or Ctrl+Shift+M)
Set to: 9600 baud
Expected Output:
25.3
23.7
21.4
PLAY_AUDIO β Trigger when object at 10-40 cm
20.1
18.9
Physical Testing:
- Move hand/object in front of sensor
- LED should illuminate when object is 10-40 cm away
- "PLAY_AUDIO" appears in serial when LED is ON
|
Option A: GUI Display python Distance_GUI.pyFeatures:
|
Option B: Audio Alert System python Distance_custom_audio.pyFeatures:
|
# macOS
SERIAL_PORT = "/dev/cu.usbmodem101"
# Linux
SERIAL_PORT = "/dev/ttyUSB0"
# Windows
SERIAL_PORT = "COM3"Edit in arduino_sketch_simple.ino:
#define MIN_DISTANCE 10 // Minimum trigger distance (cm)
#define MAX_DISTANCE 40 // Maximum trigger distance (cm)
// Example: Detect objects 20-60 cm away
#define MIN_DISTANCE 20
#define MAX_DISTANCE 60delay(100); // Current: 10 Hz (100ms interval)
// Faster sampling (20 Hz)
delay(50);
// Slower sampling (5 Hz)
delay(200);#define DEBOUNCE_DELAY 1500 // Milliseconds between triggers
// Reduce trigger frequency (3 seconds)
#define DEBOUNCE_DELAY 3000
// Increase trigger frequency (1 second)
#define DEBOUNCE_DELAY 1000// Current: LED ON when object in range
digitalWrite(LED_PIN, HIGH);
// Inverse: LED OFF when object in range
digitalWrite(LED_PIN, LOW);
// Blinking LED
digitalWrite(LED_PIN, millis() % 500 < 250 ? HIGH : LOW);
|
|
|
β No Serial Output
Solutions:
- β Check USB connection (use data-capable cable, not charge-only)
- β Verify correct COM port selected in Arduino IDE
- β Confirm baud rate is 9600 in Serial Monitor
- β Check Arduino board selection: Tools β Board β Arduino UNO
- β Try different USB port on computer
- β Restart Arduino IDE
π Incorrect Distance Readings
Solutions:
- β
Verify HC-SR04 wiring:
- VCC β 5V
- GND β GND
- TRIG β D9
- ECHO β D10
- β Ensure sensor has clear line of sight (no obstructions)
- β Check 5V power supply is stable (measure with multimeter: 4.8-5.2V)
- β Avoid angled surfaces (use flat, perpendicular objects)
- β Don't use sound-absorbing materials (foam, fabric)
- β Test at 20-50 cm distance first (optimal range)
π‘ LED Not Working
Solutions:
- β
Check LED polarity:
- Long leg (anode) β 220Ξ© resistor β D3
- Short leg (cathode) β GND
- β Verify 220Ξ© resistor is in series (not parallel)
- β Confirm LED is not burnt out (test with battery)
- β Check D3 pin connection
- β Test with multimeter: LED should drop ~2V when ON
π Python Script Connection Errors
Solutions:
- β Close Arduino Serial Monitor before running Python (port conflict)
- β
Update
SERIAL_PORTvariable with correct port:# Find your port: # Windows: Device Manager β Ports (COM & LPT) # macOS: ls /dev/cu.* # Linux: ls /dev/ttyUSB* /dev/ttyACM*
- β
Check port permissions (Linux/macOS):
sudo chmod 666 /dev/ttyUSB0 # or add user to dialout group: sudo usermod -a -G dialout $USER
- β
Install required libraries:
pip install pyserial pygame
- β
Verify Python version:
python --version(3.8+)
β οΈ Unstable/Fluctuating Readings
Solutions:
- β
Add smoothing/filtering in code:
// Moving average of last 5 readings float readings[5]; float average = 0; for(int i = 0; i < 5; i++) { readings[i] = distance; average += readings[i]; } average /= 5;
- β
Increase
DEBOUNCE_DELAYto reduce noise - β Shield sensor from electromagnetic interference
- β Use shielded cables for long wire runs
- β Add decoupling capacitor (0.1Β΅F) across VCC-GND
| Parameter | Value | Notes |
|---|---|---|
| Operating Voltage | 5V DC | Regulated from USB (4.8-5.2V) |
| Current Consumption | ~30mA | Arduino (15mA) + HC-SR04 (15mA) |
| Measurement Range | 2 cm - 400 cm | Effective: 10-300 cm |
| Accuracy | Β±3 mm | Under optimal conditions |
| Resolution | 1 cm | Limited by calculation |
| Measurement Angle | 15Β° cone | 30Β° total beam width |
| Ultrasonic Frequency | 40 kHz | 8 pulses per measurement |
| Sampling Rate | 10 Hz | 100ms measurement cycle |
| Serial Baud Rate | 9600 | UART, 8N1 configuration |
| Response Time | <100 ms | Total system latency |
| Parameter | Range |
|---|---|
| Operating Temperature | -10Β°C to 60Β°C |
| Storage Temperature | -20Β°C to 70Β°C |
| Humidity | 10% - 90% RH (non-condensing) |
ultrasonic-distance-arduino/
β
βββ π arduino_sketch_simple.ino # Main Arduino sketch (RECOMMENDED)
βββ π arduino_sketch_compatible.ino # Advanced mode-selectable version
β
βββ π Distance_GUI.py # Real-time GUI display interface
βββ π Distance_custom_audio.py # Audio alert system
β
βββ πΌοΈ circuit_diagram.png # Wiring diagram
βββ π README.md # This file
βββ π LICENSE # MIT License
β
βββ π audio_files/ # Audio alerts (user-provided)
βββ π audio_1.mp3
βββ π audio_2.mp3
βββ π audio_3.mp3
| Version | Date | Changes |
|---|---|---|
| v2.0 (Current) |
Jan 2025 |
β
Added dual Python interface support β Implemented mode-selectable operation β Enhanced serial protocol β Improved documentation β Added troubleshooting guide |
| v1.5 | Dec 2024 |
β
Added debounce mechanism β Improved stability β Added configuration options |
| v1.0 | Nov 2024 |
β
Basic distance measurement β LED indicator β Serial output |
Contributions are welcome! Help improve this project:
- π΄ Fork the repository
- πΏ Create a feature branch
git checkout -b feature/enhancement
- βοΈ Commit your changes
git commit -m 'Add amazing enhancement' - π€ Push to the branch
git push origin feature/enhancement
- π Open a Pull Request
- π Follow existing code style
- β Test thoroughly before submitting
- π Update documentation for new features
- π Include issue number in PR description
- π¬ Provide clear commit messages
This project is licensed under the MIT License
See LICENSE file for complete details
MIT License - Free to use, modify, and distribute
Attribution appreciated but not required
Special thanks to:
- π Arduino Community for comprehensive documentation
- π HC-SR04 Datasheet Authors for technical specifications
- π PySerial & Pygame Contributors for Python libraries
- π₯ Open Source Community for continuous inspiration
If this project helped you, please consider:
β Star this repository if you found it helpful!
π΄ Fork it to customize for your projects!
π’ Share it with others learning embedded systems!
π‘ "Hardware is hard. Software is soft. Integration is magic."
Made with Arduino by Aryan Ranjan
**Β© 2025 Aryan Ranjan | Open Source | IoT & Embedded Systems **