Make water level monitoring with gpios pins

Hi everyone, i am new here. I need to make water level monitoring without and sensors. I just want to use gpios pins which connected with ground to give water level. Like this i want to use 4 gpios pins for 25%, 50%, 75% and 100% for water level monitoring. I used ESP8266 as well as if possible esp01 is also like a boom for me because there are 2 gpios pins in this and tx and rx is also usable pins. I need your guidance to intiate this process so i can get success in this project and monitor the water level using blynk application.

Good luck with that!

You’d be better using either an ultrasonic sensor or float switches placed at various points in the tank.

Pete.

I just decided to use wire instead of any ultrasonic sensor and floating switch. Because when wires are connected with water it will send signal. Which helps to monitor level.

Have you tested this, and obtained reliable results?

Pete.

Yes, but using web server. I need modification to use in blynk app.

I’d start by looking at the Blynk examples that are installed when you install the Blynk C++ library, but don’t use the Edgent examples.

These will show you the basic structure of a Bl6nk sketch for your board, and how to use BlynkTimer to send data to the Blynk server.

Pete…

Thanks, finally i found solution somehow and my project works fine.

Would you like to share the details, so that other community members can refer to it in future?

Pete.

1 Like
#define BLYNK_TEMPLATE_NAME "Water level"
#define BLYNK_AUTH "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define BLYNK_AUTH "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define SENSOR_1 D1
#define SENSOR_2 D2
#define SENSOR_3 D5
#define SENSOR_4 D6

char auth[] = BLYNK_AUTH;
char ssid[] = "wifi name";
char pass[] = "wifi password";

BlynkTimer timer;

int tankLevel = 0;

void setup() {
  Serial.begin(9600);
  pinMode(SENSOR_1, INPUT_PULLUP);   
  pinMode(SENSOR_2, INPUT_PULLUP);
  pinMode(SENSOR_3, INPUT_PULLUP);
  pinMode(SENSOR_4, INPUT_PULLUP);

  Blynk.begin(auth, ssid, pass);
}

void loop() {
  Blynk.run();
  timer.run();

  updateTankLevel();

  // Send tank level to Blynk every 1 seconds
  Blynk.virtualWrite(V1, tankLevel);
  delay(1000);  // Adjust delay as needed
}

void updateTankLevel() {
  if (digitalRead(SENSOR_4) == LOW) {
    tankLevel = 100;
  } else if (digitalRead(SENSOR_3) == LOW) {
    tankLevel = 75;
  } else if (digitalRead(SENSOR_2) == LOW) {
    tankLevel = 50;
  } else if (digitalRead(SENSOR_1) == LOW) {
    tankLevel = 25;
  } else {
    tankLevel = 0;
  }
}

This is the code that monitors water levels with only GPIO pins. There is no need for any sensors.
GPIO pins used:
D1 = 25%
D2 = 50%
D5 = 75%
D6 = 100%

More pins are also addable according to requirements.

@Karan_Sethi Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

#define BLYNK_TEMPLATE_NAME “Water level"
#define BLYNK_AUTH "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define SENSOR_1 D1
#define SENSOR_2 D2
#define SENSOR_3 D5
#define SENSOR_4 D6

char auth[] = BLYNK_AUTH;
char ssid[] = "wifi name";
char pass[] = "wifi password";

BlynkTimer timer;

int tankLevel = 0;

void setup() {
  Serial.begin(9600);
  pinMode(SENSOR_1, INPUT_PULLUP);   
  pinMode(SENSOR_2, INPUT_PULLUP);
  pinMode(SENSOR_3, INPUT_PULLUP);
  pinMode(SENSOR_4, INPUT_PULLUP);

  Blynk.begin(auth, ssid, pass);
}

void loop() {
  Blynk.run();
  timer.run();

  updateTankLevel();

  // Send tank level to Blynk every 1 second
  Blynk.virtualWrite(V1, tankLevel);
  delay(1000);  // Adjust delay as needed
}

void updateTankLevel() {
  if (digitalRead(SENSOR_4) == LOW) {
    tankLevel = 100;
    // Trigger event in Blynk if tank level is 100%
    Blynk.logEvent("water_tank_full");
  } else if (digitalRead(SENSOR_3) == LOW) {
    tankLevel = 75;
  } else if (digitalRead(SENSOR_2) == LOW) {
    tankLevel = 50;
  } else if (digitalRead(SENSOR_1) == LOW) {
    tankLevel = 25;
  } else {
    tankLevel = 0;
  }
}

above code for those who require an alert when the water tank touches 100%.

You obviously didn’t understand the role of BlynkTimer.
You shouldn’t be using blocking delays in your code, and this…

shouldn’t be in your void loop.

Also this…

will log an event once every second while the tank is full, which will use up your daily allowance of events within 100 seconds.
You should read this…

You might so want to redact your Auth token and WiFi password from your sketches.

Pete.

I got it.

I need help in this code, actually i want to add a relay with it. And i want that the pump will turn off, when water touch 100%. I tried but did not worked. Kindly anybody modify this code or guide me.

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME "Water level"
#define BLYNK_AUTH "xxxxxxxxxxxxxxxxxxxxxxxxxxxram"
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define SENSOR_1 D1
#define SENSOR_2 D2
#define SENSOR_3 D5
#define SENSOR_4 D6
#define RELAY_PIN D7  // Change this to the GPIO pin connected to the relay

char auth[] = BLYNK_AUTH;
char ssid[] = "";
char pass[] = "";

BlynkTimer timer;

int tankLevel = 0;
int relayState = LOW;

void setup() {
  Serial.begin(9600);
  pinMode(SENSOR_1, INPUT_PULLUP);   
  pinMode(SENSOR_2, INPUT_PULLUP);
  pinMode(SENSOR_3, INPUT_PULLUP);
  pinMode(SENSOR_4, INPUT_PULLUP);
  pinMode(RELAY_PIN, OUTPUT);  // Set relay pin as output

  Blynk.begin(auth, ssid, pass);
  // Set up a virtual pin to control the relay
  Blynk.virtualWrite(V2, relayState);
}

void loop() {
  Blynk.run();
  timer.run();

  updateTankLevel();

  // Send tank level to Blynk every 1 seconds
  Blynk.virtualWrite(V1, tankLevel);
  delay(1000);  // Adjust delay as needed
}

BLYNK_WRITE(V2) {
  int newRelayState = param.asInt();
  if (newRelayState != relayState) {
    relayState = newRelayState;
    digitalWrite(RELAY_PIN, relayState);
  }
}

void updateTankLevel() {
  if (digitalRead(SENSOR_4) == LOW) {
    tankLevel = 100;
    // Trigger event in Blynk if tank level is 100%
    Blynk.logEvent("water_tank_full");
    // Turn off the relay (pump)
    digitalWrite(RELAY_PIN, LOW);
  } else if (digitalRead(SENSOR_3) == LOW) {
    tankLevel = 75;
  } else if (digitalRead(SENSOR_2) == LOW) {
    tankLevel = 50;
  } else if (digitalRead(SENSOR_1) == LOW) {
    tankLevel = 25;
  } else {
    tankLevel = 0;
    // Turn on the relay (pump)
    digitalWrite(RELAY_PIN, relayState);
  }
}

I just tried and used V2 as a virtual pin, even though I did not turn on and off the relay manually using Blynk.

And I want to ask you: Can I use 3V and GND to power the relay from the ESP8266? Does it work fine? Or do I need to get power from another source? Is it still working fine?

Your void setup need to initialise a BlynkTimer that will call your updateTankLevel function at the required interval. Because that’s missing, your BlynkTimer is doing nothing.

Read this for a simple example…

If you want to understand more about BlynkTimer then read this…

You haven’t taken onboard my comments about the log event code, so testing of notifications won’t go the way that you hope, and you’ll need to wait 24 hours after running the sketch to allow the event limit to clear.

Having this in your void setup probaly won’t produce the results you’re expecting either…

Because it takes time for the Blynk connection to be established. Depending on exactly what it is that you’re trying to achieve, you may be better reading the document in for BLYNK_CONNECTED()

It depends on your relay. Either way, you’d be better powering the relay from the 5v pin on your board rather than the 3.3v pin, as it’s easy to overload the onboard voltage regulator which converts the 5v supply to 3.3v.

Pete.

#define BLYNK_TEMPLATE_NAME "Water level"
#define BLYNK_AUTH "pFbl_2ihJ"
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define SENSOR_1 D1
#define SENSOR_2 D2
#define SENSOR_3 D5
#define SENSOR_4 D6
#define RELAY_PIN D7  // Change this to the GPIO pin connected to the relay

char auth[] = BLYNK_AUTH;
char ssid[] = "SWE";
char pass[] = "";

BlynkTimer timer;

int tankLevel = 0;
int relayState = LOW;

void setup() {
  Serial.begin(9600);
  pinMode(SENSOR_1, INPUT_PULLUP);   
  pinMode(SENSOR_2, INPUT_PULLUP);
  pinMode(SENSOR_3, INPUT_PULLUP);
  pinMode(SENSOR_4, INPUT_PULLUP);
  pinMode(RELAY_PIN, OUTPUT);  // Set relay pin as output

  Blynk.begin(auth, ssid, pass);
  // Set up a virtual pin to control the relay
  Blynk.virtualWrite(V2, relayState);
}

void loop() {
  Blynk.run();
  timer.run();

  updateTankLevel();

  // Send tank level to Blynk every 1 second
  Blynk.virtualWrite(V1, tankLevel);
  delay(1000);  // Adjust delay as needed
}

BLYNK_WRITE(V2) {
  int newRelayState = param.asInt();
  if (newRelayState != relayState) {
    relayState = newRelayState;
    digitalWrite(RELAY_PIN, relayState);
  }
}

void updateTankLevel() {
  if (digitalRead(SENSOR_4) == LOW) {
    tankLevel = 100;
    // Trigger event in Blynk if tank level is 100%
    Blynk.logEvent("water_tank_full");
    // Turn off the relay (pump)
    digitalWrite(RELAY_PIN, LOW);
  } else if (digitalRead(SENSOR_3) == LOW) {
    tankLevel = 75;
  } else if (digitalRead(SENSOR_2) == LOW) {
    tankLevel = 50;
  } else if (digitalRead(SENSOR_1) == LOW) {
    tankLevel = 25;
  } else {
    tankLevel = 0;
    // Turn on the relay (pump)
    digitalWrite(RELAY_PIN, relayState);
  }
}