Hello Guys , In this post we learn how to connect nodemcu with ultrasonic sensor in easy way.
Let Start it:-
*Parts Required
1.Nodemcu
2.Ultrasonic sensor
3.Jumper wire
*Schematics & Diagrams
-do all the connection according to this diagram.
*Lets coding time
-Copy The following code in ardunio ide
/*********
-Tech Gyan Creative
Complete project details at https://techgyancreative.blogspot.com
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*********/const int trigP = 2; //D4 Or GPIO-2 of nodemcu
const int echoP = 0; //D3 Or GPIO-0 of nodemcu
long duration;
int distance;
void setup() {
pinMode(trigP, OUTPUT); // Sets the trigPin as an Output
pinMode(echoP, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Open serial channel at 9600 baud rate
}
void loop() {
digitalWrite(trigP, LOW); // Makes trigPin low
delayMicroseconds(2); // 2 micro second delay
digitalWrite(trigP, HIGH); // tigPin high
delayMicroseconds(10); // trigPin high for 10 micro seconds
digitalWrite(trigP, LOW); // trigPin low
duration = pulseIn(echoP, HIGH); //Read echo pin, time in microseconds
distance= duration*0.034/2; //Calculating actual/real distance
Serial.print("Distance = "); //Output distance on arduino serial monitor
Serial.println(distance);
delay(3000); //Pause for 3 seconds and start measuring distance again
}
-choose your com port and board type
-upload the code
-after uploading open serial monitor at 9600 baud rate.
-Enjoy

