Ultrasonic sensor comes very handy for small projects. It can be used for making radar, measuring distance, different types of alarm system. You can download ultrasonic sensor HC-SR04 datasheet. Arduino connection with ultrasonic sensor is shown in picture.
void setup()
{
pinMode(12,OUTPUT);//pin for trigger
pinMode(11,OUTPUT);//output for led bulb
pinMode(13,INPUT);//pin for echo
Serial.begin(9600);
}
void loop()
{
digitalWrite(12,HIGH);
delayMicroseconds(30);
digitalWrite(12,LOW);
delayMicroseconds(2);
int a=pulseIn(13,HIGH);//gives time after which high pulse is coming
int d=(a*0.034)/2;//342 is the speed of sound
Serial.print(d);//print on serial monitor
Serial.println(" cm");
delay(50);
if(d==50)
{
digitalWrite(11,HIGH);//glows led
}
else
{
digitalWrite(11,LOW);
}
}
You can set range for led bulb to work.
Example:
if(d>=50&&d<=100)
{
digitalWrite(11,HIGH);//glows led
}
if(d>=1&&d<=49)
{
digitalWrite(11,LOW);
}
Comments
Post a Comment