Hello everyone! I need help with my project. It seems that the Ultrasonic Sensor is not detecting any motion/object. When I run the code, automatically the LED and Buzzer activate and will loop every 2 second, and will send notification to my Blynk app/email without even going near to the designated distance. I tried to remove the Ultrasonic Sensor and run the code, but still the behavior is still the same. I tested the Ultrasonic Sensor and ESP8266 with sample code and it works fine. What could be the problem ?
HERE’S THE CONNECTION:
Connect the VCC pin of the Ultrasonic Sensor to the 5V pin on the NodeMCU.
Connect the GND pin of the Ultrasonic Sensor to the GND pin on the NodeMCU.
Connect the Trig pin of the Ultrasonic Sensor to the D2 pin on the NodeMCU.
Connect the Echo pin of the Ultrasonic Sensor to the D1 pin on the NodeMCU.
Connect the LED anode (positive leg) to the D6 pin through a 220 ohm resistor on the NodeMCU.
Connect the LED cathode (negative leg) to GND pin on the NodeMCU.
Connect the positive terminal of the buzzer to the D7 pin on the NodeMCU.
Connect the negative terminal of the buzzer to the GND pin on the NodeMCU.
CODE GOES HERE:
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "your_auth_token";
char ssid[] = "your_wifi_ssid";
char pass[] = "your_wifi_password";
BlynkTimer timer;
const int trigPin = D2;
const int echoPin = D1;
const int ledPin = D6;
const int buzzerPin = D7;
long duration;
int distance;
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
timer.setInterval(2000L, sendUltrasonicData);
}
void loop() {
Blynk.run();
timer.run();
}
void sendUltrasonicData() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance < 10) {
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
Blynk.logEvent("notification", "Someone is at the door!");
delay(1000);
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
}
}