1x Uno,
1x ds3231RTC,
1x I2c Lcd display
1x Dh22
1x water temp sensor
1x 240v-5v 3.5amp Psu
1x 5v 10amp relay this one is causing trouble
1x 5v 30amp relay
#include <RTClib.h> // Real time clock
#include <DHT.h> // Air Temp Humid Sensor
#include <OneWire.h> // Sensors (water temp) connected to one wire
#include <DallasTemperature.h> // Water Temp Sensor
#include <LiquidCrystal_I2C.h> // Lcd display
const int lightingPin = 4;
const int bulblightingPin = 9;
const int pumpPin = 6;
RTC_DS3231 rtc; // Init the DS3231 using the hardware interface (Nano A4=SDA A5=SCL)
unsigned long currentTime;
unsigned long previousTime = 0;
int marker = 0;
int lampState =0;
int pumpState =0;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Lcd Display ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
// set the LCD address to 0x3F for a 16 chars 2 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Air Temperature & Humidity sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Water Temperature sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
//########################################################################## SETUP ###################################################################################################
void setup()
{
Serial.begin(9600);
rtc.begin(); // Initialize the rtc object
lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines
lcd.backlight(); // Turns backlight LCD on
sensors.begin(); // water temp sensor
dht.begin(); // temp humid sensor
pinMode (lightingPin, OUTPUT);
digitalWrite (lightingPin, HIGH);
pinMode (bulblightingPin, OUTPUT);
digitalWrite (bulblightingPin, HIGH);
pinMode (pumpPin, OUTPUT);
digitalWrite (pumpPin, HIGH);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Uncomment to set the date and time ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Automatic RTC DS3231 time update from computer clock while compiling
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); // This line sets the RTC with an explicit date & time for example to set January 21, 2014 at 3am
}
//########################################################################## LOOP ###################################################################################################
void loop()
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OUTPUT REAL TIME CLOCK ON SERIAL ~~~~~~~~~~~~~~~~~~~~~~~~~~
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ lighting RELAY ACTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lampState = (now.hour()*60) + now.minute(); // Hour times it by 60 add the minutes, 18 hour cycle 5am = 300 11pm = 1380
// 12 hour cycle 6am = 360 12am = 00
// 14 hour cycle 6am = 360 2am = 1200
if (lampState >= 749 && lampState < 751){
digitalWrite (lightingPin, HIGH);
digitalWrite (bulblightingPin, LOW);
Serial.println("Lights ON");
}
else {
digitalWrite (lightingPin, LOW);
digitalWrite (bulblightingPin, HIGH);
Serial.println("Lights OFF");
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Water Pump Relay Times ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// pumpState = (now.hour()*60) + now.minute(); // Hour times it by 60 add the minutes Pump run for 1 minute @10pm = 1320
// if (pumpState >= 1200 && pumpState < 1201){
// digitalWrite (pumpPin, LOW);
// Serial.println("Water Pump ON");
// }
// else {
// digitalWrite (pumpPin, HIGH);
// Serial.println("Water Pump OFF");
// }
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Air and Humid Sensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The system is to mainly to control lighting. Also give info on water, air and humidity data via the i2c display
The lighting relays are controlled from via a time interval, from the RTC.
When i first connect the computer to the uno and upload the sketch the relays both switch off as the time interval has not yet come round all good. The serial monitor tells me that the lights are off (correct) and the relays are off (correct) as the time interval has not been reached. If the time interval is reached the relays both activate as expected turn on then off depending on the interval set.
However if i disconnect the usb cable from the uno the 5v10amp relay turns on and stays on! this relay needs the pin to be high to turn it off and low to turn it on. I have hooked an oscilloscope up to pin9 and indeed it is high, however the relay is activated (on) when with the channel High it should be off.
I noticed that the voltage on pin 9 had a ripple so i put a 420mf capacitor across the psu and it smoothed out quite well, I then put a pull up 20k resistor across the signal and 5v, but still the relay remains on.
any ideas would be greatly apreciated
https://www.google.co.uk/search?q=arduino+5v+10ampo+relAY&tbm=isch&source=iu&ictx=1&fir=89QG9J5ULuNElM%253A%252CW3Vj-LKh19Bc_M%252C_&usg=__2FUyiKHROQEvofSyKEX6hjcgxdM%3D&sa=X&ved=0ahUKEwixqui725vaAhWBBsAKHdPXBAcQ9QEIQzAG#imgrc=oYHSYLtJw6YKDM: