Challenge

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
THEORY:
Ultrasound is a high-pitched sound wave whose frequency exceeds the audible range of human hearing.
HC-SR04 sensor: Operating voltage: 5V
HC-SR04 ultrasonic distance sensor actually consists of two ultrasonic transducers.
First transducer converts electrical energy into sound energy of very high frequency (ultrasonic waves) this is transmitter.
Second transducer converts reflected sound energy into electrical energy, this is reciever.
The receiver produces the output electrial PWM waves whose duty cycle is proportional to the distance of reflection.
Sensor produces highly accurate output with deviation of 3 to 4mm from the actual position of object.
WORKING:
The above mentioned sensor have 4 pins, TRIG, EN, GND, VCC.
When the TRIG pin is set HIGH. In response, the sensor transmits an ultrasonic burst of eight pulses at 40 kHz.
These eight ultrasonic pulses travel through the air away from the transmitter.
After the transmissin the ECHO is pin is turned on to receive signal.
If those pulses are not reflected back, the echo signal times out and goes low after 38ms. Thus a pulse of 38ms indicates no obstruction within the range of the sensor.
If those pulses are reflected back, the echo pin goes low as soon as the signal is received. This generates a pulse on the echo pin whose width varies from 150 µs to 25 ms depending on the time taken to receive the signal.
MEASURING DISTANCE:
The PWM wavesare generated by receiver as output.
The duty cycle or width of signal is directly prportional to the distance travelled by the wave.
Now let’s calculate how far the object is from the sensor. For this we will use the below equation.
Distance = Speed x Time
PROGRAM:
/*
* P02.c
*
*
* Author : Shree
*/
#include <avr/io.h> /*IP, OP HEADER FILE INCLUDED*/
#define F_CPU 16000000L /*CPU FREQUENCY = 16MHz*/
#include <util/delay.h> /*DELAY FUNCTION INCLUDED*/
#define RS_high PORTD |= (1<<0); /*PORTD-> PIN0-> RS_high*/
#define RS_low PORTD &= ~(1<<0); /*PORTD-> ~PIN0-> RS_low*/
#define EN_high PORTD |= (1<<2); /*PORTD-> PIN2-> EN_high*/
#define EN_low PORTD &=~(1<<2); /*PORTD-> ~PIN2-> EN_low*/
#define RW_high PORTD |= (1<<1); /*PIN1-> RW_high*/
#define RW_low PORTD &= ~(1<<1); /*~PIN1-> RW_low*/
#define US_ERROR -1 /*ULTRASONIC SENSOR: WORKING OR NOT*/
#define US_NO_OBSTACLE -2 /*NO OBSTACLE*/
#define US_PORT PORTC /*ULTRASONIC SENSOR-> PORTC-> INPUT*/
#define US_PIN PINC /*INPUT PORTC-> PINC*/
#define US_DDR DDRC /*DDRD-> INPUT/OUTPUT*/
#define US_TRIG_POS PC0 /*TRIG-> PORTC PIN0*/
#define US_ECHO_POS PC1 /*ECHO-> PORTC PIN1*/
int distance, previous_distance; /*GLOBAL VARIABLES DECLARED*/
void lcd_cmd(unsigned char cmd) /*COMMAND FUNC*/
{
PORTB= cmd;
rs_low;
RW_high;
en_high;
_delay_ms(1);
en_low;
}
void lcd_data(unsigned char data) /*DATA FUNC*/
{
PORTB= data;
RW_low;
RS_high;
en_high;
_delay_ms(1);
en_low;
}
void lcd_init() /*LCD INIT*/
{
RW_high;
RS_high;
EN_high;
PORTB = 0xff; /*LCD PORTB-> OUTPUT*/
lcd_cmd(0x38);
_delay_ms(5);
lcd_cmd(0x02);
_delay_ms(5);
lcd_cmd(0x01);
_delay_ms(5);
lcd_cmd(0x0c);
_delay_ms(5);
lcd_cmd(0x06);
_delay_ms(5);
lcd_cmd(0x80);
_delay_ms(5);
}
void cursorpos(char x,char y) /*FUNC TO DECIDE CURSOR POSI*/
{
if((x<1 || x>2)||(y<1 || y>16)) /*CHECK DISPLAY*/
{
x=1; /*ROW*/
y=1; /*COLUMN*/
}
if (x==1)
{
lcd_cmd(0x7F+y);
}
else
{
lcd_cmd(0xBF+y);
}
}
void lcd_out(char a, char b,char *str) /*DISPLAY-> OUTPUT ON LCD WITH CURSOR POSI*/
{
cursorpos(a,b);
while(*str)
{
lcd_data(*str);
str++;
}
}
void lcd_clear()
{
lcd_cmd (0x01); /*CLEAR LCD SCREEN*/
_delay_ms(2); /*DELAY*/
lcd_cmd (0x80); /*CURSOR POSI: ROW1, COLUMN1*/
}
void HCSR04Init()
{
/*TRIG PIN-> OUTPUT*/
US_DDR|=(1<<US_TRIG_POS);
}
void HCSR04Trigger()
{ /*US WAVE OF 15 MICROSEC*/
/*TRIG-> HIGH 15MSEC*/
US_PORT |= (1<<US_TRIG_POS);
_delay_us(15); /*DELAY*/
US_PORT&=~(1<<US_TRIG_POS);
}
uint16_t GetPulseWidth()
{
/*MEASURE PULSE DURATION*/
/*THE MCU WILL TAKE SIGNAL AS INPUT*/
uint32_t i,result;
/*TO CHECK SENSOR WORKING OR NOT*/
for(i=0;i<600000;i++)
{
if(!(US_PIN & (1<<US_ECHO_POS)))
continue; /*LOW LEVEL*/
else
break; /*RISING EDGE*/
}
if(i==600000)
return US_ERROR; /*TIME OUT FOR WAVE*/
/*TIMER 1*/
TCCR1A=0X00;
TCCR1B=(1<<CS11); /*COUNTER RESOLUTION*/
TCNT1=0x00; /*COUNTER-> START*/
for(i=0; i<600000; i++) /*40 MSEC*/
{
if(US_PIN & (1 << US_ECHO_POS))
{
if(TCNT1 > 60000) break; else continue; /*NO OBSTACLE*/
}
else
break;
}
if(i == 600000)
return US_NO_OBSTACLE; /*NO OBSTACLE*/
/*EDGE-> FALLING*/
result = TCNT1; /*TCNT1-> VALUE OF NO OF PULSES*/
/*TIMER-> LOW*/
TCCR1B=0x00;
if(result > 60000)
return US_NO_OBSTACLE; /*NO ECHO SIGNAL*/
else
return (result>>1);
}
int main(void)
{
lcd_init(); /*INITIALIZATION-> LCD*/
char numberString[4]; /*STORE VALUE*/
while(1)
{
uint16_t r;
_delay_ms(100); /*DELAY*/
/*INITIALIZATION OF SENSOR*/
HCSR04Init();
while(1)
{
/*TRIG-> HIGH*/
HCSR04Trigger(); /*CALLING TRIG FUNC-> TO START SENSOR*/
/*MEASURE WIDTH OF WAVE*/
r=GetPulseWidth(); /*DURATION*/
if(r == US_ERROR) /*ERROR*/
{
cursorpos(1, 1); /*CURSOR POSI*/
char j[10] = "Error!";
lcd_out(1,1,&j);
}
else
{
distance=(r*0.034/2.0); /*DISTANCE IN CM*/
char f[15]= "Distance = ";
lcd_out(1,0,&f);
itoa(distance, numberString, 10); /*DISTANCE*/
lcd_out(2,4,numberString);
char h[6] = " cm";
lcd_out(2,10,&h);
previous_distance= distance;
_delay_ms(30);
}
}
}
}
CIRCUIT DIAGRAM:
OUTPUT:
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.