Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
Project Overview: The project aims to measure the distance of an object using an ultrasonic distance sensor, specifically the HC-SR04 sensor. The distance is determined by measuring the time it takes for an ultrasonic wave to travel from the sensor to the object and back. The project will involve implementing code to accomplish this measurement.
Goals:
- Measure the distance of an object using the HC-SR04 ultrasonic distance sensor.
- Utilize the Input Capture Mode feature for precise timing measurements.
Key Highlights of the Project:
- Measurement of the distance traveled by the ultrasonic wave from the sensor to the object and back.
- Calculation of pulse width using the Input Capture Mode of Timer 1.
Deliverables of this Project:
- The distance traveled by the ultrasonic wave to the object, which provides an estimate of the distance to the object using the HC-SR04 sensor.
- Measurement of pulse width using the Input Capture Mode of Timer 1.
Code:
#include <avr/io.h>
#define F_CPU 16000000ul
#include <util/delay.h>
#include <avr/interrupt.h>
#include <string.h>
#include <stdlib.h>
#define lcd_data_dir DDRD
#define lcd_command_dir DDRC
#define lcd_data_port PORTD
#define lcd_command_port PORTC
#define EN PC2
#define RW PC3
#define RS PC4
void lcd_command(char);
void lcd_char(char);
void lcd_init(void);
void lcd_string(char*);
void lcd_string_xy (char,char,char*);
void lcd_command (char cmd)
{
lcd_data_port = cmd;
lcd_command_port&=~((1<<RS)|(1<<RW));
lcd_command_port|= (1<<EN);
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(2);
}
void lcd_char(char char_data)
{
lcd_data_port= char_data;
lcd_command_port&=~(1<<RW);
lcd_command_port|=((1<<EN)|(1<<RS));
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(1);
}
void lcd_init(void)
{
lcd_command_dir|= (1<<RS)|(1<<RW)|(1<<EN);
lcd_data_dir = 0xff;
_delay_ms(20);
lcd_command(0x38);
lcd_command(0x0C);
lcd_command(0x06);
lcd_command(0x01);
lcd_command(0x80);
}
void lcd_string(char *str)
int i;
for(i=0;str[i]!=0;i++)
{
lcd_char (str[i]);
}
}
void lcd_string_xy(char row,char pos,char *str)
{
if (row == 1)
lcd_command((pos & 0x0f)|0x80);
else if (row == 2)
lcd_command((pos & 0x0f)|(0xC0));
lcd_string(str);
}
#define trigger_pin PA0
int TimerOverFlow = 0;
ISR(TIMER1_OVF_vect)
{
TimerOverFlow++;
}
int main (void)
{
char string[10];
long count;
double distance;
DDRA = 0x01;
PORTB = 0xff;
lcd_init();
lcd_string_xy(1,0,"ultrasonic");
sei();
TIMSK = (1<< TOIE1);
TCCR1A = 0;
while(1)
{
PORTA |= (1<< trigger_pin);
_delay_ms(10);
PORTA &=(~(1<< trigger_pin));
TCNT1 = 0;
TCCR1B = 0x41;
TIFR = 1<<ICF1;
TIFR = 1<<TOV1;
while ((TIFR & (1<<ICF1)) == 0);
TCNT1 = 0;
TCCR1B = 0x01;
TIFR = 1<<ICF1;
TIFR = 1<<TOV1;
TimerOverFlow = 0;
while ((TIFR & (1<< ICF1))== 0);
count = ICR1 +(65535 * TimerOverFlow);
distance = (double)count/466.47;
dtostrf(distance,2,2,string);
strcat(string,"cm");
lcd_string_xy(2,0,"dist= ");
lcd_string_xy(2,7,string);
_delay_ms(200);
}
}
Expected Outcomes:
- Students will gain knowledge about the HC-SR04 ultrasonic sensor and its ability to measure distances.
- Understanding of the hardware connections between the Atmega328p microcontroller and the HC-SR04 sensor.
- Proficiency in using the input capture mode of Timer 1 to accurately measure pulse widths, enabling distance calculations based on ultrasonic wave travel time.
Result:
Thus the implementation of code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04) is done.