Tutorial by Phoebe
/* # 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 */ #include "U8glib.h" U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8 #define MG_PIN (1) //define which analog input channel you are going to use #define BOOL_PIN (2) #define DC_GAIN (8.5) //define the DC gain of amplifier #define READ_SAMPLE_INTERVAL (50) //define how many samples you are going to take in normal operation #define READ_SAMPLE_TIMES (5) //define the time interval(in milisecond) between each samples in #define ZERO_POINT_VOLTAGE (0.324) //define the output of the sensor in volts when the concentration of CO2 is 400PPM #define REACTION_VOLTGAE (0.020) //define the voltage drop of the sensor when move the sensor from air into 1000ppm CO2 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=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); }