Temperature and Humidity Logger Per Hour
In this project, we're going to use the DHT11 humidity and temperature sensor to calculate the average temperature and humidity every hour.
Components and supplies
1
Arduino UNO
1
Resistor 220 ohm
1
Breadboard (generic)
1
DHT11 Temperature & Humidity Sensor (4 pins)
1
Single Turn Potentiometer- 10k ohms
1
Jumper wires (generic)
1
Pmod SD
Apps and platforms
1
Arduino IDE
Project description
Code
Humidity and temperature logger
c_cpp
1/* Humidity and Temperature Logger 2 * 3 * Logging the humidity and temperature per 1 hour 4 * while taking samples every 15 minutes and 5 * get the averege hunidity and temperature by dividing 6 * them with the count of samples it got. 7 * 8 * The first reading is from 9 * the start up and gets refreshed 10 * every 15 minutes. 11 * 12 * 13 * Using the timeLib we count how many hours 14 * it runs. 15 * If we add an rtc we can have the real 16 * time of the time the variable logged. 17 * 18 * 19 * The sd pin out conection: 20 * SD SCK : PIN 13 21 * SD MISO : PIN 12 22 * SD MOSI : PIN 11 23 * SD CS : PIN 10 24 * 25 * 26 * The lcd conection: 27 * RS : PIN 9 28 * ENABLE : PIN 8 29 * LCD D4 : 7 30 * LCD D5 : 6 31 * LCD D6 : 5 32 * LCD D7 : 4 33 * 34 * The DHT conection: 35 * DHT : PIN 2 36 * 37 * 38 * Created 19/2/2019 39 * by Lefteris X. 40 */ 41///////////////////////TIME////////////////////////////////////// 42 43#include "TimeLib.h" 44 45unsigned long lastnow=0; // LAST TIME WRITED IN SD CARD 46 47unsigned long lastSample=0; // LAST SAMPLE TAKEN 48 49//////////////////////////////////////////////////////////////// 50 51///////////////////////////SD CARD///////////////////////// 52 53 #include <SPI.h> 54 #include <SD.h> 55 56 const int chipSelect = 10; 57 58 // make a string for assembling the data to log: 59 String dataString = ""; 60 String time = ""; 61 62 int lastWrite=0; // COUNT THE HOURS LOGGED 63 64 float averegeT, averegeH; // STORE THE AVEREGE OF DATA 65 66/////////////////////////////////////////////////////////// 67 68///////////////////////DHT///////////////////////////////// 69 70#include "DHT.h" 71 72#define DHTPIN 2 // the digital pin of sensor 73 74#define DHTTYPE DHT11 // define the type of sensor 75 76 77DHT dht(DHTPIN,DHTTYPE); // initialize DHT sensor 78 79// THE ARAY FOR STORING AND CALCULATING THE AVEREGE 80 float TempSample[4]= {0,0,0,0}; 81 float HumSample[4]= {0,0,0,0}; 82 83 int counter=1; // COUNT THE SAMPLES 84 85 86//////////////////////////////////////////////////////////// 87 88/////////////////////////////LCD//////////////////////////// 89 #include <LiquidCrystal.h> 90 91 LiquidCrystal lcd(9, 8, 7, 6, 5, 4); 92///////////////////////////////////////////////////////////////// 93 94void setup() { 95 Serial.begin(9600); // SERIAL INITIALIZE 96 97 dht.begin(); // DHT INITIALIZE 98 99 // GIVE THE FIRST VALUES UNTIL THE FIRST AVEREGE CHECK 100 averegeT=dht.readTemperature(); 101 averegeH=dht.readHumidity(); 102 103 104 lcd.begin(16,2); // LCD 16x2 INITIALIZE 105 106 107 setTime(00,00,00,19,2,19); // SET THE TIME AS ZERO TO COUNT THE TIME IT RUNS 108 109 110 Serial.print("Initializing SD card..."); 111 112 // see if the card is present and can be initialized: 113 if (!SD.begin(chipSelect)) { 114 Serial.println("Card failed, or not present"); 115 lcd.print("Card Failed"); 116 // don't do anything more: 117 while (1); 118 } 119 Serial.println("card initialized."); 120 lcd.clear(); 121 lcd.home(); 122 lcd.print("card initialized."); 123 delay(1000); 124 lcd.clear(); 125 126} 127 128void loop() { 129 130 131 samples(); 132 133 134 loging(); 135 136 137 lcd_print(); 138 139 140 141 142}//loop closing 143////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 144 145void lcd_print() 146{ 147/* 148 * a FUCTION TO PRINT EVERY 30 SECONDS THE HUMIDITY 149 * AND TEMPRATURE AND PRINT THE TIME IS RUNNING FOR 150 * 27 SECOND EVERY TIME. 151 */ 152 if (second()==1 || second()==30 ) 153 { 154 lcd.setCursor(03,00); 155 lcd.print(averegeH); 156 lcd.print(" "); 157 lcd.print(averegeT); 158 lcd.print(" "); 159 } 160 161 lcd.home(); 162 lcd.setCursor(00,00); 163 lcd.print(second()); 164 if (second()<=9) 165 { 166 lcd.setCursor(01,00); 167 lcd.print(" "); 168 } 169 170 while (second()>0 && second()<29) 171 { 172 173 lcd.setCursor(00,01); 174 lcd.print("Time : "); 175 lcd.print(hour()); 176 lcd.print(":"); 177 lcd.print(minute()); 178 lcd.print(":"); 179 lcd.print(second()); 180 lcd.print(" "); 181 break; 182 } 183 while (second()<59 && second()>29) 184 { 185 186 lcd.setCursor(00,01); 187 lcd.print("Loged Hours "); 188 lcd.print(lastWrite); 189 lcd.print(" "); 190 191 } 192 193 194} 195 196 197 198 199void samples() 200{ 201/* 202 * THE FUCTION THAT CALCULATES AND PRINTS BY SERIAL 203 * THE VARIABLE OF THE ARAYS AND THE AVEREGE 204 * EVERY 15 MINUTES 205 * 206 */ 207 unsigned long curentmills=millis(); 208 if (curentmills-lastSample>=900000) 209 { 210 lastSample=curentmills; 211 float t=dht.readTemperature(); 212 float h = dht.readHumidity(); 213 214 switch (counter) 215 { 216 case 1: 217 { 218 219 TempSample[0]=t; 220 HumSample[0]=h; 221 break; 222 } 223 case 2: 224 { 225 226 TempSample[1]=t; 227 HumSample[1]=h; 228 break; 229 } 230 case 3: 231 { 232 233 TempSample[2]=t; 234 HumSample[2]=h; 235 break; 236 } 237 case 4: 238 { 239 240 TempSample[3]=t; 241 HumSample[3]=h; 242 break; 243 } 244 } 245 246 Serial.println("==================================="); 247 248 for (int i=0; i<4; i++) 249 { 250 Serial.print("TempSample["); 251 Serial.print(i); 252 Serial.print("] = "); 253 Serial.println(TempSample[i]); 254 255 256 } 257 258 Serial.println("==================================="); 259 260 for (int j=0; j<4; j++) 261 { 262 263 Serial.print("HumSample["); 264 Serial.print(j); 265 Serial.print("] = "); 266 Serial.println(HumSample[j]); 267 268 } 269 270 Serial.println("==================================="); 271 272 273 float HumSampleMo= ( HumSample[0] + HumSample[1] + HumSample[2] + HumSample[3]) /counter; 274 Serial.print("HumSampleMo = "); 275 Serial.println(HumSampleMo); 276 277 float TempSampleMo= ( TempSample[0] + TempSample[1] + TempSample[2] + TempSample[3]) /counter; 278 Serial.print("TempSampleMo = "); 279 Serial.println(TempSampleMo); 280 281 Serial.println("==================================="); 282 283 averegeT = TempSampleMo; 284 285 averegeH = HumSampleMo; 286 287 288 289 counter++; 290 if (counter>4) 291 { 292 counter=1; 293 for (int i=0; i<4; i++) 294 { 295 296 297 TempSample[i] = 0; 298 299 HumSample[i] = 0; 300 301 } 302 303 } 304 } 305 306 307 308 309 310} 311 312 313 314 315 316 317void loging() 318{ 319 /* 320 * THE FUCTION THAT STORING TO THE SD CARD THE 321 * AVEREGE OF HUMIDITY AND TEMPERATURE EVERY 1 HOUR 322 * STARTS FROM 00:59:00 323 */ 324 float h = dht.readHumidity(); // save humidity to memory 325 326 float t = dht.readTemperature(); // save temperature in C to memory 327 328 unsigned long curentnow=millis(); 329 330 331 if ( isnan(h) || isnan(t)) 332 { 333 Serial.println("Failed to read from DHT sensor"); 334 lcd.clear(); 335 lcd.print("DHT MISING"); 336 337 } 338 339 // if (second()==1) 340 //if (minute()==59 && second()==0) 341 if(curentnow-lastnow>=3600000) 342 { 343 lastnow=curentnow; 344 345 346 dataString = " "; 347 time = " "; 348 349dataString += "Temperature Averege = "; 350dataString += String(averegeT); 351dataString += "\ "; 352dataString += "Humidity Averege = "; 353dataString += String(averegeH); 354dataString += "\ 355"; 356dataString += "Hours logged : "; 357dataString += String(lastWrite); 358 359time +="Time = "; 360time +=String(hour()); 361time +=":"; 362time +=String(minute()); 363time +=":"; 364time +=String(second()); 365 // open the file. note that only one file can be open at a time, 366 // so you have to close this one before opening another. 367 File dataFile = SD.open("templog.txt", FILE_WRITE); 368 369 // if the file is available, write to it: 370 if (dataFile) { 371 dataFile.println(time); 372 dataFile.println(dataString); 373 dataFile.flush(); 374 dataFile.close(); 375 // print to the serial port too: 376 Serial.println(time); 377 Serial.println(dataString); 378 } 379 // if the file isn't open, pop up an error: 380 else { 381 Serial.println("error opening datalog.txt"); 382 } 383 lastWrite++; 384 Serial.println(lastWrite); 385 } 386 387 388} 389 390/////////////////////////////////////////////////////////////////////////////////////////////////////////// 391 392 393
Downloadable files
Humidity and temperature fritzing
Humidity and temperature fritzing
Humidity and temperature fritzing
Humidity and temperature fritzing
Comments
Only logged in users can leave comments