/*Smoke Sensor MQ7 mit LCD Display connect the MQ2 sensor as follows : VCC >>> 5V AD0 >>> A0 GND >>> GND Contribution: epierre Based on http://sandboxelectronics.com/?p=165 License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0) */ #include //Sample using LiquidCrystal library LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // select the pins used on the LCD panel #define CHILD_ID_MQ 0 /************************Hardware Related Macros************************************/ #define MQ_SENSOR_ANALOG_PIN (0) //define which analog input channel you are going to use #define RL_VALUE (5) //define the load resistance on the board, in kilo ohms #define RO_CLEAN_AIR_FACTOR (9.83) //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO, //which is derived from the chart in datasheet /***********************Software Related Macros************************************/ #define CALIBARAION_SAMPLE_TIMES (50) //define how many samples you are going to take in the calibration phase #define CALIBRATION_SAMPLE_INTERVAL (500) //define the time interal(in milisecond) between each samples in the //cablibration phase #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 interal(in milisecond) between each samples in //normal operation /**********************Application Related Macros**********************************/ #define GAS_LPG (0) #define GAS_CO (1) #define GAS_SMOKE (2) /*****************************Globals***********************************************/ unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) //VARIABLES float Ro = 10000.0; // this has to be tuned 10K Ohm int val = 0; // variable to store the value coming from the sensor float valMQ =0.0; float lastMQ =0.0; float LPGCurve[3] = {2.3,0.21,-0.47}; //two points are taken from the curve. //with these two points, a line is formed which is "approximately equivalent" //to the original curve. //data format:{ x, y, slope}; point1: (lg200, 0.21), point2: (lg10000, -0.59) float COCurve[3] = {2.3,0.72,-0.34}; //two points are taken from the curve. //with these two points, a line is formed which is "approximately equivalent" //to the original curve. //data format:{ x, y, slope}; point1: (lg200, 0.72), point2: (lg10000, 0.15) float SmokeCurve[3] ={2.3,0.53,-0.44}; //two points are taken from the curve. //with these two points, a line is formed which is "approximately equivalent" //to the original curve. //data format:{ x, y, slope}; point1: (lg200, 0.53), point2:(lg10000,-0.22) void setup() { Serial.begin(9600); //UART setup, baudrate = 9600bps Serial.print("Calibrating...\n"); lcd.begin(16, 2); // start the LCD library lcd.setCursor(0,0); // set cursor position at start lcd.print("Kalibrierung ..."); Serial.print("Setup ..."); Ro = MQCalibration(MQ_SENSOR_ANALOG_PIN); // display standard lcd.clear(); lcd.setCursor(0,0); lcd.print("LPG:"); lcd.setCursor(9,0); lcd.print("CO:"); lcd.setCursor(0,1); lcd.print("SMOKE:"); lcd.setCursor(12,1); lcd.print("ppm"); } void loop() { uint16_t valMQ = MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_CO); Serial.println(val); Serial.print("LPG:"); Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_LPG) ); lcd.setCursor(4,0); // clear digits lcd.print( " " ); lcd.setCursor(4,0); lcd.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_LPG) ); Serial.print( "ppm" ); Serial.print(" "); Serial.print("CO:"); lcd.setCursor(12,0); // clear digits lcd.print( " " ); Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_CO) ); lcd.setCursor(12,0); lcd.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_CO) ); Serial.print( "ppm" ); Serial.print(" "); Serial.print("SMOKE:"); lcd.setCursor(6,1); // clear digits lcd.print( " " ); Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_SMOKE) ); lcd.setCursor(6,1); lcd.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_SMOKE) ); Serial.print( "ppm" ); Serial.print("\n"); } /****************** MQResistanceCalculation **************************************** Input: raw_adc - raw value read from adc, which represents the voltage Output: the calculated sensor resistance Remarks: The sensor and the load resistor forms a voltage divider. Given the voltage across the load resistor and its resistance, the resistance of the sensor could be derived. ************************************************************************************/ float MQResistanceCalculation(int raw_adc) { return ( ((float)RL_VALUE*(1023-raw_adc)/raw_adc)); } /***************************** MQCalibration **************************************** Input: mq_pin - analog channel Output: Ro of the sensor Remarks: This function assumes that the sensor is in clean air. It use MQResistanceCalculation to calculates the sensor resistance in clean air and then divides it with RO_CLEAN_AIR_FACTOR. RO_CLEAN_AIR_FACTOR is about 10, which differs slightly between different sensors. ************************************************************************************/ float MQCalibration(int mq_pin) { int i; float val=0; for (i=0;i