Descriptions:
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent range accuracy and stable readings in an easy-to-use package. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). Similar in performance to the SRF005 but with the low-price of a Sharp infrared sensor.
Specifications:
- Power Supply :5V DC
- Quiescent Current : <2mA
- Effectual Angle: <15°
- Ranging Distance : 2cm – 500 cm/1" - 16ft
- Resolution : 0.3 cm
Sample Program Code:
#include <16F876a.h>
#FUSES NOWDT, HS, NOPUT, PROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=20000000)
#define use_portb_lcd true
#include "flex_lcd.c"
int16 distance, time; // Defining variables
// Defining the pins
#define trig pin_A0 // Change as you wish, can use any pin in the MCU
#define echo pin_A1 // Change as you wish, can use any pin in the MCU
void main()
{
lcd_init(); // initiating the LCD
delay_ms(50);
printf(LCD_PUTC, "\f Sonar test");// for LCD & MCU restart troubleshooting
delay_ms(300); // Boot-up delay, for troubleshooting
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // initiating timer
while(true)
{
output_high(trig); // ping the sonar
delay_us(20); // sending 20us pulse
output_low(trig);
while(!input(ECHO)) // wait for high state of echo pin
{}
set_timer1(0); // setting timer zero
while(input(ECHO)) // Wait for high state of echo pin
{}
time=get_timer1(); // Getting the time
distance=time*0.028 + 1.093 ; // Calculating the distance
lcd_gotoxy(1,1);
printf(lcd_putc, "\fTime :%Lu",time); // Display the time
lcd_gotoxy(1,2);
printf(lcd_putc, "\fDistance = %Lu", distance); //Display distance
delay_ms(1000);
}
}
This comment has been removed by the author.
ReplyDelete