Automatic water pump with PZEM-004T

Automatic water pump with stable voltage and dry run production

Arduino uno Rev 3
Ultrasonic sensor
Relay module

But I want to using PZEM-004T for Voltage and ampere reading.
Before Relay module high need to check voltage 200V to 230V then relay pin change to high.
Delay 20000; check Ampere if motor running without water relay pin change to low if normal continue running relay high until tank level 100% . Please suggest me

float d;
void setup() {
 Serial.begin(9600);
 pinMode(7,INPUT);//echo pin of ultraSonic
 pinMode(8,OUTPUT);//trig pin of ultraSonic
 pinMode(10,OUTPUT);// relay
 pinMode(9,OUTPUT);// buzzer pin
}
int low=23;
int high=5;

void vol() //distance calculaion...
{
 digitalWrite(8,HIGH);
 delayMicroseconds(8);
 digitalWrite(8,LOW);
 delayMicroseconds(2);
 d=pulseIn(7,HIGH);
 d=d/69;
}
 
void loop() {
  vol();
 while(1)
  {
   b:
   digitalWrite(10,HIGH);// Pump On...
   delay(2000);
   vol();
   if(d<high) //check high...
    {
     digitalWrite(9,HIGH);// buzzer on.....
     delay(1000);
     digitalWrite(9,LOW);
     goto a;
    }
  }
 while(1)
  {
   a:
   digitalWrite(10,LOW);// pump off...
   delay(100);
   vol();
   if(d>low) //check low
    {
     digitalWrite(9,HIGH);//Buzzer beeping......
     delay(1000);
     digitalWrite(9,LOW);
     delay(1000);
     digitalWrite(9,HIGH);
     delay(1000);
     digitalWrite(9,LOW);
     delay(1000);
     
     goto b;
    }
  }

}

Thanks for your advice I follow up.

Loop() will continuously run so no need to have while() loops inside of loop. Also, get rid of the goto statements :frowning:

It is much better to structure your code as a state machine and then transition from state to state based on your sensors. There are many examples that google will happily share with you.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.