Hi, I've made this circuit for watering my plant automatically, and it is working fine. I am a total beginner to electronics and don't really know if I'm just lucky but after reading some articles it seems like most "professional" circuits use some capacitors or diodes, while I have not. Does anyone know how I could improve my circuit?
Also, the relay is a 5V relay and the DC motor is a 5V waterpump. I don't quite understand why the relay turns on when it gets a ground input. It didn't work with a 5V input (from the transistor that is).
Finally, I want to add a way to reset the program so that the volume variable resets when i refill the watertank. I imagine this to be quite tricky since I cannot interact with the code or send any usable inputs while delay is running. Could i work around it using millis or will I just have to be grateful for the reset button?
Link to relay: https://www.amazon.se/DollaTek-KY-019-kanal-kortskydd-arduino/dp/B07DJ4ZD99/ref=sr_1_10?adgrpid=111850495843&dib=eyJ2IjoiMSJ9.523oRgLhG1qp_xsGAbPhluknKUbNI5U6hiQkpZ1ADYJdPq5F4qVi56Z1N73aRehQ1m-dGbP3CCh9bB3yHuyLBxFdqz6es4ZsJh0k3md4DCMR7JElzffVudbryyviez4JH2MrXDWoDsJKJvISI5AFTQfCaafUBwrt0ojdxIHTYv2dADmpdcKGq_kUjLCpHUeO5mEYRiZiQ84if3L5nBuMmplNLewzPfOcGP6UatNhhvwxcKSIysAg_3UuWvz_KKERW-9tufMwDo3dU4J5K62fJaM22amFK8Xs0tTgqnFNoM4.37SKaPxWueXuOPgRA-o4uZkmX7AclB_0URbOCf4bweE&dib_tag=se&hvadid=678397857751&hvdev=c&hvlocphy=9219639&hvnetw=g&hvqmt=e&hvrand=13882957924970332398&hvtargid=kwd-1188847092658&hydadcr=28121_2377794&keywords=arduino+relay+amazon&qid=1733768155&sr=8-10
Thanks.
#include <Arduino.h>
//DEFINE
int moistPin = A0;
int relayPin = D6;
int pumpInterval = 2000; // In milliseconds
int checkMoistInterval = 3; // In hours
float volume = 1000; // In milliliters
float pumpVolume = pumpInterval * 0.038;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
}
void loop() {
float moistValue = analogRead(moistPin);
Serial.println(moistValue);
Serial.println(volume);
if ((moistValue > 550) && (volume > pumpVolume)){
//Remove from total volume
volume -= pumpVolume;
//Turn on relay
digitalWrite(relayPin, HIGH);
delay(pumpInterval);
//Turn off relay
digitalWrite(relayPin, LOW);
}
delay(checkMoistInterval * 1000 * 60 * 60);
}
Most likely the math goes wrong in the last delay. Use unsigned long numbers.
LarryD
December 9, 2024, 6:21pm
4
As mentioned, unsigned long will be needed.
delay(checkMoistInterval * 1000ul * 60 * 60);
Thanks, what happens then it isn't used?
J-M-L
December 9, 2024, 6:25pm
7
do you mean your really is active when it gets a LOW signal ?
that's quite frequent (and has some technical rationale)
It does activate on a low signal, so ground counts as a LOW signal then I guess?
So I guess the ground provides a LOW signal then?
J-M-L
December 9, 2024, 6:30pm
11
YES when you set a pin to LOW, its voltage level is close to GND Level.
LarryD
December 9, 2024, 6:41pm
13
In electronics, HIGH/LOW don’t mean too much.
In some instances a HIGH is needed to make something to work and in others a LOW .
BTW
Use a macro in your sketches to help make things more understandable.
#define RELAYon LOW
#define RELAYoff HIGH
. . .
digitalWrite(relayPin, RELAYon);
delay(pumpInterval);
digitalWrite(relayPin, RELAYoff);
Thanks a lot! Do you know if there is any way of "resetting" the volume variable while a delay is active?
J-M-L
December 9, 2024, 10:05pm
16
potato82:
while a delay is active
Your arduino is busy waiting for the end of the delay so can’t do anything else. We say that delay() is blocking.
If you need to write code that does not block the loop, look at
system
Closed
June 7, 2025, 10:05pm
17
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.