Why does my relay activate with ground?

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);
}
  • Always show us good images of your ‘actual’ wiring.
    Show us your relay. Give links to components.

Most likely the math goes wrong in the last delay. Use unsigned long numbers.

  • As mentioned, unsigned long will be needed.

delay(checkMoistInterval * 1000ul * 60 * 60);

  • There is hardware here that is not in your diagram.

  • Those relays are usually LOW to operate.

Thanks, what happens then it isn't used?

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?

  • The math used by the compiler is integer math.
    Use ul after a number to tell the compiler to use unsigned math.

So I guess the ground provides a LOW signal then?

YES when you set a pin to LOW, its voltage level is close to GND Level.

  • Your relay module uses components in a way that needs a LOW to energize the relay’s coil.

  • There are some modules that are made differently and requires a HIGH to energize the coil.

  • 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?

  • You can change volume prior to or just after delay( ).

  • Try to avoid using delay( . . . ) in your sketches.

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

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