Source code for ultrasonic with node mcu
const int trigPin = D1;
const int echoPin = D2;
long duration;
int distance;
void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(D3, OUTPUT);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.println(distance);
Serial.println("cm");
if
(distance > 35)
{
digitalWrite(D3, HIGH);
delay(500);
}
else
{
digitalWrite(D3, LOW);
delay(250);
}
}
Comments
Post a Comment