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);
}