//scinario:Design contact less Calling Bell using Ultrasonic Sensor
int trigPin=10;
int echoPin=11;
int buzzerPin=12;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
long duration,distance;
// Clear the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send a 10us pulse to the trigger pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the pulse on the echo pin
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
distance = duration * 0.034 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check if someone is close (within 50 cm) and trigger the buzzer
if (distance < 50) {
tone(12,262,1024);
delay(500); // Buzzer ON time (adjust as needed)
// tone(buzzerPin, LOW);
// delay(2000); // Buzzer OFF time (adjust as needed)
}
else
{
tone(12,262,250);
delay(2000); // Buzzer OFF time (adjust as needed)
}
}