PIC16F877A Ultrasonic Sensor Interface: A Step-by-Step Guide

by Marwen Maghrebi

In this article, we explore how to interface an ultrasonic sensor with the PIC16F877A microcontroller for precise distance measurement.

Features of PIC16F877A Ultrasonic Sensor Interface project"

Things used in this project

Software apps and online services:

1- MPLAB

2- Proteus 8

Interfacing HC-SR04 Ultrasonic Sensor with PIC16F877A: Overview and Project Guide

In this project, we will explore how to interface an ultrasonic sensor with the PIC16F877A microcontroller to achieve accurate distance measurement, perfect for embedded systems requiring reliable proximity sensing.

 HC-SR04 Ultrasonic Sensor Overview

Ultrasonic ranging module HC-SR04 offers non-contact distance measurement from 2 cm to 400 cm with an accuracy of 3 mm. Featuring ultrasonic transmitters, receivers, and a control circuit, it calculates distances based on the time interval between the transmitted ultrasonic waves and their echoes. Operating at 5V DC, it is highly efficient, compact, and suitable for applications requiring precise and reliable distance sensing.
If you’d like to learn more about this sensor and explore additional projects using other microcontrollers, check out the article STM32 Ultrasonic Sensor.

Overview of HC-SR04 Ultrasonic Sensor

Project : Interfacing HC-SR04 Ultrasonic Sensor with PIC Microcontroller

This project demonstrates how to interface the HCSR04 ultrasonic sensor with the PIC16F877A microcontroller to measure distance. The setup uses UART communication to transmit the measured distance to a connected terminal. With its accurate ranging capability, the HC-SR04 is perfect for proximity sensing in embedded systems.

Configuration and Definitions

This section includes the configuration bits, pin definitions, constants, and function prototypes to set up the PIC16F877A microcontroller and ultrasonic sensor.

  1. #include <xc.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4.  
  5. // Configuration bits
  6. #pragma config FOSC = HS // High-Speed Crystal
  7. #pragma config WDTE = OFF // Watchdog Timer disabled
  8. #pragma config PWRTE = ON // Power-up Timer enabled
  9. #pragma config BOREN = ON // Brown-out Reset enabled
  10. #pragma config LVP = OFF // Low Voltage Programming disabled
  11. #pragma config CPD = OFF // Data EEPROM Memory Code Protection disabled
  12. #pragma config WRT = OFF // Flash Program Memory Write disabled
  13. #pragma config CP = OFF // Flash Program Memory Code Protection disabled
  14.  
  15. #define _XTAL_FREQ 20000000 // 20 MHz oscillator frequency
  16.  
  17. // Pin Definitions
  18. #define TRIG_PIN RB0 // Trigger pin connected to RB0
  19. #define ECHO_PIN RB1 // Echo pin connected to RB1
  20. #define TRIG_PIN_DIR TRISB0 // Direction register for TRIG_PIN
  21. #define ECHO_PIN_DIR TRISB1 // Direction register for ECHO_PIN
  22.  
  23. // Constants
  24. #define US_ROUNDTRIP_CM 145 // Microseconds for sound to travel 1 cm (adjust for calibration)
  25.  
  26. // Function Prototypes
  27. void initialize_uart(void);
  28. void uart_send_string(const char* str);
  29. void uart_send_byte(uint8_t data);
  30. void initialize_pins(void);
  31. uint16_t measure_distance(void);
  32.  
  33. // UART Buffer
  34. char uart_buffer[32];

UART Initialization and Communication

This section handles UART initialization and functions for sending data over UART.

  1. void initialize_uart(void) {
  2. TRISC6 = 0; // TX pin as output
  3. TRISC7 = 1; // RX pin as input
  4. SPBRG = 129; // Baud rate set for 9600 with 20MHz crystal
  5. BRGH = 1; // High-speed baud rate
  6. SYNC = 0; // Asynchronous mode
  7. SPEN = 1; // Enable serial port
  8. TXEN = 1; // Enable transmission
  9. CREN = 1; // Enable reception
  10. }
  11.  
  12. void uart_send_byte(uint8_t data) {
  13. while (!TXIF); // Wait for transmit buffer to be empty
  14. TXREG = data; // Send the byte
  15. }
  16.  
  17. void uart_send_string(const char* str) {
  18. while (*str) {
  19. uart_send_byte(*str++);
  20. }
  21. }

Ultrasonic Sensor Initialization and Main Logic

This section includes functions to initialize pins, measure distance, and the main logic for continuous distance measurement.

  1. void initialize_pins(void) {
  2. TRIG_PIN_DIR = 0; // Set TRIG_PIN as output
  3. ECHO_PIN_DIR = 1; // Set ECHO_PIN as input
  4. TRIG_PIN = 0; // Ensure trigger pin is low initially
  5. }
  6.  
  7. uint16_t measure_distance(void) {
  8. uint16_t pulse_duration = 0;
  9. // Generate 10s trigger pulse
  10. TRIG_PIN = 1;
  11. __delay_us(10);
  12. TRIG_PIN = 0;
  13. // Wait for echo pin to go high
  14. while (!ECHO_PIN); // Wait for rising edge
  15. // Start Timer1
  16. TMR1H = 0;
  17. TMR1L = 0;
  18. TMR1ON = 1; // Start Timer1
  19. // Wait for echo pin to go low
  20. while (ECHO_PIN); // Wait for falling edge
  21. // Stop Timer1
  22. TMR1ON = 0;
  23. // Calculate pulse duration
  24. pulse_duration = ((uint16_t)TMR1H << 8) | TMR1L;
  25. // Convert pulse duration to distance in cm
  26. return pulse_duration / US_ROUNDTRIP_CM;
  27. }
  28.  
  29. void main(void) {
  30. uint16_t distance;
  31. // Initialize peripherals
  32. initialize_pins();
  33. initialize_uart();
  34. // Timer1 configuration
  35. T1CON = 0x10; // Prescaler 1:2, internal clock (Fosc/4)
  36. while (1) {
  37. distance = measure_distance();
  38. // Send distance over UART
  39. sprintf(uart_buffer, "Distance: %ucm\r\n", distance);
  40. uart_send_string(uart_buffer);
  41. __delay_ms(500); // Delay before next measurement
  42. }
  43. }

Proteus Configuration :

  • Open Proteus & Create New Project and click next
  • Click on Pick Device
  • Search for PIC16F877A & SRF04
  • Click on Virtual Instruments Mode then choose VIRTUAL TERMINAL & OSCILLOSCOPE
  • Click on Terminal Mode then choose (DEFAULT & POWER & GROUND)
  • finally make the circuit below and start the simulation
Circuit design for PIC16F877A Ultrasonic Sensor Interface in Proteus simulation

That’s all!

If you have any questions or suggestions don’t hesitate to leave a comment below

You Might Also Like

Leave a Comment


Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
    -
    00:00
    00:00