Challenge

Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)

Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04):

Overview of the Project:

In this project, the implementation of code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04)

Goals:

  • Measuring the distance of an object using Ultrasonic Distance Sensor (HC-SR04)
  • Usage of Input Capture Mode

Key Highlights of the Project:

  • Measuring the distance travelled by the wave to the object and gets bounced back from the object to sensor.
  • Measuring Pulse width using Input Capture Mode

Deliverables of this project:

  • The distance travelled by a wave to the object for which we want to measure the distance using Ultrasonic Distance Sensor (HC-SR04).
  • Measuring Pulse width using Input Capture Mode of Timer 1

Expected Outcomes:

The students will get the Knowledge on Ultrasonic sensor (HC-SR04) to measure the distance travelled by wave to the object, the hardware connection of Atmega328p and HC-SR04 and how to use input capture mode of Timer1.

CODE:

#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
#include <string.h>
#include <stdlib.h>
#define lcd_cmd_dir DDRC //define lcd command port direction
#define lcd_data_dir DDRD //define lcd data port direction
#define lcd_cmd_port PORTC //define lcd command port
#define lcd_data_port PORTD //define lcd data port
#define EN PC2 //define enable signal pin
#define RW PC3 //define read/write signal pin
#define RS PC4 //define register select signal pin

void lcd_cmd (char); //lcd command write function
void lcd_data (char); //lcd data write function
void lcd_init (void); //lcd initialize function
void lcd_string (char*); //send string to lcd function
void lcd_string_xy (char, char, char*); //send row, position and string to lcd function

void lcd_cmd(char cmd) //lcd command write function
{
lcd_data_port = cmd; // write command data to lcd data port
lcd_cmd_port &=~ ((1<<RS)|(1<<RW)); //make RS LOW (command reg.), RW LOW(write)
lcd_cmd_port |= (1<<EN);
_delay_us(1); //delay
lcd_cmd_port &=~ (1<<EN);//high to low transition on EN (enable)
_delay_ms(3); //delay
}

void lcd_data(char data) //lcd data write function
{
lcd_data_port = data; // write data to lcd data port
lcd_cmd_port &=~ (1<<RW); //make RW low(write)
lcd_cmd_port |= ((1<<EN)| (1<<RS)); //make RS high (data reg.) and high to low transition on EN (enable)
_delay_ms(1); //delay
lcd_cmd_port &=~(1<<EN); //high to low transition on EN (enable)
_delay_ms(1); //delay
}

void lcd_init(void) //lcd initialize fuunction
{
lcd_cmd_dir |= (1<<RS)|(1<<RW)|(1<<EN); //make lcd command port direction as o/p
lcd_data_dir = 0xff; //make lcd data port direction as o/p
_delay_ms(20); //lcd power up time to get things ready, it should always >15ms
lcd_cmd(0x38); //initialize 16x2 lcd 8bit mode
lcd_cmd(0x0C); //display on cursor off command
lcd_cmd(0x06); //auto increment cursor
lcd_cmd(0x01); //clear lcd command
lcd_cmd(0x80); //8 is for first line and 0 is for 0th position
}

void lcd_string(char *str) //send string to lcd function
{
int i;
for(i=0;str[i]!=0;i++) //send each char string till the NULL
lcd_data(str[i]); //call lcd data write
}
}
void lcd_string_xy (char row, char pos, char *str) //send string to lcd function
{
if (row == 1)
lcd_cmd ((pos & 0x0f)|0x80); //command of first row and required position <16
else if (row==2)
lcd_cmd((pos & 0x0f)|0xC0); //command of second row and required position <16
lcd_string(str); //call lcd strind function
}
#define Trigger_pin PA0 //trigger pin
int Timeroverflow = 0;
ISR(TIMER1_OVF_vect)
{
Timeroverflow ++; //increment timeoverflow count
}
int main(void) //main function
{
char string[10];
long count;
double distance;
DDRA = 0x01; //make trigger p[in as output
PORTB = 0xff; //turn on pull-up
lcd_init(); //lcd initialization
lcd_string_xy(1,0,"Ultrasonic");
sei(); //enable global interrupt
TIMSK = (1<<TOIE1); //enable timer1 overflow interrupts
TCCR1A = 0; //set all bit to zero normal position
while(1)
{
//give 10us trigger pulse on trig. pin to HC-SR04
PORTA |=(1<<Trigger_pin);
_delay_us(10);
PORTA &=(~(1<<Trigger_pin));
TCNT1 = 0; //clear timer counter
TCCR1B = 0X41; //capture on rising edge, no prescaler
TIFR = 1<<ICF1; //clear ICP flag (input capture flag)
TIFR = 1<<TOV1; //clear timer overflow flag

//calculate width of echo by input capture (ICP)

while ((TIFR & (1<<ICF1))==0); //waiting for rising edge
TCNT1 = 0; //clear timer counter
TCCR1B = 0X01; //capture on falling edge, no prescaler
TIFR = 1<<ICF1; //clear ICP flag (input capture flag)
TIFR = 1<<TOV1; //clear timer overflow flag
Timeroverflow = 0; //clear timer overflow count
while ((TIFR & (1<<ICF1))==0); //wait for falling edge
count = ICR1 + (65535 * Timeroverflow); //take count

//8MHz timer frequency, sound speed=343 m/s
distance = (double) count/466.47;
dtostrf(distance,2,2,string); //distance to string
strcat(string, "cm"); //concat unit i.e. cm
lcd_string_xy(2,0,"Dist=");
lcd_string_xy(2,7,string); //print distance
_delay_ms(200);
}
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.