Air Quality Monitor

Discussion in 'The Projects Forum' started by Apoorv Chaudhary, Sep 12, 2015.

  1. Apoorv Chaudhary

    Thread Starter New Member

    Sep 12, 2015
    7
    2
  2. Wendy

    Moderator

    Mar 24, 2008
    20,766
    2,536
    You need to host the code and whatnot locally, perhaps as a .zip file. If this requirement is not met I will move this thread to the Projects Forum. This post will be removed when the Completed Projects forum requirements are met.

    How to post a project to the collection
     
  3. Apoorv Chaudhary

    Thread Starter New Member

    Sep 12, 2015
    7
    2
    Here is the code for this project

    Code (C):
    1.  
    2. #include <LiquidCrystal_I2C.h>
    3. #include <DHT.h>
    4. #include <Wire.h>
    5. #include "RTClib.h"
    6. #include <SPI.h>
    7. #include <SD.h>
    8.  
    9. #define DHTTYPE DHT11
    10.  
    11. // select the pins used on the LCD panel
    12. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7);
    13.  
    14. DHT dht(A1, DHTTYPE);
    15. const int chipSelect = 4;
    16. int measurePin = A2;
    17. int ledPower = A3;
    18. int thermistorPin = A1;
    19.  
    20. int samplingTime = 280;
    21. int deltaTime = 40;
    22. int sleepTime = 9680;
    23.  
    24. float voMeasured = 0;
    25. float calcVoltage = 0;
    26. float dustDensity = 0;
    27.  
    28. float avgDust = 0;
    29. int avgDustCnt = 0;
    30.  
    31. byte termometru[8] = //icon for termometer
    32. {
    33. 4, 10, 10, 10, 14, 31, 31, 14
    34. };
    35.  
    36. byte picatura[8] = //icon for water droplet
    37. {
    38. 4, 4, 14, 14, 31, 31, 31, 14
    39. };
    40.  
    41. byte threeIco[8] = //raise to power 3 character
    42. {
    43. 24, 4, 24, 4, 24, 0, 0, 0
    44. };
    45.  
    46. byte miliIco[8] = //micrograms character
    47. {
    48. 0, 17, 17, 17, 19, 29, 16, 16
    49. };
    50.  
    51.  
    52. boolean backlit = true;
    53. RTC_DS1307 rtc;
    54.  
    55. void setup()
    56. {
    57. lcd.begin (16, 2); // for 16 x 2 LCD module
    58. lcd.createChar(1, termometru);
    59. lcd.createChar(2, picatura);
    60. lcd.createChar(3, threeIco);
    61. lcd.createChar(4, miliIco);
    62. lcd.setBacklightPin(3, POSITIVE);
    63. lcd.setBacklight(HIGH);
    64. Serial.begin(9600);
    65. while (!Serial) {
    66. ; // wait for serial port to connect.
    67. }
    68. Wire.begin();
    69. rtc.begin();
    70. //Uncomment this line and upload the sketch to arduino after uploading
    71. //again comment it and upload the sketch again this will set the date and time
    72. //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    73. pinMode(A3, OUTPUT);
    74. lcd.home();
    75. if (!SD.begin(chipSelect)) {
    76. Serial.println("Card failed, or not present");
    77. lcd.print("Card failed, or not present");
    78. // don't do anything more:
    79. return;
    80. }
    81. Serial.println("card initialized.");
    82. lcd.print("Card Initialized");
    83. }
    84.  
    85. void loop()
    86. {
    87. DateTime now = rtc.now();
    88. digitalWrite(ledPower, LOW); // power on the dust sensor LED
    89. delayMicroseconds(samplingTime);
    90.  
    91. voMeasured = analogRead(measurePin); // read the dust value
    92.  
    93. delayMicroseconds(deltaTime);
    94. digitalWrite(ledPower, HIGH); // turn the dust sensor LED off
    95. delayMicroseconds(sleepTime);
    96.  
    97. // 0 - 5V mapped to 0 - 1023 integer values
    98. // recover voltage
    99. calcVoltage = voMeasured * (5.0 / 1024.0);
    100.  
    101. // linear eqaution taken from [URL]http://www.howmuchsnow.com/arduino/airquality/[/URL]
    102. // Chris Nafis (c) 2012
    103. dustDensity = (0.172 * calcVoltage - 0.0999) * 1000;
    104.  
    105. avgDust += dustDensity;
    106. avgDustCnt++;
    107. if (avgDustCnt == 1000)
    108. {
    109. // make a string for assembling the data to log:
    110. String dataString = "";
    111. dataString += "Dst" + String(avgDust / avgDustCnt) + ",";
    112. dataString += "Tmp" + String(dht.readTemperature()) + ",";
    113. dataString += "Hum" + String(dht.readHumidity()) + ",";
    114. dataString += String(now.year()) + "/" + String(now.month()) + "/" + String(now.day()) + " ";
    115. dataString += String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second());
    116. File dataFile = SD.open("airlog.txt", FILE_WRITE);
    117. lcd.clear();
    118. lcd.home();
    119. lcd.write(1);
    120. lcd.print(dht.readTemperature(), 0);
    121. lcd.print((char)223);
    122. lcd.print("C ");
    123. lcd.write(2);
    124. lcd.print(dht.readHumidity(), 0);
    125. lcd.print("%");
    126. lcd.print(" ");
    127. if (now.hour() < 10)
    128. {
    129. lcd.print('0');
    130. }
    131. lcd.print(now.hour(), DEC);
    132. lcd.print(':');
    133. lcd.print(now.minute());
    134. lcd.setCursor(0, 1);
    135. lcd.print("Dust: ");
    136. lcd.print(avgDust / avgDustCnt, 1);
    137. lcd.print(" ");
    138. lcd.write(4);
    139. lcd.print("g/m");
    140. lcd.write(3);
    141. // if the file is available, write to it:
    142. if (dataFile) {
    143. dataFile.println(dataString);
    144. dataFile.close();
    145. // print to the serial port too:
    146. Serial.println(dataString);
    147. } else {
    148. Serial.println("error opening datalog.txt");
    149. }
    150. avgDust = 0;
    151. avgDustCnt = 0;
    152. }
    153. }
    154.  
    Moderators note: please use code tags for pieces of code
     
Loading...