Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
Aim
The aim of this project is to measure the distance of an object using an Ultrasonic Distance Sensor (HC-SR04) interfaced with an AVR microcontroller (Atmega328) and communicate the distance measurements via UART.
Components Required
- Atmega 328
- Ultrasonic Distance Sensor - HC-SR04
- Fixed Voltage Sorce - 5V
- Potentiometer
- Oscilloscope
Working
- Ultrasonic Sensor
Ultrasonic sensors work based on the principle of sound waves. They emit ultrasonic pulses and measure the time it takes for the pulses to bounce back after hitting an object.The sensor's transmitter emits high-frequency sound waves (typically ultrasonic pulses in the range of 40 kHz).
The waves travel through the air until they encounter an object. The waves then reflect back to the sensor's receiver, and the sensor measures the time taken for the waves to return. By knowing the speed of sound in the medium (air), the sensor calculates the distance to the object using the time-of-flight method.
Code
#include
#define F_CPU 16000000
#include
#include
//ultrasonic sensor connections
#define TRIG_PIN PD3
#define ECHO_PIN PD2
void Uart_init()
{
UBRR0H =0;
UBRR0L=103; //baud rate for 16MHz clock
UCSR0B=(1<
UCSR0C=(1<
}
void Pulse_send()
{
DDRD |= (1<
PORTD |=(1<
_delay_ms(10);
PORTD &= ~(1<
DDRD &= ~(1<
}
void Uart_send(uint8_t data)
{
while(!(UCSR0A &(1<
UDR0=data; //send data type
}
uint16_t measureEchoPulse()
{
//measure echo pulse width in us
uint32_t pulse_width_us=0;
while(!(PIND &(1<
while (PIND &(1<
{
_delay_us(1);
pulse_width_us++;
}
//calculate distance in cm
uint16_t distance_cm=pulse_width_us/58;
return distance_cm;
}
int main(void)
{
Uart_init(); //initialize UART
while (1)
{
Pulse_send(); //trigger ultrasonic sensor to start measurement
uint16_t distance_cm=measureEchoPulse(); //measure echo pulse width and calculate distance
//send distance value in ASCII chars via UART
Uart_send('D');
Uart_send('i');
Uart_send('s');
Uart_send('t');
Uart_send('a');
Uart_send('n');
Uart_send('c');
Uart_send('e');
Uart_send(':');
char no_string[5];
itoa(distance_cm,no_string,10);
for (uint8_t i=0;no_string[i]!='0';i++)
{
Uart_send(no_string[i]);
}
Uart_send(' ');
Uart_send('c');
Uart_send('m');
Uart_send('\n');
_delay_ms(500); //wait for a short delay before starting next measurement
}
return 0;
}
Code Explanation
- The code initializes UART communication to send distance measurements.
- It triggers the ultrasonic sensor to initiate a distance measurement using a 10μs pulse.
- The code measures the echo pulse width using Input Capture Mode of Timer 1.
- It calculates the distance based on the pulse width and communicates the measurement via UART.
Registers Used in the Program
- 'UBRR0H' and 'UBRR0L': Baud rate registers for setting the communication speed.
- 'UCSR0B': Control register for enabling the UART transmitter ('TXEN0').
- 'UCSR0C': Control register for setting the frame format (8 data bits, no parity, 1 stop bit).
- 'DDRD': Data Direction Register for setting pins as input or output.
- 'PORTD': Port D register for setting output pin states (high/low).
Result
The project successfully measures the distance of an object using the HC-SR04 ultrasonic sensor and communicates the measured distance via UART.