-
1Step 1
Rough Code so far... Was able to get it to verify in the arduino IDE. I had a photo sensor tutorial in my arduino starter kit and was able to piece together part of the circuit. Right now i have another test sketch which turns on an LED to indicate day or night as proof of concept.
I also added in some logic to check if the relay is already on before switching it on, and to make sure it is on before switching it off so i am not constantly looping and telling it to shut off when already off. Not sure if this is necessary but i wanted to be on the safe side so i don't burn anything out.
Will probably be a week or so before i get the rest of the parts i need (relays and temp/humidity sensor).
Questions / Need help with:
- Should i totally isolate the relays from the arduino (by feeding them a separate power source) as described below?
NOTES: If you want complete optical isolation, connect "Vcc" to Arduino +5 volts but do NOT connect Arduino Ground. Remove the Vcc to JD-Vcc jumper. Connect a separate +5 supply to "JD-Vcc" and board Gnd. This will supply power to the transistor drivers and relay coils.
Work In Progress Mockup:
CODE:
/*-----( Import needed libraries )-----*/ #include "DHT.h" /*-----( Declare Constants )-----*/ #define RELAY_ON 0 #define RELAY_OFF 1 /*-----( Declare Variables )-----*/ #define DHTPIN 2 // what pin we're connected to for DHT22 #define Relay_1 3 // Arduino Digital I/O pin number for first outlet #define Relay_2 4 // Arduino Digital I/O pin number for second outlet #define DHTTYPE DHT22 // DHT 22 (AM2302) DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino // Connect pin 1 (on the left) of the sensor to +5V // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 // to 3.3V instead of 5V! // Connect pin 2 of the sensor to whatever your DHTPIN is // Connect pin 4 (on the right) of the sensor to GROUND // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor int lightPin = 0; //the analog pin the photoresistor is connected to //the photoresistor is not calibrated to any units so //this is simply a raw sensor value (relative light) int ledPin = 9; //Night Indicator LED int ledPin2 = 10; //Day Indicator LED int threshold = 700; //Photosensor value for Night time int lightVal = 0; //Variable to hold reading from photosensor boolean nightInd = false; //True/False to indicate if day or night. True = Night time //HUMIDITY THRESHOLDS float dayRHthresholdUpper = 60.00; //DAY RH High threshold when exhaust is turned on float dayRHthresholdLower = 40.00; //DAY RH Low threshold when exhaust is turned off float nightRHthresholdUpper = 60.00; //NIGHT RH High threshold when exhaust is turned on float nightRHthresholdLower = 40.00; //NIGHT RH Low threshold when exhaust is turned off //TEMP THRESHOLDS float dayTempThresholdUpper = 78.00; //DAY Temp High threshold when heat is turned off float dayTempThresholdLower = 75.00; //DAY Temp Low threshold when heat is turned on float nightTempThresholdUpper = 70.00; //NIGHT Temp High threshold when heat is turned off float nightTempThresholdLower = 66.00; //NIGHT Temp Low threshold when heat is turned on void setup() /****** SETUP: RUNS ONCE ******/ { //-------( Initialize Pins so relays are inactive at reset)---- digitalWrite(Relay_1, RELAY_OFF); digitalWrite(Relay_2, RELAY_OFF); //---( THEN set pins as outputs )---- pinMode(Relay_1, OUTPUT); //Humidity outlet (exhaust fan - do not exceed 1200watts) pinMode(Relay_2, OUTPUT); //Heat outlet (heater - do not exceed 1200watts) delay(4000); //Check that all relays are inactive at Reset pinMode(ledPin, OUTPUT); //Night Indicator LED pinMode(ledPin2, OUTPUT); //Day Indicator LED Serial.begin(9600); //start up serial output dht.begin(); //assuming this turns on the DHT22 sensor }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { lightVal = analogRead(lightPin); //Read photosensor value delay(2000); // Wait a few seconds between measurements. DHT22 is slow. // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read Humidity float t = dht.readTemperature(); // Read temperature as Celsius float f = dht.readTemperature(true); // Read temperature as Fahrenheit // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } if(analogRead(lightPin) > threshold){ //This is NIGHT TIME digitalWrite(ledPin, HIGH); //Turn night indicator LED ON. digitalWrite(ledPin2, LOW); //Turn day indicator LED OFF. nightInd = true; }else{ //This is DAY TIME digitalWrite(ledPin2, HIGH); //Turn day indicator LED ON. digitalWrite(ledPin, LOW); //Turn night indicator LED OFF. nightInd = false; } //------DAY TIME------// if (nightInd == false){ //nightInd = false so this means it is daytime // If humidity hits threshold, turn on exhaust (outlet #1) if (h > dayRHthresholdUpper) { if (Relay_1 == 1){ //Make sure relay is not already on before switching digitalWrite(Relay_1, RELAY_ON);// set the Relay ON delay(1000); // wait for a second } } // If humidity hits threshold, turn off exhaust (outlet #1) if (h < dayRHthresholdLower) { if (Relay_1 == 0){ //Make sure relay is on before switching off, else do nothing since its already off digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF delay(1000); // wait for a second } } // If temperature hits threshold, turn heat on. (outlet #2) if (f < dayTempThresholdLower) { if (Relay_2 == 1){ //Make sure relay is not already on before switching digitalWrite(Relay_2, RELAY_ON);// set the Relay ON delay(1000); // wait for a second } } // If temperature hits threshold, turn heat off. (outlet #2) if (f > dayTempThresholdUpper) { if (Relay_1 == 0){ //Make sure relay is on before switching off, else do nothing since its already off digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF delay(1000); // wait for a second } } }//------END DAY TIME-----// //------NIGHT TIME------// if (nightInd == true){ // If humidity hits threshold, turn on exhaust (outlet #1) if (h > nightRHthresholdUpper) { if (Relay_1 == 1){ //Make sure relay is not already on before switching digitalWrite(Relay_1, RELAY_ON);// set the Relay ON delay(1000); // wait for a second } } // If humidity hits threshold, turn off exhaust (outlet #1) if (h < nightRHthresholdLower) { if (Relay_1 == 0){ //Make sure relay is on before switching off, else do nothing since its already off digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF delay(1000); // wait for a second } } // If temperature hits threshold, turn heat on. (outlet #2) if (f < nightTempThresholdLower) { if (Relay_2 == 1){ //Make sure relay is not already on before switching digitalWrite(Relay_2, RELAY_ON);// set the Relay ON delay(1000); // wait for a second } } // If temperature hits threshold, turn heat off. (outlet #2) if (f > nightTempThresholdUpper) { if (Relay_2 == 0){ //Make sure relay is on before switching off, else do nothing since its already off digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF delay(1000); // wait for a second } } }//------END NIGHT TIME-----// float hi = dht.computeHeatIndex(f, h); // Compute heat index, might be useful later... //Must send in temp in Fahrenheit! //DEBUG Outputs Serial.println(lightVal); //Photosensor value Serial.println(nightInd); // True = Night | False = Day Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t"); Serial.print("Heat index: "); Serial.print(hi); Serial.println(" *F"); }//--(end main loop )---
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.