Simple water level project

First I know there are so many topics about the esp and arduino regarding the water level and I read a lot but due to my level in programming is very bad and just two days ago I got new ESP8266 I start with.
I just want to get a code to my ESP8266 D1 to measure the water level with simple gauge and not in such advanced codes as I found in the forums of Blynk.
I appreciate if you help me by sending a very simple code so i can learn from it and add and modify.

thank in advance

Providing some indication about the type of water level gauge you are planning to use would be a good starting point.


So… you want someone else to write you a simple code so you don’t have to learn how to write a simple code :stuck_out_tongue:

Well, using a simple water gauge (float with ON/OFF) you can use this example builder sketch…

Or this using interrupts…

Lots of other Blynk examples in there as well

I mentioned that I’m new and I have no experience in coding.
thank you for your help

Yes… you said… guess what, so was I.

There are literally millions of examples of coding on the web, along with simple tutorials and hundreds, if not more, simple examples and even explanations in this forum.

I also tend to learn from example… and I started with Blynk long before they added in the Sketch Builder… so if I was able to Google and learn to program (despite some medical issues involving staying awake long enough :stuck_out_tongue: ), I suspect anyone can.

So, on that note… I already pointed you to exactly what you asked for… try it out.

1 Like

Please see the code below and correct me if I’m wrong.
I connect the trigger of the HCR-04 to D5 and echo to D5 then the gause on Blynk app to V5 , i cant see any action from the gauge when changing the distance

#define TRIGGER 4
#define ECHO    5

// NodeMCU Pin D0 > TRIGGER | Pin D2 > ECHO

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "my SSID";
char pass[] = "my PASS";
void setup() {
  Serial.begin (9600);
  Blynk.begin(auth, ssid, pass, "", 8080);
  pinMode(ECHO, INPUT);

void loop() {
  long duration, distance;
  digitalWrite(TRIGGER, LOW);  
  digitalWrite(TRIGGER, HIGH);
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;

   if (distance <=200) {
    Blynk.virtualWrite(V0, 255);
  else {
    Blynk.virtualWrite(V0, 0);

 if (distance <= 35) {
    Blynk.virtualWrite(V1, 255);
  else {
    Blynk.virtualWrite(V1, 0);

   if (distance <= 30) {
    Blynk.virtualWrite(V2, 255);
  else {
    Blynk.virtualWrite(V2, 0);

   if (distance <= 25) {
    Blynk.virtualWrite(V3, 255);
  else {
    Blynk.virtualWrite(V3, 0);

   if (distance <= 20) {
    Blynk.virtualWrite(V4, 255);
  else {
    Blynk.virtualWrite(V4, 0);

  Blynk.virtualWrite(V5, distance);


OK, firstly, your choice of sensor is NOT a “simple” one in that it requires particular timing, especially when used with Blynk which has it’s own timing needs.

Secondly, the preference is to NOT use the silk screened Dx pin designations as they do differ between various types of boards.

Your sketch is using the GPIO numbering, so should you when wiring it up.

For the Wemos D1 mini… Google for yours if it is different…

Wemos D1 Mini Pinout

Thirdly, Virtual pins are NOT directly associated with GPIO numbering, they instead refer to similarly labeled Blynk functions, commands and widgets.

And finally… do NOT dump everything into your void loop() when programming with Blynk!

If you do insist on using your ultrasonic sensor to learn with, look at this example and simply disregard the servo if you don’t have one (the gauge widget will still show positioning).

I appreciate your support
my board is Wemos D1 R1 as shown

now I connect the trigger to D5 and echo to D6 without changing the code you send except the auth , SSID and password
also the gauge is connected to V0 in Blynk app but there is no measurement and same when put the gauge on V1
I couldn’t download the Blynk Qr code to my mobile since energy is 500
please advice

Ah, the wonderfully confusing D1 R1 board!

GPIO13, which connects to the Trigger Pin on your sensor, is the pin labelled “D11/MOSI/D7”
GPIO12, which connects to the Echo pin on your sensor, is the pin labelled “D12/MISO/D6”

You should have a Labelled Value widget connected to pin V0


1 Like

trigger is connected on GPIO 13 even in my board is labeled with D1 /MIOS/D7 ,in my board the D11 in different position than your picture

and the echo is D12/MISO/D6 and still no change on Blynk app gauge at changing the sensor distance

Comparing your image with @PeteKnight image - the D11 is in the same position.
In your sketch:

This is:
Trigger = GPIO 4 == Wemos D1R1 pin D4 (D14/SDA/D4)
Echo = GPIO 5 == Wemos D1R1 pin D3 (D15/SCL/D3)

Later in your comments you mention D5 and D6 . . . then later you mention GPIO 13 . . .

I would suggest that you remove all the blynk/wifi code from your sketch and get the sensor working with simple Serial Monitor output.

You need to clarify exactly which GPIO you have defined in your sketch, then connect the sensor to the matching physical pin on the board (printed on the board as D1, D2, D3 etc with the corresponding GPIO number on the reverse of the board.

It can be confusing but you need to be very clear on the GPIO assignment vs physical pin assignment.

Once you have the sensor operating with serial ouput, then add the wifi/blynk code and send the results to Blynk.

Keep it simple to start with.




I used your sketch as the basis for a test. Removed or commented out Blynk commands and it worked perfectly printing distance in cm to serial monitor.

Trigger = GPIO 4 == Wemos D1R1 pin D4 (D14/SDA/D4)
Echo = GPIO 5 == Wemos D1R1 pin D3 (D15/SCL/D3)


Check you code, remove Blynk - get the senor working, then reinstate Blynk . . .



@Bill_Donnelly Sorry I wasn’t specific in my post ,I was referring to GTT code as below how it will be according to Wemos D1
int trigPin = 12; // D5 on Wemos D1 Mini
int echoPin = 13; // D6 on Wemos D1 Mini
int servoPin = 4; // D2 on Wemos D1 Mini

Based on the upside down image you posted, the back of your board seems to show the GPIO numbering… if so, don’t use 13 or 15 as they are probably used by the serial link to the PC?

Just pick some other GPIO pins on your board, make sure the code references the same GPIO pin numbering and test until it works.

On the Wemos D1R1 board these pins work - I tested and confimed (note: It is not a Wemos Mini which has different pin outs)


1 Like