Tuesday, April 18, 2017

ESP8266 Node MCU & Ultrasonic HCSR-04[ Accurate measurement ]

             This Tutorial, We Are Going To used ESP8266 12E Node MCU with Ultrasonic Sensor Module (HC-SR04).

 HC-SR04 Is Used To Measure The Distance Between 2cm to 4meter With Ranging Accuracy Up to 3 Millimeter.

The material required for this tutorial are:-
  1. The Jumpers wires
  2. Arduino Board For (5v) Supply 
  3. Ultrasonic Sensor Module 
  4. Solderless Bread Board And ESP8266 Module It Self.

The Working Principle Of Module Is Very Simple.

It Emits Ultrasonic Sound Which Travelle With The Air If Any Obstacle Comes In The Path It will bounce back from the module.

consider the travel time speed of the sound by this formula:-

   The ultrasonic module having 4 pins
  • GND 
  • VCC
  • ECHO
As you can see the interfacing image on the video

To generate the ultrasonic sound we need to send a trigger on the high state for 10 microseconds.

And 8 the cycle of sonic burst which will the speed of sound and At the echo pin, you will get the double of its number.

The calculation part is as shown

now it's time to going for the tutorial and here design sample code is given. we have to find trigger pins and echo and in these cause these are pin no. 5&4 on ESP8266 NODE MCU then we will need long variables and

I have named it long duration for travel time which will be often for sensor and integer variable for distance in the setup trigger pin as work as input.to start serial communication for showing the result on the serial monitor we will say it board rate 9600
in the main program, we have to stay the trigger pin on low state up to 2 microseconds. now observe ultrasound we have to say trigger pin on high state up to 10 microseconds.

now using pulse in function:-
set the echo pin and second, it can be set high or low.
in the cause of high pulse, the function will wait for go to the high because bounce sound .......it is start timing.
it will wait for low when the sound stops timing. for calculating the distance 

* ESP8266 Node MCU & Ultrasonic HCSR-04[ Accurate measurement ]
* Crated by Ruchita devikar
* www.pictorobo.com
// defines pins numbers
const int trigPin = 5;    // pin no. 5 is D1 ESP8266
const int echoPin = 4;    // pin no. 5 is D2 ESP8266
// defines variables
long duration;
int dist;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication @9600
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
dist= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");

for getting the distance go to the tool and serial monitor and you will get the output distance.
for more details, you can visit my website www.pictorobo.in.

1 comment:

  1. I have been your silent reader for long.. Now I think you have to know how much your articles have inspired me to do better. This is very insightful and informative. Thank you for sharing. I would love to see more updates from you.