Due to attachment problems I include the rest of the code for the i2c interface here.
//*******************************************************
Code for 1604 with i2c interface
//*******************************************************
// Temperature and Humidity display and logger
// Code is patched and modified from various sources (Thank you original contributors)
/*
I made this instrument to keep the humidity in my house in check. Started off with just a sensor and display.
This was ignored most the time and therefore a buzzer was added. This starts giving a short beep as the humidity exceeds the "hla".
The more it exceeds it the longer and more frequent the beeps become. When the humidity reaches "hha" it beeps continuously.
For record purposes a SD card logs the temperatures and humidity.
This project started off on an Arduino Duemilanove 328 and then transferred to a strip-board.
I used Arduino IDE 1.0.1
Feel free to contact me for any suggestions and questions.
maroelawerner@gmail.com
*/
#include <SD.h> // needed for SD card
const int chipSelect = 4;
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> // needed for LCD 16x2 display
#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574A is
#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);
int beep = 10; // pin for piezo buzzer
int hd = 0; // initialize beep delay
int ds = 0; // initialize beep interval
int hum = 0; // initialize humidity
int hla = 70; // High humidity Low level Alarm
int hha = 20; // High humidity High level Alarm
File root;
// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(9, 8, 6 , 5, 3, 2); // connect rs -> pin 9, enable -> pin 8, d4 -> pin 6, d5 -> pin 5, d6 -> pin 3, d7 -> pin 2
#include "DHT.h"
#define DHTPIN 7 // pin for data from DHT
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
{
lcd.begin (16,2,LCD_5x8DOTS);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); // init the backlight
lcd.setBacklight(HIGH); // Backlight on
}
pinMode(beep, OUTPUT); // define pin as output
if (!SD.begin(chipSelect)) {
// don't do anything more:
return;
}
lcd.print("card initialized.");
delay(3000);
lcd.println("DHTxx test!");
lcd.clear();
lcd.print("Home Weather");
lcd.setCursor(0, 1);
lcd.print("display");
delay(2000);
dht.begin();
}
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();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
lcd.println("Failed to read from DHT"); // display error message if no data from sensor is available
} else {
lcd.setBacklight(HIGH); // Backlight on
lcd.home();
lcd.print("Hum: ");
lcd.print(h);
lcd.print(" %");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" *C ");
int sensor = h;
String dataString = "";
dataString += String(sensor);
dataString += ",";
sensor = t;
dataString += String(sensor);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
if (h > hla);{ // calculate duration and interval of beep
hum = h;
hd = hum - hla; // duration of beep
ds = hha - hd; // interval of beep
ds = ds * 10;
digitalWrite(beep, HIGH); // turn the Buzzer on
for (int x = 0; x <= hd; x++) {
delay(100);
}
Serial.println();
digitalWrite(beep, LOW); // turn the Buzzer off
for (int y = 0; y <= ds; y++) {
delay(100);
}
}
}
}
//***************************************************
Code for i2c 2004 interface
//***************************************************
// Temperature and Humidity display and logger
// Code is patched and modified from various sources (Thank you original contributors)
/*
I made this instrument to keep the humidity in my house in check. Started off with just a sensor and display.
This was ignored most the time and therefore a buzzer was added. This starts giving a short beep as the humidity exceeds the "hla".
The more it exceeds it the longer and more frequent the beeps become. When the humidity reaches "hha" it beeps continuously.
For record purposes a SD card logs the temperatures and humidity.
This project started off on an Arduino Duemilanove 328 and then transferred to a strip-board.
I used Arduino IDE 1.0.1
Feel free to contact me for any suggestions and questions.
maroelawerner@gmail.com
*/
#include <SD.h> // needed for SD card
const int chipSelect = 4;
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> // needed for LCD 16x2 display
#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574A is
#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);
int beep = 10; // pin for piezo buzzer
int hd = 0; // initialize beep delay
int ds = 0; // initialize beep interval
int hum = 0; // initialize humidity
int hla = 70; // High humidity Low level Alarm
int hha = 20; // High humidity High level Alarm
File root;
// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(9, 8, 6 , 5, 3, 2); // connect rs -> pin 9, enable -> pin 8, d4 -> pin 6, d5 -> pin 5, d6 -> pin 3, d7 -> pin 2
#include "DHT.h"
#define DHTPIN 7 // pin for data from DHT
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
{
lcd.begin (16,2,LCD_5x8DOTS);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); // init the backlight
lcd.setBacklight(HIGH); // Backlight on
}
pinMode(beep, OUTPUT); // define pin as output
if (!SD.begin(chipSelect)) {
// don't do anything more:
return;
}
lcd.print("card initialized.");
delay(3000);
lcd.println("DHTxx test!");
lcd.clear();
lcd.print("Home Weather");
lcd.setCursor(0, 1);
lcd.print("display");
delay(2000);
dht.begin();
}
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();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
lcd.println("Failed to read from DHT"); // display error message if no data from sensor is available
} else {
lcd.setBacklight(HIGH); // Backlight on
lcd.home();
lcd.print("Hum: ");
lcd.print(h);
lcd.print(" %");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" *C ");
int sensor = h;
String dataString = "";
dataString += String(sensor);
dataString += ",";
sensor = t;
dataString += String(sensor);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
if (h > hla);{ // calculate duration and interval of beep
hum = h;
hd = hum - hla; // duration of beep
ds = hha - hd; // interval of beep
ds = ds * 10;
digitalWrite(beep, HIGH); // turn the Buzzer on
for (int x = 0; x <= hd; x++) {
delay(100);
}
Serial.println();
digitalWrite(beep, LOW); // turn the Buzzer off
for (int y = 0; y <= ds; y++) {
delay(100);
}
}
}
}
//*********************************************