#define TRIG_PIN 27 // Changed to GPIO 27
#define ECHO_PIN 13 // Changed to GPIO 13
#define BUZZER_PIN 33 // Changed to GPIO 33
long duration;
float distance;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
// Send trigger signal
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo time
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance (in meters)
distance = (duration * 0.034 / 2) / 100;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" m");
if (distance < 5.0) { // If vehicle is within 5 meters
tone(BUZZER_PIN, 1000); // Generate a 1kHz tone on buzzer
} else {
noTone(BUZZER_PIN); // Stop the tone on buzzer
}
delay(100);
}