You are on page 1of 9

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/326580937

Distance measurement with SR HC 04, NodeMcu ESP 8266 and Blynk

Research Proposal · July 2018


DOI: 10.13140/RG.2.2.21334.34880

CITATIONS READS
0 1,521

1 author:

M. Todica
Babeş-Bolyai University
97 PUBLICATIONS   330 CITATIONS   

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

IR and Raman Investigation of Some Poly(acrylic) Acid Gels in Aqueous and Neutralized State View project

Simple WI-FI remote car with smartphone and Blynk View project

All content following this page was uploaded by M. Todica on 24 July 2018.

The user has requested enhancement of the downloaded file.


Distance measurement with SR HC 04, NodeMcu ESP 8266 and Blynk

M. Todica

In this project we show how to measure distances with the ultrasound sensor SR HC 04
connected to NodeMcu ESP 8266 and send the results to the smartphone, using Blynk
application. There are a lot of projects referring to this subject, but few of them are
concerning with the transmission of data trough the internet. The majority of the projects
use the module SR HC 04 which contains one ultrasound sender and one receiver, (Fig.
1). The principle of distance measurement is very simple: a short ultrasonic pulse
transmitted at the time 0 is reflected by an object and received after a time interval the
echo time,(Fig. 2). Knowing the speed of the ultrasound in air, c= 340 m/s, and the time
, the distance is given by the simple relation d=c /2. (Keep in mind that  represents the

Fig. 1. The ultrasound HC SR 04 module

Fig. 2. The principle of measuring distances with ultrasounds


time to go forward and back of the pulse, for what reason we divided by 2 in previous
equation). When HC SR 04 module is used, a 10μs width trigger pulse is sent to the Trig
pin, and the ultrasonic module will output eight 40kHz ultrasonic signal and detect the
echo back. The measured distance is proportional to the echo pulse width and can be
calculated by the formula above. If no obstacle is detected, the output pin will give a
38ms high level signal. The next pulse can be transmitted when the echo is faded away.
This time period is called cycle period and it is recommended be no less than 50ms. The
pulse diagram is shown in figure 3. For simplest project the management of the pulses is
achieved by Arduino board. However this board cannot ensure internet connection, for
what reason another very popular board, NodeMcu ESP 8266 is used. This board can be
programmed in the same manner as Arduino. Theoretically we can use the same IDE
code as for Arduino, without any modification, if we don’t intend to transmit the data
trough the internet via Blynk server. Generally the codes used for distance measurements
included the function “delay” to set the length of the pulses. However this function stops
the entire code, for the “delay” period, that could causes blocking of the Blynk server. As

Fig. 3. Ultrasound pulse diagram


consequence the code must be modified in order to avoid these undesired situations. The
solution is to replace all “delay” by other functions, for instance “Millis”, which doesn’t
stop the code. That was done in the present work. The new code is presented below.

Hardware
For this project we need only HC SR 04 module and NodeMcu ESP 8266 board.
Optionally we can use Oled 0.96” (124x64 pixels) display if we want do display locally
the data. The electric diagram is presented in figure 4. We must take care that HC SR 04
is 5V operating and NodeMcu ESP 8266 works at 3.3V. For this reason a level logic
converter must be used when connecting the two modules together. A much simplest
solution is the use of 2/3 resistive divider connected to the Echo pin of the HC SR 04.
This is an output pin which provides TTL signals with amplitudes going up to +5V. This
signal is applied to NodeMcu board which accepts only signals smaler than 3.3V. The
resistive divider reduces the amplitude of the Echo signal up to 3.3V. The trigger pin of
the HC SR 04 is an input pin driven by the NodeMcu board and can be activated by
signals with amplitude smaller than +5V. So we don’t need level converter because the
output signal provided by NodeMcu is enough high to drive the ultrasound board. The
ECHO and TRIG pins are connected to the pins D5 and D6 of NodeMcu board using the
syntax:
#define ECHOPIN 12 // Echo pin to D6
#define TRIGPIN 14 // Trig pin to D5

To power the HC SR 04 we need +5V, which is available on the pin VU of NodeMcu, so


we don’t need separately power source. Both the boards can be powered by external
power bank connected to the micro USB port of NodeMcu. If we intend to use the Oled
display we must made the circuit presented in figure 5. This small display is +3.3V
working, so it is directly compatible with NodeMcu board, without any level logic
converter. It is connected as shown in table below.
OLED NodeMcu
GND GND
VDD +3V
SCL D1
SDA D2

The software
For this project we used the version Arduino 1.6.8. Then Arduino must be set to
recognize and communicate with the NodeMcu ESP8266. We can follow the procedure
described in the link below:

5.6 K
2 x 5.6 K

Fig. 4. Connection of ultrasound module to NodeMcu board


http://www.instructables.com/id/Programming-ESP8266-ESP-12E-NodeMCU-Using-
Arduino-/?ALLSTEPS

Briefly the procedure is described below:

1. Download and install the version Arduino 1.6.8 or latter.


2. Add the information of the board ESP8266 to the Arduino. Open the Arduino IDE, go
to the menu File/ Preference, and introduce into the box Additional Boards Manager
URLs, the address of the board of JSON file ESP8266,

5.6 K
2 x 5.6 K

Fig. 5. Connection of ultrasound module and Oled display to NodeMcu


http://arduino.esp8266.com/package_esp8266com_index.json.

3. Manage ESP library. In the Arduino IDE go to the menu Sketch/ Include Library/
Manage Libraries/ and add the Library of ESP8266.
4. Set the NodeMcuESP8266 board. Go to menu Tools/ Board/ Boards Manager/, Select
NodeMCU 1.0 (ESP-12E Module).

5. Downoload and install Blynk library 0.4.0 from


http://www.blynk.cc/getting-started/

At this stage the Arduino is able to recognize and communicate with the board.
If we want to use the Oled display we must download and install the libraries Adafruit
GFX and Adafruit SSD1306. We can use the link below.
http://arduino-er.blogspot.ro/2016/04/nodemcu-esp8266-to-display-on-128x64.html

Setting the Blynk application.

Download Blynk application from the link: http://www.blynk.cc/getting-started, and


install it on the smartphone. Create an account with your email and password. Open a
new project, give it a name and select your board, (Arduino UNO ESP 8266 WI-FI). The
Blynk server will send "YourAuthToken". Copy it into the Arduino code. Go to the
Widget Box and add widget terminal on virtual V12, (Fig. 6).

The code
Open new sketch in Arduino IDE and upload the following code:

//////////////////////////

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "YourAuthToken";

// Your WiFi credentials.


// Set password to "" for open networks.
char ssid[] = "Your SSID";
char pass[] = "Your password";

#define ECHOPIN 12 //Echo to pin D6


#define TRIGPIN 14 //Trig to pin D5

unsigned long previousMillisA = 0;


unsigned long previousMillisB = 0;

const long intervalA = 2;


const long intervalB = 10;
const long intervalC = 1000; // cycle period
SimpleTimer timer;
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

// OLED display TWI address


#define OLED_ADDR 0x3C

Adafruit_SSD1306 display(-1);

void setup()

{
Serial.begin(9600);

pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
Blynk.begin(auth, ssid, pass);

timer.setInterval(1000L, sendUptime);

display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,40);
display.print("radar");
display.display();

void sendUptime()
{

unsigned long currentMillisA = millis();


unsigned long currentMillisB = millis();

if (currentMillisB - previousMillisB >= intervalC) {


previousMillisB = currentMillisB;// trebuie

if (currentMillisA - previousMillisA <= intervalA) {


previousMillisA = currentMillisA;
digitalWrite(TRIGPIN, LOW);
}

else {
digitalWrite(TRIGPIN, HIGH);
}

if (currentMillisA - previousMillisA >= intervalB) {

digitalWrite(TRIGPIN, LOW);
}

float distance = pulseIn(ECHOPIN, HIGH);


distance= distance/50;

Serial.println(distance);
Blynk.virtualWrite(12, " ");
Blynk.virtualWrite(12, distance);
display.clearDisplay();
display.setTextSize(4);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print(distance);
display.display();
}
}

void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer

}
////////////////////////////

After uploading the code the board can be disconnected from the computer and powered
by an externally separately source. It will become to work and send the data to the
smartphone. For reliable connection to the internet the board NodeMcu must be placed in
the area of strength signal of the WI FI router.
View publication stats

Fig. 6. Setting the Blynk workspace

You might also like