Yeah, Ultrasonic sensor now its working. Today i am gonna to share the code of how its work:)
It quite easy. We just need to connect 4-pin which is VCC,GND,ECHO and TRG from Ultrasonic sensor to Arduino. Done:)
Code :
It quite easy. We just need to connect 4-pin which is VCC,GND,ECHO and TRG from Ultrasonic sensor to Arduino. Done:)
Code :
int
trigger_pin = 4;
int
echo_pin = 2;
int
LED_pin = 13;
int time;
int
distance;
void
setup ( ) {
Serial.begin (9600);
pinMode (trigger_pin, OUTPUT);
pinMode (echo_pin, INPUT);
pinMode (LED_pin, OUTPUT);
}
void loop
( ) {
digitalWrite (trigger_pin, HIGH);
delayMicroseconds (10);
digitalWrite (trigger_pin, LOW);
time = pulseIn (echo_pin, HIGH);
distance = (time * 0.034) / 2;
if (distance <= 10)
{
Serial.println (" Object Detect
");
Serial.print (" Distance=
");
Serial.println (distance);
digitalWrite (LED_pin, HIGH);
delay (500);
}
else {
Serial.println (" No obstacle
");
Serial.print (" Distance=
");
Serial.println (distance);
digitalWrite (LED_pin, LOW);
delay (500);
}
}
Practical:
Comments
Post a Comment