Relay, water pump, power down problem

Hello,

I am trying to build an automatic watering system were I would only check the humidity a few times during the day. For the rest of the time I want to have my arduino in sleep mode to save the battery.

My setup includes arduino nano, RTC, humidity sensor, relay and water pump. Arduino is powered by USB at the moment and pump with 9V DC.

The problem is that whenever I attach the pump to the relay after first operation of the pump arduino will remain in sleep mode and never wake up. This won't happen if there won't be anything attached to the relay.

Anyone have any idea what is wrong here?

Below a photo of my setup and the code.

#include <DS3232RTC.h>

#include <TimeLib.h>

#include <Wire.h>

#include <LowPower.h>


int relay_pin = 10;

int sensor_pin = A0;

const int AirValue = 586; //you need to replace this value with Value_1

const int WaterValue = 310; //you need to replace this value with Value_2

const int dry = 65;

int soilMoistureValue = 0;

// RTC

tmElements_t tm;

const byte rtcAlarmPin = 3; // External interrupt on pin D3


void setup()

{

Serial.begin(9600); // open serial port, set the baud rate to 9600 bps

pinMode(relay_pin, OUTPUT); //output for relay

// Setup RTC

pinMode(rtcAlarmPin, INPUT_PULLUP); // Set interrupt pin

RTC.squareWave(SQWAVE_NONE); // Disable the default square wave of the SQW pin

// setAlarm Syntax (RTC.setAlarm(alarmType, seconds, minutes, hours, dayOrDate);)

RTC.setAlarm(ALM1_MATCH_SECONDS, 0, 0, 0, 0 );

RTC.alarm(ALARM_1);

RTC.alarmInterrupt(ALARM_1, true); // Enable alarm 1 interrupt A1IE

}


void loop()

{

// Read temperature

int t = RTC.temperature();

float celsius = t / 4.0;

Serial.println(celsius);

// Read time

RTC.read(tm);

char dateBuffer[19];

sprintf(dateBuffer, "%04u/%02u/%02u,%02u:%02u:%02u", (tm.Year + 1970), tm.Month, tm.Day, tm.Hour, tm.Minute, tm.Second);

Serial.println(dateBuffer);

delay(500);

// Enter sleep

sleepNow();

Serial.println("Awake...");

// Wake up

if (RTC.alarm(ALARM_1))

{

RTC.read(tm);

RTC.setAlarm(ALM1_MATCH_SECONDS, (tm.Second + 20) % 60, 0, 0, 0);

delay(500);

sensor();

}

delay(500);

}


void sensor()

{

soilMoistureValue = analogRead(sensor_pin); //put Sensor insert into soil

soilMoistureValue = map(soilMoistureValue, AirValue, WaterValue, 0, 100);

Serial.print("Moisture: ");

Serial.print(soilMoistureValue);

Serial.println("%");

delay(1000);

if(soilMoistureValue < dry)

{

digitalWrite(relay_pin, LOW);

delay(5000);

digitalWrite(relay_pin, HIGH);

delay(1000);

}

else

{

digitalWrite(relay_pin, HIGH);

}

}


void wake ()

{

detachInterrupt (digitalPinToInterrupt (3)); // Disable interrupts on D3

}

void sleepNow ()

{

Serial.println("Entering sleep");

delay(100);

noInterrupts (); // Disable interrupts before entering sleep

attachInterrupt (digitalPinToInterrupt(3), wake, FALLING); // Wake on falling edge of D3

interrupts (); // Enable interrupts to ensure next instruction is executed

LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);

}

Schematic?

Hope it's clear

You simplified the relay board, those typically have an optocoupled interface.

SCL & SDA need pullup resistors to 5V, 4.7K is usually used.

CrossRoads:
You simplified the relay board, those typically have an optocoupled interface.

SCL & SDA need pullup resistors to 5V, 4.7K is usually used.

Yes, fair point. Still they are part of the modules so I didn't find it crucial to include them in the schematics.

Code looks wrong , or at least confusing to me - have a look at examples of using interrupts , you don’t need to attach and detach interrupts everywhere - get rid ?? and attach interrupt( ...., etc) is normally in void setup () and only setup once .

You look to be disabling interrupts when you wake up ( why?) .I’ve not followed it through but maybe you then enter sleep with interrupts still disabled ?

hammy:
Code looks wrong , or at least confusing to me - have a look at examples of using interrupts , you don’t need to attach and detach interrupts everywhere - get rid ?? and attach interrupt( ...., etc) is normally in void setup () and only setup once .

You look to be disabling interrupts when you wake up ( why?) .I’ve not followed it through but maybe you then enter sleep with interrupts still disabled ?

I've found somewhere that it is a good thing to do. I've tested the code without detaching and attaching interrupts and it didn't change anything. Anyway, I will get rid of these steps.

As I mentioned in the first post the code works fine without pump attached. Problem starts after first operation of the pump.

Then it could be noise created by the pump or a power supply issue . Where is the 5v supply from that powers the Arduino and is the 9v battery of suitable size ( ie not a little smoke alarm battery ).

hammy:
Then it could be noise created by the pump or a power supply issue . Where is the 5v supply from that powers the Arduino and is the 9v battery of suitable size ( ie not a little smoke alarm battery ).

At the moment I am powering Arduino from the USB connected to my laptop.

Pump is powered from 9V DC adapter. I have also tried different 12V adapter with the same result.

Hi,
Those motors are in my experience very very noisy electrically.
Have you tried a diode and capacitor across the motor terminals, that is right at the terminals of the motor?


Tom.. :slight_smile:

TomGeorge:
Hi,
Those motors are in my experience very very noisy electrically.
Have you tried a diode and capacitor across the motor terminals, that is right at the terminals of the motor?


Tom.. :slight_smile:

Cheers Tom. I have tried diode but without a capacitor. Will try both and post the result.

TomGeorge:
Hi,
Those motors are in my experience very very noisy electrically.
Have you tried a diode and capacitor across the motor terminals, that is right at the terminals of the motor?


Tom.. :slight_smile:

Adding diode and capacitor improved things slightly. The sleep-wake up-turn on pump cycle will work from 3-4 times. After that Arduino will fall into an infinite sleep.

IF the environment you are testing in includes fluorescent lights, go somewhere to test. The lights are probably the cause of your problem.

Paul

Paul_KD7HB:
IF the environment you are testing in includes fluorescent lights, go somewhere to test. The lights are probably the cause of your problem.

Paul

I am not using fluorescent lights. It is a water pump.

mksz:
I am not using fluorescent lights. It is a water pump.

I know that but I have developed projects that would not work properly because there was a florescent light over the bench. Turned off and all worked properly.

Big magnetic flux from the light and ballast.

Paul

Paul_KD7HB:
I know that but I have developed projects that would not work properly because there was a florescent light over the bench. Turned off and all worked properly.

Big magnetic flux from the light and ballast.

Paul

Oh, that is interesting. No, there is no fluorescent light in my flat. I checked different pump with the same result. Seems I will need to try a different code. The power down is a problematic step.