Hi everyone!
I am quite new to Arduino, and although I have set up two "thermoduino" projects at my home, (Thanks to marque developing work...) this is the first project I am developing on my own...
As the title says, the project is to have my bathroom extractor fan to come on depending on the humidity...
For that I have put together:
1 x Arduino Uno //the board
1 x DHT22 //Temperature and humidity sensor
1 x LCD and keypad display //To give visual information and to change set point using the keypad.
1 x 5v relay board //to turn on and off the fan.
Now, from what I have learned reading, and looking at other sketches I managed to write my code, pretty much to what I want...
Although there is one thing that I do not know why is not working, and your help will be welcome!
This is my code:
/*******************************************************************Bathroom Extractor FAN Automation - By lmsvvavr v.1.0.0 ********************************/
/* Bathroom extractor FAN automation, based on humidity readings.
What you need:
One Arduino Board, this has been based on the UNO, you can use other boards, you might need however to adapt the sketch and connections.
One LCD, this has been based on the DFROBOT 16X2, the LCD is however optional, the system will work just on its own.
One DHT humidity and temperature sensor, you need to adapt the sketch if not using the DHT22.
One single relay board, capable of handling with your FAN load, please make the necessary research to ensure of that.
*/
#include "DHT.h" //DHT library, google to download it and install if you don't have it already.
#include <LiquidCrystal.h>//LCD library, google to download it and install if you don't have it already.
#define DHTPIN 2 //Defines the pin where the DATA is connected.
#define DHTTYPE DHT22 // DHT 22 (AM2302), if you use DHT11 or DHT23 change it accondingly.
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
DHT dht(DHTPIN, DHTTYPE);
const int FAN = 11; //Defines the pin where the relay is connected.
float hm; // Measured humidity
int hd = 67; // Desired humidity
int lcd_key = 0;
int adc_key_in = 0;
int count = 0;
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Pins where the lcd is connected, change it accordingly if you are using different pin assignment.
/************************************************ SETUP *************************************/
void setup() {
pinMode(FAN, OUTPUT); //Defines the pin as an output.
Serial.begin(9600); //Serial port speed.
Serial.println("FAN Humidity System"); //Serial message.
lcd.begin(16, 2); //Defines how many rows and columns the lcd has.
lcd.setCursor(0,0);
lcd.print("Bathroom FAN ATM"); //Start-Up message line 0 (Top Line)
lcd.setCursor(0,1);
lcd.print("By lmsvvavr"); //Start-Up message line 1 (Bottom Line)
delay(3000);
lcd.setCursor(0,0);
lcd.print("System is now ");
lcd.setCursor(0,1);
lcd.print("Starting ");
delay(250);
lcd.setCursor(0,1);
lcd.print("Starting. ");
delay(250);
lcd.setCursor(0,1);
lcd.print("Starting.. ");
delay(500);
lcd.setCursor(0,1);
lcd.print("Starting... ");
delay(250);
lcd.setCursor(0,1);
lcd.print("Starting.... ");
delay(500);
lcd.setCursor(0,1);
lcd.print("Starting..... ");
delay(500);
lcd.setCursor(0,1);
lcd.print("Starting...... ");
delay(200);
lcd.setCursor(0,1);
lcd.print("Starting....... ");
delay(200);
lcd.setCursor(0,1);
lcd.print("Starting........");
dht.begin();
}
/********************************************** LOOP ***************************************/
void loop() {
adc_key_in = lcd_key;
lcd_key = read_LCD_buttons(); // read the buttons
float hm = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(t) || isnan(hm))
{
Serial.println("Failed to read from DHT");
lcd.setCursor(1,1);
lcd.print(" Check sensor! ");
}
else
{
Serial.print("Humidity: ");
Serial.print(hm);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
delay (1500);
lcd.setCursor(0,0); //Set where about on the lcd you want to print the values.
lcd.print(hd); //To print on the lcd the desired humidity.
lcd.print(" RH% Fan ON At"); //Message in front of the desired humidity.
lcd.setCursor(0,1); //Set where about on the lcd you want to print the values.
lcd.print(hm); //To print on the lcd the measured humidity.
lcd.print("RH% In Room"); //Message in front of the measured humidity.
}
if (hm > hd)
{
digitalWrite(FAN, LOW);
}
else
{
digitalWrite(FAN, HIGH);
}
if (adc_key_in != lcd_key)
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
lcd.setCursor(0,0);
lcd.print("Reset RH% to its ");
lcd.setCursor(0,1); //Reset the changes to the FAN set point.
lcd.print("default value...");
hd = (hd);
break;
}
case btnLEFT:
{
lcd.setCursor(0,0);
lcd.print(t);
lcd.print(" *C ");
lcd.setCursor(0,1); //Display the temperature.
lcd.print("Room Temperature");
delay(15000);
break;
}
case btnUP:
{
lcd.setCursor(7,0);
lcd.print("Increase "); //Increase the Fan set point.
hd = (hd + 1);
break;
}
case btnDOWN:
{
lcd.setCursor(7,0);
lcd.print("Decrease "); //Decrease the Fan set point.
hd = (hd - 1);
break;
}
case btnSELECT:
{
lcd.setCursor(0,0);
lcd.print("Uptime Min");
lcd.setCursor(7,0);
lcd.print(millis()/60000); //Display the Uptime since last Start/Reset and System version.
lcd.setCursor(0,1);
lcd.print("Sys Ver. v1.0.0 ");
delay(15000);
break;
}
case btnNONE:
{
break;
}
}
}
Everything works, except the "Right" key on the keypad.
I have set it to reset any changes on the set point of the relay, but the button does nothing!!!
All other ones work perfectly.
Sure is right in front of my nose... but I can see it!
I have tried another lcd/keypad... same result!
Thank you in advance for your help!
