Tutorial by Phoebe
It's 12:50, right after lunch. The gloomy winter has arrived months ago, and you had just settled your brain for a short winter's nap in the warm office. Some people may think that ventilation is the same thing as air conditioning, but does it really make sense of keeping a closed, dense space air-conditioned all day long? The high temperature and poor heat dissipation make the room similar to a "greenhouse", and Carbon Dioxide is the largest contributor to the greenhouse effect. Thus I decided to DIY a simple indoor carbon dioxide detector with Arduino.
1) DFRduino UNO R3
2) LCD12864 Shield for Arduino
3) Arduino Compatible CO2 Sensor
4) 7.4 V, 2200 ma lipo battery (with charge and discharge protection circuit)
Please remember installing the U8g library into Arduino IDE library before uploading the following code.
/* # This Sample code is for display the CO2 sensor output on LCD12864. # Editor : Phoebe # Date : 2014.1.15 # Ver : 0.1 # Product: LCD12864 Shield for Arduino # SKU : DFR0287 # Hardwares: 1. Arduino UNO 2. LCD12864 Shield for Arduino 3. CO2 Sensor #Steps: 1.Connect the CO2 Sensor to the Analog pin 0 2.Power the UNO board with 7~12V */ U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8 float CO2Curve[3] = { 2.602,ZERO_POINT_VOLTAGE,(REACTION_VOLTGAE/(2.602-3))}; int percentage; float volts; /***************************** LCD12864 display *********************************** Display the voltage & ppm data on the LCD12864 ************************************************************************************/ void draw() { u8g.setFont(u8g_font_unifont); u8g.drawStr( 0,11,"Voltage:"); u8g.setPrintPos(70,11); u8g.print(volts); u8g.drawStr( 110, 11,"V" ); u8g.drawStr(0,30,"CO2:"); if (percentage == -1) { u8g.drawStr( 40,30,"<400" ); } else { u8g.setPrintPos(40,30); u8g.print(percentage,DEC); } u8g.drawStr( 80,30,"ppm" ); u8g.drawStr( 3, 63,"www.DFRobot.com" ); } /***************************** MGRead ********************************************* Input: mg_pin - analog channel Output: output of CO2 Sensor Remarks: This function reads the output of CO2 Sensor ************************************************************************************/ float MGRead(int mg_pin) { int i; float v=0; for (i=0;i<READ_SAMPLE_TIMES;i++) { v += analogRead(mg_pin); delay(READ_SAMPLE_INTERVAL); } v = (v/READ_SAMPLE_TIMES) *5/1024 ; return v; } /***************************** MQGetPercentage ********************************** Input: volts - CO2 Sensor output measured in volts pcurve - pointer to the curve of the target gas Output: ppm of the target gas Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm) of the line could be derived if y(MG-811 output) is provided. As it is a logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic value. ************************************************************************************/ int MGGetPercentage(float volts, float *pcurve) { if ((volts/DC_GAIN )>=ZERO_POINT_VOLTAGE) { return -1; } else { return pow(10, ((volts/DC_GAIN)-pcurve[1])/pcurve[2]+pcurve[0]); } } void setup() { u8g.setRot180();// rotate screen, if required } void loop() { volts = MGRead(MG_PIN); percentage = MGGetPercentage(volts,CO2Curve); u8g.firstPage(); do { draw(); } while( u8g.nextPage() ); delay(200); }
As seen on the screen, the indoor co2 concentration is < 400 PPM which is suitable for survival:)