Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Last active September 30, 2015 06:24
Show Gist options
  • Save JustinSDK/7c27b5b63f406fd28ce3 to your computer and use it in GitHub Desktop.
Save JustinSDK/7c27b5b63f406fd28ce3 to your computer and use it in GitHub Desktop.
自製 CO2、溫溼度感應器
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define MG_PIN (0) //define which analog input channel you are going to use
#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
//normal operation
//These two values differ from sensor to sensor. user should derermine this value.
#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
#define DHTPIN 8
#define DHTTYPE DHT11 // DHT 11
float CO2Curve[3] = {2.602,ZERO_POINT_VOLTAGE,(REACTION_VOLTGAE/(2.602-3))};
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
DHT dht(DHTPIN, DHTTYPE);
byte dat[5];
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
dht.begin();
}
void loop() {
float volts = MGRead(MG_PIN);
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
lcd.clear();
printRPM(MGGetPercentage(volts,CO2Curve));
printHumdityTemp(temperature, humidity);
delay(500);
}
/***************************** MGRead *********************************************
Input: mg_pin - analog channel
Output: output of SEN-000007
Remarks: This function reads the output of SEN-000007
************************************************************************************/
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 - SEN-000007 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) {
return pow(10, ((volts/DC_GAIN)-pcurve[1])/pcurve[2]+pcurve[0]);
}
void printRPM(float percentage) {
lcd.setCursor(0,0);
lcd.print("CO2 RPM: ");
lcd.print(percentage);
}
void printHumdityTemp(int temperature, int humdity) {
lcd.setCursor(0,1);
lcd.print("TEMP: ");
lcd.print(temperature);
lcd.print(" HUM: ");
lcd.print(humdity);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment