Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
AIM:-
- The implementation of code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04).
DESCRIPTION:-
- Ultrasonic Module HC-SR04 works on the principle of SONAR and RADAR systems.
- HC-SR-04 module has an ultrasonic transmitter, receiver, and control circuit on a single board.
- The module has only 4 pins, Vcc, Gnd, Trig, and Echo.
- When a pulse of 10µsec or more is given to the Trig pin, 8 pulses of 40 kHz are generated. After this, the Echo pin is made high by the control circuit in the module.
- The echo pin remains high till it gets an echo signal of the transmitted pulses back.
- The time for which the echo pin remains high, i.e. the width of the Echo pin gives the time taken for generated ultrasonic sound to travel towards the object and return.
- Using this time and the speed of sound in air, we can find the distance of the object using a simple formula for distance using speed and time.
-
Steps of Programming
- ATmega32 microcontroller needs to transmit at least 10 us trigger pulse to the HC-SR04 Trig Pin.
- After getting a trigger pulse, HC-SR04 automatically sends eight 40 kHz sound waves and the microcontroller waits for rising edge output at the Echo pin.
- When the rising edge capture occurs at the Echo pin which is connected to an input of ATmega32, start Timer of ATmega16 and again wait for a falling edge on the Echo pin.
- As soon as the falling edge is captured at the Echo pin, the microcontroller reads the count of the Timer. This time count is used to calculate the distance to an object.
Here we are using the input capture mode Atmega32 on PD6(ICP1) pin as shown in the above interfacing diagram.
Calculation (distance in cm) (H1)
Sound velocity = 343.00 m/s = 34300 cm/s
The distance of Object (in cm) =
=
=
Now, here we have selected an internal 8 MHz oscillator frequency for ATmega16, with No-presale for timer frequency. Then time to execute 1 instruction is 0.125 us.
So, the timer gets incremented after 0.125 us time elapse.
= 17150 x (TIMER value) x 0.125 x 10^-6 cm
= 0.125 x (TIMER value)/58.30 cm
= (TIMER value) / 466.47 cm
CODE:-
#define F_CPU 8000000L
#define rs_high PORTA |=(1<<6)//enabling lcd register high
#define rs_low PORTA &=~(1<<6)//enabling lcd register low
#define en_high PORTA |=(1<<7)//enabling lcd enable high
#define en_low PORTA &=~(1<<7)//enabling lcd enable low
#include <avr/io.h> // This header file includes the appropriate Input/output definitions for the device
#include <util/delay.h> // to use delay function we need to include this library
#include <avr/interrupt.h>
#include <stdlib.h> // we'll be using itoa() function to convert integer to character array that resides in this library
#define Trigger_pin PB1
void lcd_cmd(unsigned char cmd)
{
PORTC = cmd;//giving cmd to the LCD
rs_low;//making register low
en_high;//making enable high
_delay_ms(1);
en_low;//making enable low
}
void lcd_data(unsigned char data)
{
PORTC = data;//giving data to the LCD
rs_high;//making register high
en_high;//making enable high
_delay_ms(1);
en_low;//making enable low
}
void lcd_init()
{
//Making lcd ready for displaying the 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)
{
if((x<1 || x>2)||(y<1 || y>16))//condtion check the 2 rows and 16 columns
{
x=1;
y=1;
}
if (x==1)
{
lcd_cmd(0x7F+y);//{80-1=7f} for starting the 1st line hex decimal value(80) y we r using means we need to dtart from that column
}
else
{
lcd_cmd(0xBF+y);//0c-1=0b for starting 2nd line,2nd row
}
}
void lcd_out(char a, char b,char *str)
{
cursorpos(a,b);//checking the cursor position or setting the position
while(*str)
{
lcd_data(*str);//sending the data to lcd function with string as argument
str++;
}
}
int Timeroverflow=0;
ISR(TIMER1_OVF_vect)
{
Timeroverflow++;/* Increment Timer Overflow count */
}
int main()
{
char dist[10];
long count;
double dis;
DDRB |=0b00000010; /* Make trigger pin as output */
DDRD&=~(1<<6);
PORTD |=(0b010000000);
DDRC |=0xFF;/* Make trigger pin as output */
DDRA |=0xFF;/* Make trigger pin as output */
lcd_init();
lcd_out(1,1,"Ultrasonic");
sei();/* Enable global interrupt */
TIMSK=(1<<TOIE1);/* Enable Timer1 overflow interrupts */
TCCR1A=0;/* Set all bit to zero Normal operation */
while(1)
{
/* Give 10us trigger pulse on trig. pin to HC-SR04 */
PORTB |=(1<<Trigger_pin);
_delay_ms(10);
PORTB &=(~(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);/* Wait 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 freq, sound speed =343 m/s */
dis = (double)count / 466.47;
itoa(dis,dist,4);
lcd_out(2,1,dist);
lcd_out(2,5,"CM");
_delay_ms(10);
}
}
OUTPUT:-
