So the relay is connected to digital pin 3.
Its quite straight forward the code so please help me out.
int sensor = A0; //Sets pin A0 as our sensor input.
int RELAY_0 = 3; //sets digital pin 2 as our relay control.
unsigned long startBlink = 0UL; // because millis() returns unsigned long
unsigned long waitBlink = 0UL; // because millis() returns unsigned long
void setup() {
Serial.begin(9600); //starts up the serial monitor.
pinMode(RELAY_0, OUTPUT);
}
void loop() {
Serial.println("turn off pump per default");
digitalWrite(RELAY_0, LOW); // turn off pump 5 seconds not working.
Serial.println("run plant zero");
Plant_zero();
}
void Plant_zero() {
//sets the relay to cut the power to the track
int SOIL_MOISTURE_0 = analogRead(sensor); //Sets valA1 as whatever the sensor reads
delay(5000);
Serial.println("read sensor wait..");
Serial.println(SOIL_MOISTURE_0);
if (SOIL_MOISTURE_0 < 500) {
digitalWrite(RELAY_0, LOW); //sets the relay to cut the power to the track
delay(3000);
}
if (SOIL_MOISTURE_0 > 500) {
//digitalWrite(RELAY_0, HIGH); //keeps the power on while the sensor is not tripped
digitalWrite(RELAY_0, LOW); //keeps the power on while the sensor is not tripped
delay(3000);
}
delay(86400000UL);
Serial.println("delay done.");
}
In my Plant_zero function I am away of the if and else return the same outcome just for test-purposes.
but the pump is always on I dont get it Please help me out
I also went back to the basic tested following code:
// constants won't change
const int RELAY_PIN = 3; // the Arduino pin, which connects to the IN pin of relay
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin as an output.
pinMode(RELAY_PIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(RELAY_PIN, HIGH);
delay(500);
digitalWrite(RELAY_PIN, LOW);
delay(500);
}
that code works fine with the delay so it seems like everything is connected corretly and responding well.
Thanks for all replies:
Is this a ideal way of it?
I want following:
Keep the relay off per default.
Read soil moisture wait some time for the sensor to calculate complete.
Turn on the relay if threshold is over value 500.
Have the relay on for 3 seconds.
Wait 24 hours for next run.
int sensor = A0; //Sets pin A0 as our sensor input.
int RELAY_0 = 3; //sets digital pin 2 as our relay control.
unsigned long startBlink = 0UL; // because millis() returns unsigned long
unsigned long waitBlink = 0UL; // because millis() returns unsigned long
void setup() {
Serial.begin(9600); //starts up the serial monitor.
pinMode(RELAY_0, OUTPUT);
digitalWrite(RELAY_0, HIGH);
}
void loop() {
Serial.println("Turn off relay");
digitalWrite(RELAY_0, HIGH);
Plant_zero();
}
void Plant_zero() {
digitalWrite(RELAY_0, HIGH);
int SOIL_MOISTURE_0 = analogRead(sensor); //Sets valA1 as whatever the sensor reads
delay(5000);
Serial.println("read sensor wait..");
Serial.println(SOIL_MOISTURE_0);
delay(10000);
Serial.println("turn on pump");
if (SOIL_MOISTURE_0 > 500) {
digitalWrite(RELAY_0, LOW); //sets the relay to cut the power to the track
Serial.println("delay 3 sec");
delay(3000);
}
Serial.println("Tun on Relay");
digitalWrite(RELAY_0, HIGH);
delay(2000);
Serial.println("Tun off Relay");
digitalWrite(RELAY_0, HIGH);
delay(86400000UL);
}
#define LEDon HIGH
#define LEDoff LOW
#define pumpON LOW
#define pumpOFF HIGH
const byte sensor = A0;
const byte heartbeatLED = 13;
const byte RELAY_0 = 3;
int SOIL_MOISTURE_0;
//timing stuff
unsigned long heartbeatTime;
unsigned long machineTime;
unsigned long commonTime;
unsigned long commonInterval;
//const unsigned long pumpOnTime = 3000ul; //3 seconds
//const unsigned long pumpRestartTime = 24ul * 60 * 60 * 1000; //24 hours
//for testing
const unsigned long pumpOnTime = 5000ul; //5 seconds
const unsigned long pumpRestartTime = 10000; //10 seconds
//State Machine stuff
enum MachineStates {Startup, State0, State1, State2};
MachineStates mState = Startup;
// s e t u p ( )
//********************************************^************************************************
void setup()
{
Serial.begin(9600); //starts up the serial monitor.
digitalWrite(heartbeatLED, LEDoff);
pinMode(heartbeatLED, OUTPUT);
digitalWrite(RELAY_0, HIGH);
pinMode(RELAY_0, OUTPUT);
} //END of setup()
// l o o p ( )
//********************************************^************************************************
void loop()
{
//********************************************** h e a r t b e a t T I M E R
//is it time to toggle the heartbeatLED ?
if (millis() - heartbeatTime > 500)
{
//restart this TIMER
heartbeatTime = millis();
//toggle LED
digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
}
//********************************************** m a c h i n e T I M E R
//is it time to service the State Machine ?
if (millis() - machineTime > 1000ul)
{
//restart this TIMER
machineTime = millis();
checkMachine();
}
} //END of loop()
// c h e c k M a c h i n e( )
//********************************************^************************************************
void checkMachine()
{
switch (mState)
{
//******************
case Startup:
{
//next state
mState = State0;
}
break;
//******************
case State0:
{
SOIL_MOISTURE_0 = analogRead(sensor);
Serial.print("Soil Moisture = ");
Serial.println(SOIL_MOISTURE_0);
//is the soil dry ?
if (SOIL_MOISTURE_0 > 500)
{
digitalWrite(RELAY_0, pumpON);
Serial.println("\nTurning ON the pump");
Serial.println("Waiting for 3 sec");
//set the wait time
commonInterval = pumpOnTime;
//restart the common TIMER
commonTime = millis();
//next state
mState = State1;
}
}
break;
//******************
case State1:
{
//has the common TIMER expired ?
if (millis() - commonTime >= commonInterval)
{
digitalWrite(RELAY_0, pumpOFF);
Serial.println("\nTurning OFF the pump");
Serial.println("Waiting for 24 hours\n");
//set the wait time
commonInterval = pumpRestartTime;
//restart the common TIMER
commonTime = millis();
//next state
mState = State2;
}
}
break;
//******************
case State2:
{
//has the common TIMER expired ?
if (millis() - commonTime >= commonInterval)
{
//we will now restart the cycle
//next state
mState = State0;
}
break;
}
} //END of switch()
} //END of checkMachine()
//********************************************^************************************************
If that grey box is the pump, it is permanently connected to the batteries, so the relay cannot switch it on/off.
What will happen is that when the relay is activated, it will short-circuit the battery and drain it very quickly. This may also make the batteries and wires hot. It will stop the pump from running, but not in a good way.
The relay should be used to interrupt the current to the motor, not short circuit the motor/battery. That's how your light switch in your house works. It doesn't short circuit the grid, it opens the circuit to the light.