#define triggerPin 2 // Pin connected to the TRIGGER pin of the ultrasonic sensor
#define echoPin 4 // Pin connected to the ECHO pin of the ultrasonic sensor
#define buzzerPin 5 // Pin connected to the buzzer
void setup() {
pinMode(triggerPin, OUTPUT);// Initialize the TRIGGER pin as an output
pinMode(echoPin, INPUT); // Initialize the ECHO pin as an input
pinMode(buzzerPin, OUTPUT);// Initialize the buzzer pin as an output
}
void loop() {
// Generate trigger pulse
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Measure the pulse from the echo pin
long duration = pulseIn(echoPin, HIGH);
// Convert pulse duration to distance (Speed of sound: 343m/s, 0.0343 cm/us, accounting for round-trip travel)
int distance = duration * 0.0343 / 2;
// If an object is detected within 50cm, activate the buzzer
if (distance < 50) {
tone(buzzerPin, 1000, 1000); // Generate a 1000ms beep
delay(1000); // Wait for 1000ms
noTone(buzzerPin); // Turn off the buzzer
delay(100); // Wait for 100ms
}
delay(100); // Wait for 100ms before the next reading
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
bz1:1
bz1:2
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND