Hi
I came across this sketch while googling for information regarding the use of Arduino switching a fan on 24C then off @21C. I have played around with the parameters, and the sketch responds by stating on the serial monitor either Fan is ON or Fan is OFF, what i cannot figure out is this, when i download the sketch to my UNO,there is no problem "done uploading" confirmed at the bottom of the screen, the problem is, the relay does not physically change state ?? though the serial monitor states that it has, can anyone guide me please.
//libraries
#include "DHT.h"
#define RELAY_FAN_PIN 6 // Arduino pin connected to relay which connected to fan
#define DHTPIN 10 // Arduino pin connected to relay which connected to DHT sensor
#define DHTTYPE DHT22
const int TEMP_THRESHOLD_UPPER = 24; // upper threshold of temperature, change to your desire value
const int TEMP_THRESHOLD_LOWER = 21; // lower threshold of temperature, change to your desire value
DHT dht(DHTPIN, DHTTYPE);
float temperature;// temperature in Celsius
void setup()
{
Serial.begin(9600); // initialize serial
dht.begin(); // initialize the sensor
}
void loop()
{
// wait a few seconds between measurements.
delay(2000);
temperature = dht.readTemperature();; // read temperature in Celsius
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
if(temperature > TEMP_THRESHOLD_UPPER){
Serial.println("The fan is turned on");
digitalWrite(RELAY_FAN_PIN, HIGH); // turn on
} else if(temperature < TEMP_THRESHOLD_LOWER){
Serial.println("The fan is turned off");
digitalWrite(RELAY_FAN_PIN, LOW); // turn on
}
}
}
You are missing a pinMode call for your relay pin.
Hi.
I have added a pinMode to the void set up, can you confirm please.
//libraries
#include "DHT.h"
#define RELAY_FAN_PIN 6 // Arduino pin connected to relay which connected to fan
#define DHTPIN 10 // Arduino pin connected to relay which connected to DHT sensor
#define DHTTYPE DHT22
const int TEMP_THRESHOLD_UPPER = 20; // upper threshold of temperature, change to your desire value
const int TEMP_THRESHOLD_LOWER = 19; // lower threshold of temperature, change to your desire value
DHT dht(DHTPIN, DHTTYPE);
float temperature;// temperature in Celsius
void setup()
{
Serial.begin(9600); // initialize serial
dht.begin(); // initialize the sensor
pinMode(RELAY_FAN_PIN, OUTPUT);
digitalWrite(RELAY_FAN_PIN, HIGH);
}
void loop()
{
// wait a few seconds between measurements.
delay(2000);
temperature = dht.readTemperature();; // read temperature in Celsius
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
if(temperature > TEMP_THRESHOLD_UPPER){
Serial.println("The fan is turned on");
digitalWrite(RELAY_FAN_PIN, HIGH); // turn on
} else if(temperature < TEMP_THRESHOLD_LOWER){
Serial.println("The fan is turned off");
digitalWrite(RELAY_FAN_PIN, LOW); // turn on
}
}
}
Hi.
Here's my Janet & John schematic drawing, sorry i do not own a CAD or drawing package, i use some parts of MS office Powerpoint.
It's hard to tell from the image of the relay board, but from the wiring, it looks like you are indeed powering the relay coil from the Arduino, which as you surmised is a bad idea.
Do you have more details on the relay board?
I have had this working on a different sketch, a sketch that only switched the fan ON at say 25 degrees, the problem with that was the moment the temperature dropped to 24.9 degrees the Fan would turn ON again, this could happen several times a minute. That's when i searched the internet for answers and come across the above sketch, so i assume the relay is ok, as the wiring has not altered while using the above sketch.
Sorry the Relay Module is a SONGLE SRD 05VDC-SL-C
I think that's just the relay itself. What's important is what else is on the relay board. Mine has optoisolation, tiny power transistors (I think), snubber diodes and separate pins to provide power for the relay coil. I don't see any of that in your image.
Those extra components are what make the relay usable by an Arduino.
Hi wildbill
The image of a relay in my Jpeg is what you say, it's just an image, i have uploaded a jpeg of the actual Relay i am using, for your perusal.
May be stating the obvious, but have you checked the voltage of the terminals at the relay? It is receiving 5V? Does it does get a 5V high signal? Leastways that negates the sketch.
Check switching by connecting switch to gnd / 5V as required.
I have done several checks this morning, and i can confirm that pin 6 which my relay is connected to gets 4.98 vdc when called upon by UNO.
I have just tried the simple code below, strangely the RELAY goes HIGH for about 2 seconds, then goes LOW for a further two seconds.
int Relay = 6;
void setup()
{
pinMode(Relay, OUTPUT);
}
void loop()
{
digitalWrite(Relay, HIGH); // turn on relay
}
Put some serial output in there. I'll guess that you are pulling enough current that the Arduino resets.
I have just entered in void set up () serial.begin(9600); now the LED Pin stays LOW.??
Make it print something in setup so you can see if it is resetting.
I would still like to see some documentation for that relay board, but I suspect that you will need to give it a separate 5V power supply. Connect the grounds.
Hi All.
I have purchased and installed a SONGLE SRD05VDC-SL-C relay module, and it works perfectly on my original sketch (uploaded for your perusal)
Can anyone advise me now what to put in the sketch to achieve my original goal (1st Post)
[code//Libraries
#include "DHT.h"
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
//Constants
#define DHTPIN 10 // Cabinet Sensor on this PIN.
#define DHTTYPE DHT22
#define fan 6 // what pin, fan relay is connected to
DHT dht(DHTPIN, DHTTYPE);
int maxHum = 57; // Safe levels for Guitar Storge - 45% > 55%
int maxTemp = 27; // Safe levels for Guitar storage - 20c > 24c
//ALWAYS USE THIS WITH LCD I2C and Addres 0x27
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup() {
Serial.begin(9600);
pinMode(fan, OUTPUT);
dht.begin();
lcd.begin(20,4);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
}
void loop() {
// 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 temperature as Celsius
float t = dht.readTemperature();
// 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;
}
if(h > maxHum || t > maxTemp)
{
digitalWrite(fan, LOW);
} else {
digitalWrite(fan, HIGH);
}
//Read data and store it to variables hum and temp
h = dht.readHumidity();
t = dht.readTemperature();
Serial.print("Cabinet Humidity : ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Cabinet Temperature : ");
Serial.print(t);
Serial.println(" Degrees C");
lcd.setCursor(1,0);
lcd.print("* AIRCON CABINET *");
lcd.setCursor(1,1);
lcd.print("Current Condition ");
lcd.setCursor(1,2);
lcd.print("Temp : ");
lcd.print(t);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(2,3);
lcd.print("Hum : ");
lcd.print(h);
lcd.print(" % RH");
delay(5000);
}]
Why are you reading the humidity and temperature twice?
That makes debugging harder.
Hi.
I have removed one reading. Sketch below -
//Libraries
#include "DHT.h"
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
//Constants
#define DHTPIN 10 // Cabinet Sensor on this PIN.
#define DHTTYPE DHT22
#define fan 6 // what pin, fan relay is connected to
DHT dht(DHTPIN, DHTTYPE);
int maxHum = 57; // Safe levels for Guitar Storge - 45% > 55%
int maxTemp = 27; // Safe levels for Guitar storage - 20c > 24c
//ALWAYS USE THIS WITH LCD I2C and Addres 0x27
#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup() {
Serial.begin(9600);
pinMode(fan, OUTPUT);
dht.begin();
lcd.begin(20,4);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
}
void loop() {
// 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 temperature as Celsius
float t = dht.readTemperature();
// 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;
}
if(h > maxHum || t > maxTemp)
{
digitalWrite(fan, LOW);
} else {
digitalWrite(fan, HIGH);
}
Serial.print("Cabinet Humidity : ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Cabinet Temperature : ");
Serial.print(t);
Serial.println(" Degrees C");
lcd.setCursor(1,0);
lcd.print("* AIRCON CABINET *");
lcd.setCursor(1,1);
lcd.print("Current Condition ");
lcd.setCursor(1,2);
lcd.print("Temp : ");
lcd.print(t);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(2,3);
lcd.print("Hum : ");
lcd.print(h);
lcd.print(" % RH");
delay(5000);
}