top of page
Writer's pictureSanskruti Ashtikar

Ultrasonic Radar using PIC Microcontroller

Updated: Nov 26

Introduction


Radar systems are widely used for detecting objects and measuring their distances. By leveraging ultrasonic sensors and a PIC microcontroller, you can create a simple yet effective ultrasonic radar system. This radar system can be used for various applications, including obstacle detection, security systems, and automation. This article provides a comprehensive guide on designing and implementing an Ultrasonic Radar using a PIC Microcontroller.





Components of the System


  1. PIC Microcontroller:

  2. Model: PIC16F877A or similar

  3. Function: Controls the ultrasonic sensor, processes data, and manages the display.

  4. Ultrasonic Sensor:

  5. Model: HC-SR04 or similar

  6. Function: Emits ultrasonic waves and measures the time taken for the echo to return, thereby calculating the distance to an object.

  7. Servo Motor:

  8. Model: SG90 or similar

  9. Function: Rotates the ultrasonic sensor to scan the area.

  10. LCD Display (Optional):

  11. Model: 16x2 or 20x4 LCD

  12. Function: Displays the distance readings and radar status.

  13. LEDs or Buzzer (Optional):

  14. Function: Provides visual or audio feedback when an object is detected within a specific range.

  15. Power Supply:

  16. Type: 5V or 12V DC adapter

  17. Purpose: Powers the microcontroller, sensor, and servo motor.

  18. Connecting Wires and PCB:

  19. Purpose: For making necessary connections between components.


Design Considerations


  1. Sensor Range and Accuracy:

  2. Ensure that the ultrasonic sensor has sufficient range and accuracy for your application. The HC-SR04, for instance, typically measures distances between 2cm and 400cm.

  3. Servo Motor Control:

  4. Choose a servo motor that can rotate smoothly and accurately, covering the desired scanning area (usually 180 degrees).

  5. Display and Interface:

  6. Decide whether you want to use an LCD display, LEDs, or a buzzer for output. An LCD provides detailed information, while LEDs and buzzers offer simple alerts.

  7. Power Management:

  8. Ensure the power supply can handle the current requirements of the servo motor, sensor, and microcontroller.

  9. Software Algorithm:

  10. Design an algorithm that controls the servo motor's rotation, triggers the ultrasonic sensor, and processes the distance data. The data can be visualized on an LCD or other display.


Building the System


1. Microcontroller and Sensor Integration


  • Connecting the Ultrasonic Sensor:

  • The HC-SR04 ultrasonic sensor has four pins: VCC, GND, Trigger, and Echo. Connect the VCC and GND to the microcontroller's power supply. Connect the Trigger pin to an output pin on the PIC, and the Echo pin to an input pin.

  • Connecting the Servo Motor:

  • Connect the servo motor's control wire to another output pin on the PIC microcontroller. The servo's VCC and GND should be connected to the power supply.

  • Programming the PIC Microcontroller:

  • Write firmware to control the ultrasonic sensor, process the echo time, and calculate the distance to an object. The microcontroller also controls the servo motor to rotate the sensor and scan the area.



#include <xc.h>
#define _XTAL_FREQ 4000000
// Define pin connections
#define TRIG_PIN PORTBbits.RB0
#define ECHO_PIN PORTBbits.RB1
#define SERVO_PIN PORTBbits.RB2
void init() {
    TRISB = 0x02;  // Set RB0 as output for TRIG, RB1 as input for ECHO, RB2 as output for SERVO
    // Setup Timer1 for distance calculation
    T1CON = 0x10;
}
void triggerUltrasonic() {
    TRIG_PIN = 1;
    __delay_us(10);
    TRIG_PIN = 0;
}
unsigned int readEchoTime() {
    while (!ECHO_PIN); // Wait for the echo start
    TMR1 = 0;          // Clear Timer1
    T1CONbits.TMR1ON = 1; // Start Timer
    while (ECHO_PIN);  // Wait for the echo end
    T1CONbits.TMR1ON = 0; // Stop Timer
    return TMR1;       // Return the time duration
}
float calculateDistance(unsigned int time) {
    // Convert time to distance (speed of sound in air is ~343 m/s)
    return (time * 0.0343) / 2.0; // Distance in cm
}
void rotateServo(unsigned int angle) {
    unsigned int pulseWidth = (angle * 11) + 500; // Convert angle to pulse width
    SERVO_PIN = 1;
    __delay_us(pulseWidth);
    SERVO_PIN = 0;
    __delay_ms(20 - (pulseWidth / 1000)); // Wait for the servo to move
}
void main() {
    init();
    unsigned int echoTime;
    float distance;


    while (1) {
        for (unsigned int angle = 0; angle <= 180; angle += 10) {
            rotateServo(angle); // Rotate servo to scan
            triggerUltrasonic(); // Send ultrasonic pulse
            echoTime = readEchoTime(); // Measure echo time
            distance = calculateDistance(echoTime); // Calculate distance
            // Display or process the distance data
            __delay_ms(200); // Short delay for stability
        }
    }
}

2. Servo Motor Control


  • Servo Rotation Logic:

  • The servo motor should rotate in increments (e.g., 10 degrees) from 0 to 180 degrees and back. This will allow the sensor to scan the area effectively.

  • Adjusting Pulse Width:

  • The pulse width for controlling the servo should be between 500μs (0 degrees) and 2500μs (180 degrees). This can be adjusted based on the servo motor’s specifications.


3. Display and Feedback


  • Connecting the LCD Display (Optional):

  • If using an LCD, connect it to the appropriate data and control pins on the microcontroller. Write functions to display distance readings and the corresponding angle.

  • Visual and Audio Feedback (Optional):

  • LEDs can be used to indicate the presence of objects within a certain range, while a buzzer can provide audio alerts.


void displayData(float distance, unsigned int angle) {
    // Code to display angle and distance on LCD or LED indicators
}
void giveFeedback(float distance) {
    if (distance < 30) { // Example threshold
        // Turn on LED or buzzer
    } else {
        // Turn off LED or buzzer
    }
}




4. Testing and Calibration


  • Sensor Calibration:

  • Test the ultrasonic sensor by measuring known distances and adjusting the calculation formula if necessary. Ensure the sensor is correctly detecting objects at various distances.

  • Servo Calibration:

  • Verify that the servo motor accurately moves to the specified angles and covers the full 180-degree range.

  • System Testing:

  • Run the system and observe the radar’s operation. Ensure the distance data is accurate and that the system responds appropriately to detected objects.


Testing and Optimization


  1. Functional Testing:

  2. Test the system with various objects and distances to ensure accurate detection. Verify that the servo motor’s rotation is smooth and that the ultrasonic sensor provides consistent readings.

  3. Performance Optimization:

  4. Optimize the scanning speed and accuracy. You can adjust the servo motor's rotation speed or the delay between sensor readings to improve performance.

  5. Environmental Testing:

  6. Test the system in different environments, such as indoors and outdoors, to ensure it works well under varying conditions, including different lighting and temperature levels.

  7. User Interface Optimization:

  8. If using an LCD, make sure the information displayed is clear and easy to understand. Adjust the layout or format as needed based on user feedback.


Conclusion


An Ultrasonic Radar using a PIC Microcontroller is an excellent project for learning about sensor integration, motor control, and real-time data processing. By combining an ultrasonic sensor with a servo motor and microcontroller, you can create a system that scans an area, detects objects, and provides distance measurements.

With careful design, testing, and optimization, this radar system can be used for a variety of applications, including security, automation, and obstacle detection. The project also offers opportunities for expansion, such as adding wireless communication or data logging features.



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 



38 views0 comments

Related Posts

See All

Comments


bottom of page