A library for the MQ2 sensor. Based on https://github.com/labay11 and http://sandboxelectronics.com/?p=165
Dependents: Detection_incendie
Diff: MQ2.h
- Revision:
- 0:a5033edf6975
- Child:
- 1:323f72b59e99
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQ2.h Thu Oct 05 11:33:31 2017 +0000 @@ -0,0 +1,52 @@ +#ifndef MQ2_h +#define MQ2_h + +#include "mbed.h" + +#define RL_VALUE 5 //define the load resistance on the board, in kilo ohms +#define RO_DEFAULT 10 +#define RO_CLEAN_AIR_FACTOR 9.83f +#define CALIBARAION_SAMPLE_TIMES 5 +#define CALIBRATION_SAMPLE_INTERVAL 50 +#define READ_SAMPLE_INTERVAL 50 +#define READ_SAMPLE_TIMES 5 + +//The curves +static float LPGCurve[] = {2.3f,0.21f,-0.47f}; +static float COCurve[] = {2.3f,0.72f,-0.34f}; +static float SmokeCurve[] = {2.3f,0.53f,-0.44f}; + +typedef struct { + float lpg; + float co; + float smoke; +} MQ2_data_t; + +typedef enum { + GAS_LPG = 0, + GAS_CO = 1, + GAS_SMOKE = 2 +} GasType; + + +class MQ2 { +public: + MQ2(PinName pin) : _pin(pin){ + Ro = RO_DEFAULT; + }; + void read(MQ2_data_t *ptr); + float readLPG(); + float readCO(); + float readSmoke(); + void begin(); +private: + AnalogIn _pin; + float MQRead(); + float MQGetGasPercentage(float rs_ro_ratio, GasType gas_id); + int MQGetPercentage(float rs_ro_ratio, float *pcurve); + float MQCalibration(); + float MQResistanceCalculation(int raw_adc); + float Ro; +}; + +#endif \ No newline at end of file