Project discussion : water level indicator with auto cut off and purity checker

i am building a microcontroller water level indicator with auto cut off and purity checker. My relay seems to function but it's unable to drive the water pump

please i need an instant help with this

Thoughts and prayers

3 Likes

I'll need the code in the code tags the <CODE/> button and a schematic(hand drawn is fine).

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX pins for serial communication with ESP32

float calibration_value = 26-0.05;
int pHValue =0;
unsigned long int avgval;
int buffer_arr[10],temp;
int sensorPin = A2;

const int tdsSensorPin = A1;
const float tdsCalibrationFactor = 0.5;
int tdsValue =0;

float ph_act;
// Pin definitions
const int ultrasonicTriggerPin = 5;
const int ultrasonicEchoPin = 6;
const int relayPin = 4;

// Sensor thresholds
const int waterLevelThreshold = 70;
void setup() {
  // put your setup code here, to run once:
  pinMode(ultrasonicTriggerPin, OUTPUT);
  pinMode(ultrasonicEchoPin, INPUT);
  pinMode(relayPin, OUTPUT);

  digitalWrite(relayPin, LOW); // Initially turn off the relay

  Serial.begin(9600);
  mySerial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  int waterLevel = getWaterLevel();

  // Check water level
  //if ((pHValue >=4 && pHValue <=9) && tdsValue < 190 ) {
    if(waterLevel <= 5 ){
    digitalWrite(relayPin, HIGH);
   // Serial.println("Water level is sufficient!");
    }
     
  //}
  else {
    digitalWrite(relayPin, LOW);
    //Serial.println("Water level is low!");
  }
  // Display water level visually
  for (int i = 0; i < waterLevelThreshold; i++) {
    if (i < waterLevel) {
      Serial.print("|");
    } else {
      Serial.print("-");
    }
  }
  Serial.println();

  delay(700); // Adjust the delay as per your requirements

  for(int i=0;i<10;i++){
    buffer_arr[i]=analogRead(A0);
    delay(30);
  }
  for(int i=0;i<9;i++){
    for(int j=i+1;j<10;j++){
      if(buffer_arr[i]>buffer_arr[j]){
        temp=buffer_arr[i];
        buffer_arr[i]=buffer_arr[j];
        buffer_arr[j]=temp;
      }
    }
  }
  avgval=0;
  for(int i=2;i<8;i++)
  avgval+=buffer_arr[i];
  float volt=(float)avgval*5.0/1024/6;
  ph_act = -5.70 * volt + calibration_value;
  float pHValue = constrain(ph_act, 0, 14);

  //Serial.println("pH val: ");
  String finalpHValue = String(pHValue);
  mySerial.println(finalpHValue);
  //mySerial.println();
  delay(5000); 

  // Reading the analog value from the TDS sensor
  int tdsRawValue = analogRead(tdsSensorPin);

  // Convert the raw analog value to TDS in ppm using the calibration factor
  float tdsValue = tdsRawValue * tdsCalibrationFactor;

  // Print the TDS value to the serial monitor
  //Serial.print("TDS Value (ppm): ");
  String result = String(tdsValue);
  mySerial.println(result);

  delay(5000);

  // Check water quality based on TDS value
  if (tdsValue <25 && pHValue >=6.2 && pHValue <= 7.5 ) {
    mySerial.println("Excellent");
  } else if (tdsValue >=25  && tdsValue <= 85 && pHValue >=6.2 && pHValue <= 7.5 ) {
    mySerial.println("Good");
  }else if (tdsValue >=85  && tdsValue <=190 && pHValue >=5.0 && pHValue <= 9 ) {
    mySerial.println("Acceptable");
  }  else {
    mySerial.println("Poor");
  }

 

  delay(1000); // Adjust the delay as per your requirements
}

// Function to measure water level using ultrasonic sensor
int getWaterLevel() {
  digitalWrite(ultrasonicTriggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ultrasonicTriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultrasonicTriggerPin, LOW);

  long duration = pulseIn(ultrasonicEchoPin, HIGH);
  int waterLevel = duration * 0.034 / 2; // Calculate distance based on speed of sound

  return waterLevel;
}


@theeccentricgenius
here the code goes

The schematic will make things easier.

Thanks for posting the code.

Welcome to the Forum! Read the forum guidelines to see how to properly ask a question and some good information on making a good post.
You will get faster and better help if you choose a more descriptive title for your topic.

2 Likes

@theeccentricgenius
i currently don't have the schematic but i can send an image

Hello chipismith

Welcome to the worldbest Arduino forum ever.

If you expect the system to react in real time, you must prevent this:

Line 62: delay(700); // Adjust the delay according to your requirements.
	line 66: delay(30);
	Line 88: delay(5000); 
	Line 101: delay(5000);
	Line 116: delay(1000); // Adjust the delay according to your requirements

Have a nice day and enjoy coding in C++.

Well... that is a start, send it and draw up the schematic.

Make sure the picture is provided from the top view and nice and sharp.... A few side views won't hurt either.

Please still provide an schematic however.

as an error report that is a bit vauge
have you a multimeter to check that the relay contacts are closing

you may require an additional contactor to drive the pump
what is the rating of the relay (for an inductive load) and the pump
e.g. recently to drive a Jabsco_28520_pumpI used a 20amp contactor

how do i cause a delay then

the relay is 10A 250VAC

Design your own none blocking timer() functions by using the BlinkWithOutDelay eample of the IDE.

Where does any power get fed to the pump?

Relays switch power. It looks like you just shorting the leads where you would otherwise be attaching a battery or power supply.

Schematic! Show all sources of power and how they get where they are needed.

a7

unless otherwise stated that is probably for a resistive load
reduce it to 2 or 3 amps for an inductive load (squirrel cage motor)

please check the image