Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
AIM
Measuring distance of an object using ultrasonic distance sensor (HC-SR04)
The HC-SR04 ultrasonic sensor includes a transmitter & a receiver. This sensor is used to find out the distance from the objective. Here the amount of time taken to transmit and receive the waves will decide the distance between the sensor and an object. This sensor uses sound waves by using non-contact technology. By using this sensor the distance which is required for the target can be measured without damage and provides accurate details. The range of this sensor available between 2cms to 400cms.
The HC-SR04 is a type of ultrasonic sensor which uses sonar to find out the distance of the object from the sensor. It provides an outstanding range of non-contact detection with high accuracy & stable readings. It includes two modules like ultrasonic transmitter & receiver. This sensor is used in a variety of applications like measurement of direction and speed, burglar alarms, medical, sonar, humidifiers, wireless charging, non-destructive testing, and ultrasonography.
HC-SR04 Ultrasonic Sensor Pin Configuration
· Pin1 (Vcc): This pin provides a +5V power supply to the sensor.
· Pin2 (Trigger): This is an input pin, used to initialize measurement by transmitting ultrasonic waves by keeping this pin high for 10us.
· Pin3 (Echo): This is an output pin, which goes high for a specific time period and it will be equivalent to the duration of the time for the wave to return back to the sensor.
· Pin4 (Ground): This is a GND pin used to connect to the GND of the system.
Features
The features of the HC-SR04 sensor include the following
· The power supply used for this sensor is +5V DC
· Dimension is 45mm x 20mm x 15mm
· Quiescent current used for this sensor is <2mA
· The input pulse width of trigger is10uS
· Operating current is 15mA
· Measuring angle is 30 degrees
· The distance range is 2cm to 800 cm
· Resolution is 0.3 cm
· Effectual Angle is <15°
· Operating frequency range is 40Hz
· Accuracy is 3mm
HC-SR04 Ultrasonic Sensor Working
The HC-SR04 Ultrasonic sensor comes with four pins namely Vcc pin, Trigger pin, Echo pin, & Ground pin. This sensor is used to measure the accurate distance between the target and the sensor. This sensor mostly works on the sound waves.
When the power supply is given to this module, it generates the sound waves to travel throughout the air to hit the necessary object. These waves strike and come back from the object, then collects by the receiver module.
Here both the distance as well as time has taken is directly proportional because the time taken for more distance is high. If the trigger pin is kept high for 10 µs, then the ultrasonic waves will be generated which will travel at the sound speed. So it creates eight cycles of sonic burst that will be gathered within the Echo pin. This ultrasonic sensor is interfaced with Arduino to gauge the necessary distance between sensor & object. The distance can be calculated using the following formula.
S = (V x t)/2
Where the ‘S’ is the required distance
‘V’ is the sound’s speed
‘t’ is the time taken for sound waves to return back after striking the object.
The actual distance can be calculated by dividing its value with 2 as the time will be twice once the waves travel and get back from the sensor.
CALCULATION:
Sound velocity = 343.00 m/s = 34300 cm/s
The distance of Object (in cm) = Sound velocity *Timer/2
= 32300*Timer/2
= 17150*Timer
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
PROGRAM:
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
#define LCD_Data_Dir DDRD /* Define LCD data port direction */
#define LCD_Command_Dir DDRC /* Define LCD command port direction register */
#define LCD_Data_Port PORTD /* Define LCD data port */
#define LCD_Command_Port PORTC /* 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 (data reg./command reg.) signal pin */
void LCD_Command (char); /* LCD command write function */
void LCD_Char (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,coloumn position and string to LCD function */
void LCD_Command (char cmd) /* LCD command write function */
{
LCD_Data_Port = cmd; /* Write command data to LCD data port */
LCD_Command_Port &= ~((1<<RS)|(1<<RW)); /* Make RS LOW (command reg.), RW LOW (Write) */
LCD_Command_Port |= (1<<EN); /* High to Low transition on EN (Enable) */
_delay_us(1);
LCD_Command_Port &= ~(1<<EN);
_delay_ms(3); /* Wait little bit */
}
void LCD_Char (char char_data) /* LCD data write function */
{
LCD_Data_Port = char_data; /* Write data to LCD data port */
LCD_Command_Port &= ~(1<<RW); /* Make RW LOW (Write) */
LCD_Command_Port |= (1<<EN)|(1<<RS); /* Make RS HIGH (data reg.) and High to Low transition on EN (Enable) */
_delay_us(1);
LCD_Command_Port &= ~(1<<EN);
_delay_ms(1); /* Wait little bit */
}
void LCD_Init (void) /* LCD Initialize function */
{
LCD_Command_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_Command (0x38); /* Initialize 16X2 LCD in 8bit mode */
LCD_Command (0x0C); /* Display ON, Cursor OFF command */
LCD_Command (0x06); /* Auto Increment cursor */
LCD_Command (0x01); /* Clear LCD command */
LCD_Command (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 of string till the NULL */
{
LCD_Char (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_Command((pos & 0x0F)|0x80); /* Command of first row and required position<16 */
else if (row == 2)
LCD_Command((pos & 0x0F)|0xC0); /* Command of Second row and required position<16 */
LCD_String(str); /* Call LCD string function */
}
#define Trigger_pin PA0 /* Trigger pin */
int TimerOverflow = 0;
ISR(TIMER1_OVF_vect)
{
TimerOverflow++; /* Increment Timer Overflow count */
}
int main(void)
{
char string[10];
long count;
double distance;
DDRA = 0x01; /* Make trigger pin as output */
PORTB = 0xFF; /* Turn on Pull-up */
LCD_Init();
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 operation */
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*//*B channel*,Decimal number-0x41 is 65*/
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 */
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);
}
}