Test relay issue

Hi to everybody,

I'm working with an Arduino MKR WiFi 1010 board and I'm trying to do a simple test with a Relay (fl-3ff-s-z). The code, that I attach below, just exploits the Arduino Cloud to change the status (ON or OFF) of a Relay connected to a Waterpump. I connected the Relay in the NO configuration because I want to keep the pump off until I don't change its "status" from the Cloud. Everything works if the Arduino is connected to power (e.g. USB) but when I disconnect the Arduino from the power the pump takes current from the battery and it stays turned on continuously (as if I connected the two pins of the waterpump directly to the battery poles). I think there is a bug in the circuit. I attach also a sketch of my circuit.
Could someone help me?
Thanks a lot in advance!!

/* 
  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  bool pump_state;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
const int relay = A5;

void setup() {
  Serial.begin(9600);
  
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
  pinMode(relay, OUTPUT);
}

void loop() {
  ArduinoCloud.update();
  
  // Your code here 
  Serial.print("Pump state: ");
  Serial.println(pump_state);
  
  delay(1000);
 
}

/*
  Since PumpState is READ_WRITE variable, onPumpStateChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPumpStateChange()  {
  
  if (pump_state == false) {
    digitalWrite(relay, LOW);
  }
  else {
    digitalWrite(relay, HIGH);
  }
  
}

So what is providing current to the relay coil when the power is off? Have you checked the relay contacts to see that they are Normally Open (NO) when the relay module is not connected to power?

Test with a LED on pin A5 to see if the relay would turn on or off.

Are you certain that is what you did?


You have labelled the connection as NO but the symbols on the image of the relay module indicate you used the NC connection.

If this is the case, the water pump would be on when you want it to be off, and vice verca.

...unless... the relay input is active low and @lorenzoscavarda does not understand that.

Relay modules with opto-isolators often have active low inputs. But I have to admit that the image posted does not look like an opto-isolated relay module.

SOLVED!
Thanks for the answers. You were right, I connected the relay in the NC mode (I made a mistake due to the chinese labels on my relay). However, I had to invert also the "HIGH" and "LOW" in the digitalWrite functions in the code. I see that in the NO configuration "HIGH" means circuit opened (no current) and "LOW" circuit closed (current). Am I right? I had also to add an initialiasation in the setup() function. I attached here the final code.

So there is only a misleading thing related to the fact that if the status of the pump is "ON" it means circuit "OPEN" (no current) and if the pump is "OFF" it means circuit "CLOSED" (current).

/* 
  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  bool pump_state;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
const int relay = A5;

void setup() {
  Serial.begin(9600);
  
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
}

void loop() {
  ArduinoCloud.update();
  
  Serial.print("Pump state:");
  Serial.println(pump_state);

  delay(1000);
  
}

/*
  Since PumpState is READ_WRITE variable, onPumpStateChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPumpStateChange()  {
  
  if (pump_state == false) {
    digitalWrite(relay, HIGH);
  }
  else {
    digitalWrite(relay, LOW);
  }
  
}

I don't understand you. When a pump is 'on' that normally implies that the circuit is closed and current is flowing through the pump and the pump is then pumping. When 'off', the opposite. If you are getting the opposite results then your code is inverted and you need to change HIGH to LOW and vice versa. What a HIGH signal at the arduino pin does with regards to your pump is entirely down to the hardware attached to the pin. However, it is easy to just test and see and then invert the code as required.

Yes. You are right. Indeed as you can see from the code attached is that I have inverted the LOW and HIGH signal.