Hello, I am using a sketch that I have modified to fit my needs. I am building a arduino fan controller using a 5v relay. I have a DHT22 sensor with the information displaying on an I2C 16x2. The code allows the fan to come on at or above the max temp, and shuts off at the min temp. I have put in an LED to indicate when the fan (relay) is on/off (located at bottom of sketch), but the "on/off" doesn't work correctly in this section either. My issue is displaying the on/off on the I2C. I'm wondering if there is a simple code that will change the display by simply using the state of the relay. If you run the current sketch, fan will show on when at or above max temp; the display is blank in between min and max temp; then displays off when at or below min temp. Thanx for the info and please keep it a simple as possible for me to understand.
This is the beginning of my project. I am looking to build a complete arduino controlled grow room with display. It will operate my fan, lights, pumps, etc.
#include <LiquidCrystal_I2C.h> //initialize LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 12 // what pin we're connected to
int ledPin = 3; //LED pin
int relayPIN = 9; //5v Relay
#define DHTTYPE DHT22 //enter DHT11 or DHT22
#define pwm 9 //define PIN 9 as pwm
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
//Tempurature Range
// Its good to provide a buffer for efficiency, if the temp bounces from 79-81 constantly
// it will turn on and off the fan constantly. So set a minimum temperature that the fan
// can cool the are too before it starts checking for max temperature.
float maxTemperature = 80;
float minTemperature = 75;
// always make sure to activate baklight
// set.cursor (0, 0 = top left / 15, 0 = top right / 0, 1 = bottom left / 15, 1 = bottom right)
void setup() {
lcd.begin(16,2);
lcd.backlight();
lcd.clear();
lcd.print(" Fan Speed ");
lcd.setCursor(0, 1);
lcd.print(" Controller ");
delay(2000);
lcd.clear();
analogWrite(pwm, 255);
lcd.clear();
Serial.begin(9600);
dht.begin(); //start DHT sensor
// Pin Setup/Register
pinMode(relayPIN, OUTPUT);
pinMode(3, OUTPUT);
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
int h = dht.readHumidity(); //int (2 digit display (ex. 74F)), float (4 digit display (ex. 74.65F))
float t = dht.readTemperature(true); // Read temperature as Celsius for Farenhieght type true in ()
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("temperature: ");
Serial.print(t);
Serial.print(" *F ");
lcd.clear();
lcd.setCursor(0, 0); //set cursor location to start ()
lcd.print("Temp:");
lcd.print(t); // Printing temperature on LCD
lcd.print("F FAN");
lcd.setCursor(0, 1);
lcd.print("Humidity:");
lcd.print(h); // Printing humidity on LCD
lcd.print("%");
lcd.setCursor(13,1);
// put your main code here, to run repeatedly:
if(t >= maxTemperature)
{ lcd.print(" ON");
powerOnRelay();
delay(100); }
else if (t <= minTemperature)
{ lcd.print("OFF");
powerOffRelay();
delay(100); }
delay(1000); // Check tempertature every second
}
void powerOnRelay() {
digitalWrite(relayPIN, HIGH);
Serial.println("Relay On");
digitalWrite(3, HIGH); //activate LED when relay on
}
void powerOffRelay() {
digitalWrite(relayPIN, LOW);
Serial.println("Relay Off");
digitalWrite(3, LOW); //activate LED when relay off
delay(1000);
}