Hello All! Thanks for reviewing this project. First time posting though I have been digging through the posts for answers. My head is sore from scratching. I am new to programming. It is tedious and I have made a lot of mistakes. I really would appreciate some help getting these relays operate correctly. Please let me know if I have left out any pertinent information.
Board: ELEGOO Uno R3
The project uses (2) DS18B20 sensors to measure soil temperature and the temperature of water in a sump. When the soil temperature falls below a certain temperature a relay is supposed to activate a circulation motor/pump to warm the plant roots. The second DS18B20 sensor monitors the temperature of water in the sump to keep it warm enough to warm the plant roots. When the water temp falls below a certain temperature a relay should activate a 12V dc heater.
The project also utilizes a soil resistivity probe to measure the water content of the soil. When the soil dries out the probe should activate a relay to irrigate the plant.
This project also utilizes a DS3231 real time clock to activate 12V leds in the optimum spectrum for plant growth. The program should cycle a relay to turn on and off the plant light at a time that can be set using Arduino software. I have not been able to get the RTC to cycle the relay correctly.
No LCD, no push buttons. A computer interface will be used to set all parameters.
This is the wiring schematic used. If the relay output pins do not correctly correspond please disregard that part of the diagram.
In order to better manage my errors I have divided the code into two parts as shown below. The temp sensors are reading and displaying temperature. The RTC shows the correct time. I think most of my errors may be in the if loop sequences.
Regarding the DS18B20 and Relays code I think maybe I left something important out required to use the alarms. I also suspect that referencing sensor1, sensor2 in the loops may be problematic.
Code:
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // Data wire is connected to GPIO2
const int relayPin1 = 8; //Coordinates relay inputs to Arduino outputs
const int relayPin2 = 9; //Coordinates relay inputs to Arduino outputs
const int relayPin3 = 10; //Coordinates relay inputs to Arduino outputs
const int relayPin4 = 11; //Coordinates relay inputs to Arduino outputs
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
//DeviceAddress Temp1, Temp2;
uint8_t sensor1[8] = { 0x28, 0x3E, 0x80, 0xC1, 0x17, 0x20, 0x06, 0xB1 };
uint8_t sensor2[8] = { 0x28, 0x77, 0x92, 0xB5, 0x17, 0x20, 0x06, 0x0B };
void setup(void){
pinMode(relayPin1, OUTPUT);
//pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
//pinMode(relayPin4, OUTPUT);
Serial.println("Setting alarm temps...");
// alarm when temp is lower than 22C
sensors.setLowAlarmTemp(sensor1, 22);
// alarn when temp is lower than 26C
sensors.setLowAlarmTemp(sensor2, 26);
// attach alarm handler
Serial.begin(9600);
sensors.begin();
}
void loop(void){
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
Serial.print(" Sensor 1(*F): ");
Serial.println(sensors.getTempF(sensor1));
Serial.print(" Sensor 2(*F): ");
Serial.println(sensors.getTempF(sensor2));
delay(2000);
//Temp1A = sensors.getLowAlarmTemp(sensor1);
// Temp2A = sensors.getLowAlarmTemp(sensor2);
//checkAlarm(Temp1);
if (sensors.hasAlarm(sensor1))
{
digitalWrite(relayPin1, HIGH);
Serial.print("Soil radiator is On");
}
// checkAlarm(Temp2);
// checkAlarm(Temp2);
if (sensors.hasAlarm(sensor2))
{
digitalWrite(relayPin3, HIGH);
Serial.print("Sump Heater is On");
}
}
Regarding the DS3231 RTC and Soil Hygrometer & Relay code, I suspect that the time period specified in the if loop may be the problem. I'm not really sure what is wrong with the soil resistivity code.
Code:
/********************************
name: 5V relay module, DHT 11 humidity and temperature sensor module, DS18B20 temperature module
function: This program shows how the DHT 11 temperature and humidity sensor and the DS18B20 temperature sensor turns on
devices or power sockets connected to a 5V relay module
********************************/
// First we include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include "RTClib.h" //RTClib by Adafruit Version 1.12.5
#define ONE_WIRE_BUS 2 // Data wire is plugged into pin 2 on the Arduino DS18B20
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const int relayPin1 = 8; //Coordinates relay inputs to Arduino outputs
const int relayPin2 = 9; //Coordinates relay inputs to Arduino outputs
const int relayPin3 = 10; //Coordinates relay inputs to Arduino outputs
const int relayPin4 = 11; //Coordinates relay inputs to Arduino outputs
int Hygrometer = A1;
int thresholdValue = 600;// you can adjust the wetness of the soil by changing the threshold value
uint8_t sensor1[8] = { 0x28, 0x3E, 0x80, 0xC1, 0x17, 0x20, 0x06, 0xB1 };
uint8_t sensor2[8] = { 0x28, 0x77, 0x92, 0xB5, 0x17, 0x20, 0x06, 0x0B };
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices -DS18B20
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
RTC_DS3231 rtc; //Initialize RTC module
void setup(){
Serial.begin(9600);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
pinMode(relayPin4, OUTPUT);
pinMode(Hygrometer, INPUT);
digitalWrite(relayPin2, LOW);
//Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
delay(3000);//Wait 3 seconds before accessing Sensor
sensors.begin();
delay(1000);
Wire.begin();
delay(1000);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// Comment out below lines once you set the date & time.
// Following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Following line sets the RTC with an explicit date & time
// for example to set January 27 2017 at 12:56 you would call:
// rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
}
}
void loop()
{
DateTime now = rtc.now();
Serial.println("Current Date & Time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
delay(1000);
if (now.hour() > 19 && now.hour() < 7) { //Turns the grow light off between 7PM and 7AM
digitalWrite (relayPin4, LOW);
Serial.println("Grow Light Off");
}
else {
digitalWrite (relayPin4, HIGH);
Serial.print("Grow Light On");
}
int sensorValue = analogRead(Hygrometer);// read the soil conductivity on analog pin A1:
Serial.print(sensorValue);
if(sensorValue < thresholdValue){
Serial.println(" - Doesn't need watering");
digitalWrite(relayPin2, LOW);
}
else {
Serial.println(" - Time to water your plant");
digitalWrite(relayPin2, HIGH);
}
delay(500);
}
Thanks again for taking a look. Any help is appreciated.