0% found this document useful (0 votes)
27 views

Obstacle Detector Project

Uploaded by

saiscount01
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Obstacle Detector Project

Uploaded by

saiscount01
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Obstacle Detector Project using NodeMCU ESP8266

// Components Required:
// 1. NodeMCU ESP8266
// 2. Ultrasonic Distance Sensor (HC-SR04)
// 3. Active Buzzer
// 4. Jumper Wires
// 5. Breadboard (optional)

// PIN Connections
const int trigPin = D6; // Ultrasonic Sensor Trigger Pin
const int echoPin = D7; // Ultrasonic Sensor Echo Pin
const int buzzerPin = D8; // Buzzer Pin

// Obstacle Detection Threshold (in centimeters)


const int OBSTACLE_DISTANCE = 20;

void setup() {
Serial.begin(115200);

// Configure Ultrasonic Sensor Pins


pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

// Configure Buzzer Pin


pinMode(buzzerPin, OUTPUT);
}

void loop() {
// Measure Distance
long duration, distance;

// Clear trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Send Ultrasonic Pulse


digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Calculate Distance
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Speed of sound: 340 m/s

// Print Distance for Debugging


Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

// Obstacle Detection Logic


if (distance <= OBSTACLE_DISTANCE) {
// Activate Buzzer
tone(buzzerPin, 1000); // 1 kHz frequency
delay(500); // Buzz for 0.5 seconds
noTone(buzzerPin); // Stop buzzing
}

delay(100); // Small delay between measurements


}

You might also like