What's new
PinoyTechnician

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

REFERENCE GAS Leak/SMOKE Sensor Arduino Library

aybyd

Registered
Joined
Jun 29, 2014
Messages
7
Reaction score
2
Points
1
Hi, just want to share one of my created library for GAS Leak and Smoke Sensor device for Arduino.
Here is my library. I created this for one of my project I designed for some Universities.

Header File: MQxSensor.h
PHP:
//Created by: Aybyd Romero (c)2015
//Public License

#ifndef MQxSensor_h
#define MQxSensor_h

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#define DEBUG_MQxSensor

// Define where debug output will be printed.
#define DEBUG_PRINTER Serial

// Setup debug printing macros.

#ifdef DEBUG_MQxSensor
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#else
#define DEBUG_PRINT(...) {}
#define DEBUG_PRINTLN(...) {}
#endif


#define         CLEAN_AIR_FACTOR          (9.83)
#define         _LPG                         (0)
#define         _CO                          (1)
#define         _SMOKE                       (2)


class MQxSensor
{

  public:
    MQxSensor(int pin, int res);
    void begin(void);
    int MQxGetLPG();
    int MQxGetCO();
    int MQxGetSMOKE();


  private:
    int   MQx_PIN;
    int   MQx_LoadRes;
    int   MQxGetConcentration(float ratio, int id);
    float MQxSensorResistance(int adc);
    float MQxCalibrate();
    float MQRead();
    int   MQxGetPPM(float ratio, float *curve);

    //This logarithmic x,y,slope data are for MQ2 Sensor, please refer to datasheet for type of sensor you have
    float LPGCurve[3] = {2.3, 0.21, -0.47};
    float COCurve[3] = {2.3, 0.72, -0.34};
    float SmokeCurve[3] = {2.3, 0.53, -0.44};
    float Ro = 10;

};



#endif

Source File: MQxSensor.cpp
PHP:
#include "MQxSensor.h"

MQxSensor::MQxSensor(int pin, int res) {
  MQx_PIN = pin;
  MQx_LoadRes = res;
}

int MQxSensor::MQxGetLPG() {
  return MQxGetConcentration(MQRead() / Ro, _LPG) ;
}

int MQxSensor::MQxGetCO() {
  return MQxGetConcentration(MQRead() / Ro, _CO) ;
}

int MQxSensor::MQxGetSMOKE() {
  return MQxGetConcentration(MQRead() / Ro, _SMOKE) ;
}

void MQxSensor::begin(void) {
  DEBUG_PRINTLN(F("Calibrating..."));
  Ro = MQxCalibrate();

  DEBUG_PRINTLN(F("Calibration done..."));
  DEBUG_PRINTLN(F("Ro:"));
  DEBUG_PRINT(Ro);
}

float MQxSensor::MQxSensorResistance(int adc)
{
  //DEBUG_PRINT(raw_adc);
  return ( ((float)MQx_LoadRes * (1023 - adc) / adc));
}

float MQxSensor::MQxCalibrate()
{
  int i;
  float val = 0;

  for (i = 0; i < 50; i++) {
    val += MQxSensorResistance(analogRead(MQx_PIN));
    delay(500);
  }
  val = val / 50;
  val = val / CLEAN_AIR_FACTOR;
  return val;
}

float MQxSensor::MQRead()
{
  int i;
  float res = 0;

  for (i = 0; i < 5; i++) {
    res += MQxSensorResistance(analogRead(MQx_PIN));
    delay(50);
  }
  res = res / 5;
  return res;
}

int MQxSensor::MQxGetConcentration(float ratio, int id)
{
  if ( id == _LPG ) {
    return MQxGetPPM(ratio, LPGCurve);
  } else if ( id == _CO ) {
    return MQxGetPPM(ratio, COCurve);
  } else if ( id == _SMOKE ) {
    return MQxGetPPM(ratio, SmokeCurve);
  }
  return 0;
}

int  MQxSensor::MQxGetPPM(float ratio, float *curve)
{
  return (pow(10, ( ((log(ratio) - curve[1]) / curve[2]) + curve[0])));
}


Sample Usage:

PHP:
#include <MQxSensor.h>

MQxSensor sensor(0, 10); // xAnalog Pin#, xResistant of resistor on board x1000, in this layout we used 37 kilo ohms that set to default value of 10.

void setup() {
  Serial.begin(9600);
  sensor.begin();
}

void loop() {
  Serial.print("SMOKE: ");
  Serial.println(sensor.MQxGetSMOKE());
  Serial.print("GAS/LPG: ");
  Serial.println(sensor.MQxGetLPG());
  Serial.print("CO: ");
  Serial.println(sensor.MQxGetCO());
}

Enjoy sensing :)

This is the sensor I used on the sample codes:
img_2006.png

You can also use any MQx Gas and Smoke Sensor, just change the logarithmic value according to the datasheet.
 
pag gumana yan... ng maayos, magandang imarket yan....
 
Back
Top