Introduction
Distance measurement is a crucial aspect of various applications, including robotics, automotive parking systems, and industrial automation. Ultrasonic distance meters offer a reliable and non-contact method to measure distance by using sound waves. In this article, we will discuss how to design and implement an ultrasonic distance meter using a PIC microcontroller.
System Overview
The ultrasonic distance meter system uses an ultrasonic sensor to measure the distance between the sensor and an object. The sensor sends out ultrasonic waves, which reflect off the object, and the time taken for the echo to return is used to calculate the distance. The PIC microcontroller processes this data and displays the distance on an LCD.
Key Components:
PIC Microcontroller (e.g., PIC16F877A)
Ultrasonic Sensor Module (e.g., HC-SR04)
LCD Display (16x2)
Buzzer (optional, for distance alerts)
Power Supply (5V)
Resistors, Capacitors, Diodes
Connecting Wires and Breadboard/PCB
Working Principle
The ultrasonic sensor (HC-SR04) operates by emitting an ultrasonic pulse at a frequency of 40kHz through its "Trig" pin. This pulse travels through the air, and when it hits an object, it reflects back to the sensor, where it is received by the "Echo" pin. The time interval between sending and receiving the pulse is measured by the microcontroller, and the distance is calculated using the speed of sound in air.
Distance Calculation Formula:
Distance=Time×Speed of Sound2\text{Distance} = \frac{\text{Time} \times \text{Speed of Sound}}{2}Distance=2Time×Speed of Sound Where the speed of sound in air is approximately 343 meters per second.
Components Required
PIC Microcontroller (e.g., PIC16F877A)
Ultrasonic Sensor Module (HC-SR04)
LCD Display (16x2)
Buzzer (optional)
Voltage Regulator (e.g., LM7805)
Resistors, Capacitors, Diodes
Power Supply (5V)
Connecting Wires and Breadboard/PCB
Circuit Diagram
Connections:
Ultrasonic Sensor (HC-SR04):
VCC: Connect to 5V power supply.
GND: Connect to ground.
Trig: Connect to a digital I/O pin of the PIC microcontroller (e.g., RD0).
Echo: Connect to another digital I/O pin of the PIC microcontroller (e.g., RD1).
LCD Display:
Connect the LCD in 4-bit mode (D4-D7 for data pins) and RS, EN pins to the appropriate GPIO pins of the PIC microcontroller.
Buzzer (optional):
Connect the buzzer to a digital output pin (e.g., RB0) for distance alerts.
Power Supply:
Provide a regulated 5V power supply to the PIC microcontroller, ultrasonic sensor, and other components.
Working Procedure
Triggering the Ultrasonic Sensor:
The PIC microcontroller sends a 10µs pulse to the Trig pin of the HC-SR04 sensor to start the ultrasonic burst.
Measuring Echo Time:
The sensor emits an ultrasonic wave and waits for the echo. The Echo pin goes high when the pulse is sent and returns low when the echo is received. The PIC microcontroller measures the duration for which the Echo pin stays high.
Calculating Distance:
The time measured by the microcontroller is used to calculate the distance of the object using the distance calculation formula mentioned earlier.
Displaying Distance:
The calculated distance is displayed on the LCD.
Distance Alert (Optional):
If a buzzer is connected, the system can be programmed to emit a sound if the distance falls below a certain threshold, providing a distance alert.
PIC Microcontroller Programming
Below is an example code to implement an ultrasonic distance meter using MPLAB X IDE and the XC8 compiler.
#include <xc.h>
#include <stdio.h>
#include "lcd.h"
#define _XTAL_FREQ 20000000 // 20 MHz oscillator frequency
#define TRIG RD0 // Trigger pin
#define ECHO RD1 // Echo pin
// Configuration bits
#pragma config FOSC = HS // High-speed oscillator
#pragma config WDTE = OFF // Watchdog Timer disabled
#pragma config PWRTE = OFF // Power-up Timer disabled
#pragma config BOREN = ON // Brown-out Reset enabled
#pragma config LVP = OFF // Low-voltage programming disabled
#pragma config CPD = OFF // Data EEPROM protection disabled
#pragma config WRT = OFF // Flash program memory write protection off
#pragma config CP = OFF // Code protection off
// Function prototypes
void initialize();
unsigned int measure_distance();
void display_distance(unsigned int distance);
void main() {
initialize();
while (1) {
unsigned int distance = measure_distance();
display_distance(distance);
__delay_ms(500); // Delay between measurements
}
}
void initialize() {
TRISD0 = 0; // Set RD0 as output (Trig)
TRISD1 = 1; // Set RD1 as input (Echo)
// Initialize LCD
lcd_init();
lcd_clear();
lcd_set_cursor(1, 1);
lcd_write_string("Distance:");
}
unsigned int measure_distance() {
unsigned int time;
unsigned int distance;
// Send 10us pulse to trigger
TRIG = 1;
__delay_us(10);
TRIG = 0;
// Wait for echo to go high
while (!ECHO);
// Measure the width of the echo pulse
TMR1 = 0; // Clear timer
T1CON = 0x10; // Start Timer1 with no prescaler
while (ECHO);
time = TMR1; // Read the time value
T1CON = 0x00; // Stop Timer1
// Calculate distance in cm
distance = (time * 0.0343) / 2;
return distance;
}
void display_distance(unsigned int distance) {
char buffer[16];
sprintf(buffer, "%u cm", distance);
lcd_set_cursor(1, 10);
lcd_write_string(buffer);
}
Explanation of the Code
Initialization:
The initialize() function configures the I/O pins and initializes the LCD for displaying the distance.
Measuring Distance:
The measure_distance() function sends a trigger pulse to the ultrasonic sensor, waits for the echo, measures the duration of the echo pulse, and calculates the distance based on the time taken by the pulse to return.
Displaying Distance:
The display_distance() function formats the measured distance and displays it on the LCD.
Conclusion
The ultrasonic distance meter using a PIC microcontroller is a simple yet effective project for measuring distances without physical contact. This project can be used in various applications, such as obstacle detection in robotics, parking assistance systems, and more.
Further Enhancements
Data Logging: Add an SD card module to log distance measurements over time for analysis.
Multiple Sensors: Expand the system to include multiple ultrasonic sensors for multi-directional distance measurement.
Wireless Communication: Integrate a wireless module like Bluetooth or Wi-Fi for remote distance monitoring.
Alert System: Use a buzzer or LED indicators to alert when an object is within a certain distance range.
Advanced Display: Use a graphical LCD or OLED display to provide more detailed information, such as distance graphs over time.
Want us to guide you through your project or make the project for you ?
Create Various Projects
Check out our Free Arduino Projects Playlist - Arduino Projects
Check out our Free Raspberry Pi Projects Playlist - Raspberry Pi Projects
Check out our Free TinkerCAD Projects Playlist - TinkerCAD Projects
Check out our Free IoT Projects Playlist - IoT Projects
Check out our Free Home Automation Projects Playlist - Home Automation Projects
Check out our Free NodeMCu Projects Playlist - NodeMCu Projects
Комментарии