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

from the above we have to measure the distance of an object using ultra sonic sensor and we have to display it in the lcd

here is the code for the above question in the first i included the avr , intrupt ,delay libraries to access the predefined functions and after i included the 8000000 mhz because the micro control have the capacity of frequency and i declared #include <stdio.h>,#include<stdlib.h> because im using the sprintf statements after im using #define rs_high means im enabling the rs pin in the lcd and i assigned the portD 0th pin to the rs and after making it low and after im using the #define en_high means im giving the connection to the enable pin in the lcd from portd 1st pin and after im assigning the port d to the lc by using lcd_port

later im initializing the all functions like lcd_init ,void lcd_cmd ,void lcd_data ,void set_cursor ,void lcd_print ,void lcd_clean ,after im assiging the extern voltaile to the variables a,b,c these can hold the input cap[ture values later im again initializing the functions void capture_init (void), void send_trigger(void )

in the lcd_init () im just passing some commands for the lcd operations l;ike cearing screeen set the cursor like that

in the LCD command function first im giving the RS as iow because low means the command mode and after I SET enable pin as LOW after by using the masking technique im just sending the 4 bits of commands by making the enable high with the delay of 1 second

in the LCD CURSOR POS function im passing the arguments in the prototype in the functionn gave some condtion like if x==1 it should pass the commands fro the 1st row and which position it would be same for the y==1 for the second row which position it would be

same for the LCD DATA function first im giving rs as high because it means were sending the data after i SET the enable as low by using the masking techniques im just sendiing the 4 bits of data by making the enable pin high with some delay of 1 second

in the LCD PRINT_DATA function im just passing some arguments in the prototype after Tthat in the function im initializing the i variable after im taking the for loop in that i gave some condition like until the data is present it should print on lcd if the data is not present lcd doesnt show anything

in the lcd_clean ()function im using some commands for clearing screen with some delay after taht im initializing the a,b,c variables with 0 to avoid the garbage value

in the void capture _init (void) im using the TCCR1A = 0X00 means in normal mode not with any prescaler and after imusing the TIMSK1=(1<<ICIE1) means in TIMSKS register im enabling the ICIE1 means the input capture mode enabled now after im using the TCCR1B|= (1<<CS10) just set the prescaler after TCCR1B |= (1<<ICESC1) means im using rising edge capture mode after im initializing the icr to 0

in the void send _trigger im enabling the trigger of an ultra sonic sensor by the portc 0th pin after some delay im making it off

in the ISR(TIMER1_CAPT_VECT) im first capturing the value at rising edge after storing it in the a variable after im setting it to lower edge if it is at lower edge again im storing this value in the b variable after im taking some c varibale in that im storing the differnece between a and b values

in the MAIN function function first im calling the ;cd_init function after im enabling the intrupts by using the sei() after im maiking the port c all bits hig by usingh the DDRC = 0xff after im caling the capture init function it returns the capture value and after im just some lcd funntion like for the first row project 2 shpuld print after in the second row distance = should print later im taking the while loop in that im calling the send trigger function in that we get the ca value after some delay im doing some caluclations to get the distance after im using sprintf to conevert the digital values captured by the icr into charecters to dispaky in the lcd

here we can see the distance travelled in the lcd dispaly by varying potenti meter we can see the different distance values to measure the distance we use the ultrasonic sensor

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.