Make water level monitoring with gpios pins

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);
  }
}

This code works fine for me but i face difficulty in controlling the relay like
I am unable to power the relay using esp8266. Due to low voltage. As if i try to use external power using 5v. It always stay on and not responding while changing the configuration using blynk app. However, i tried one thing i use positive point to connect the positive of relay along with same positive I connect to ESP8266 3v pin then the rely response perfectly but it is too complicated for me. I want to give external power to control the relay but I don’t know how!

You don’t want to listen to my advice about fixing your coding, so I’m done with this topic.

Pete.

1 Like

I am sorry about it. Actually, I am too bad at coding, and I don’t know the meaning of any function. I just used CHAT-GPT for coding. So, it has become difficult for me too understand what you are saying. So, that’s why I request that you please update the code for me. I will be very grateful too you.

I’m not going to re-write your code for you.
Your basic concept of using GPIO pins as liquid level sensors is flawed in my opinion, and won’t reliably give consistent results in all situations.

As i said initially, you’d be better using an ultrasonic sensor or float switches. There are lots of example sketches of projects using an ultrasonic sensor and Blynk on the forum, and if your coding skills are inadequate then that would be the best approach.

Pete.

1 Like

Ok thanks for helping.