// Compiled and tested with no errors on 23/06/2023 By Swapnil Umredkar
/* Measuring water level of any water body with help of ESP32 and
Rotary encoder,water sensor,relay module with reset button*/
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//***************Pin definitions**********************************//
const int moisturePin = 27;
const int relayPin = 14;
// Threshold values for moisture
const int moistureThreshold = 500; // Adjust this value according to your sensor
#define outputA 12 // CLK pin
#define outputB 13 // DT pin
#define rstbtn 4 // reset button pin
#define LED_BUILTIN 15 //led
volatile int counter = 0; // Add "volatile" keyword for interrupt access
const float pi = 3.14; // Pi value
const int R = 7; // Radius of the wheel from center to edge
const int N = 40; // Number of steps for one rotation
float distance = 0;
void IRAM_ATTR readEncoder() { // Add "IRAM_ATTR" attribute for interrupt handling
int bValue = digitalRead(outputB);
if (bValue == HIGH) {
counter++; // Clockwise
}
if (bValue == LOW) {
counter--; // Counterclockwise
}
}
// Get the counter value, disabling interrupts.
// This make sure readEncoder() doesn't change the value
// while we're reading it.
int getCounter() {
int result;
noInterrupts();
result = counter;
interrupts();
return result;
}
void resetCounter() {
noInterrupts();
counter = 0;
interrupts();
}
void setup() {
//////////////
// Initialize the serial communication
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
// Set the relay pin as output
pinMode(relayPin, OUTPUT);
//////
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.print("Water Level ");
lcd.setCursor(0, 1);
lcd.print("Meassuring Tool..");
delay(2000);
lcd.clear();
// Initialize encoder pins
pinMode(outputA, INPUT);
pinMode(outputB, INPUT);
pinMode(rstbtn, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(outputA), readEncoder, FALLING);
}
void loop() {
// Delay for a certain interval before taking the next reading
delay(1000);
distance = ((2 * pi * R) / N) * getCounter(); // in cm
lcd.setCursor(0, 0);
lcd.print("Level: "); // in cm
lcd.setCursor(7, 0);
lcd.print(distance);
lcd.print(" ");
// Read the moisture sensor value
int moistureValue = analogRead(moisturePin);
// Print the moisture value to the serial monitor
Serial.print("Moisture value: ");
Serial.println(moistureValue);
// Check if the moisture value is below the threshold
if (moistureValue < moistureThreshold) {
// Turn on the relay
digitalWrite(relayPin, HIGH);
Serial.println("Relay turned ON");
bool Blink_led =moistureValue < moistureThreshold;
digitalWrite(LED_BUILTIN,LOW );
lcd.setCursor(0, 1);
lcd.print("Waterlevel not found");
} else {
bool Blink_led =moistureValue > moistureThreshold;
digitalWrite(LED_BUILTIN, Blink_led );
// Turn off the relay
digitalWrite(relayPin, LOW);
Serial.println("Relay turned OFF");
lcd.setCursor(1, 1);
lcd.print("Waterlevel Detected");
}
if (digitalRead(rstbtn) == LOW) {
resetCounter();
}
}