Ultrasonic Sensor
Ultrasonic Sensor
Applications
- Ultrasonic Sensors for Fill Level Monitoring in Gravel Silos
- Ultrasonic Sensors Detect Fill Level for Bulk Goods
- Detecting Boom Height on Agricultural Machine
- Ultrasonic Sensors for Anti-Collision Detection on Aerial Work Platforms
Sample Project Using Arduino:
Materials Required:
- ARDUINO UNO Board
- Ultrasonic sensor
- Buzzer
Circuit:
Code:
int buzzer = 13; //initialize buzzer on pin 13
int trigger = 7; // initialize triggering on pin 7
int echo= 8; // initialize echo on pin 8
void setup(){
Serial.begin(9600); // Enable serial comunication, so we can
see the distance on the serial monitor
pinMode(trigger, OUTPUT); //definining trigger as output
pinMode(echo, INPUT); // defining echo as input
pinMode(buzzer, OUTPUT); //defining buzzer as output
digitalWrite(buzzer,LOW); //initilizing the buzzer value as
LOW
}
void loop(){
int duration, distance; //Adding two variables
duration and distance
digitalWrite(trigger, HIGH); //triggering the wave by giving
HIGH
delay(10);
digitalWrite(trigger, LOW); //after sometime the triggering
is stopped
duration = pulseIn(echo, HIGH); //a special function for
listening and waiting for the wave
distance = (duration/2) / 29.1; //transforming the number to
cm
delay(1000);
Serial.print(distance); //printing the distance in serial
monitor
Serial.print("cm"); //printing the unit of
distance as cm
Serial.println(" "); //just printing to a new line
if (distance < 35) //checking whether the distance is
less than 35cm
{
digitalWrite(buzzer,HIGH); //if so make the buzzer to turn
ON
Serial.println("Buzzer On"); //also printing as
Buzzer On in serial monitor
}
else
{
digitalWrite(buzzer,LOW); //if the condition is not
satisfied means buzzer is turned off
}
}
Serial.println("Buzzer On"); //also printing as
Buzzer On in serial monitor
}
else
{
digitalWrite(buzzer,LOW); //if the condition is not
satisfied means buzzer is turned off
}
}
Serial Monitor Output:
Comments
Post a Comment