This library is made for MQ-4 gás sensor.
Dependents: MQ4_example Navitec-Firmware
MQ4.h@1:d64cac41351a, 2021-04-04 (annotated)
- Committer:
- renanbmx123
- Date:
- Sun Apr 04 14:38:22 2021 +0000
- Revision:
- 1:d64cac41351a
- Parent:
- 0:401959f72658
Refatorado;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
renanbmx123 | 0:401959f72658 | 1 | #ifndef MQ4_h |
renanbmx123 | 0:401959f72658 | 2 | #define MQ4_h |
renanbmx123 | 0:401959f72658 | 3 | |
renanbmx123 | 0:401959f72658 | 4 | #include "mbed.h" |
renanbmx123 | 0:401959f72658 | 5 | |
renanbmx123 | 0:401959f72658 | 6 | #define RL_VALUE 10 //define the load resistance on the board, in kilo ohms |
renanbmx123 | 0:401959f72658 | 7 | #define RO_DEFAULT 10 //Ro is initialized to 10 kilo ohms |
renanbmx123 | 0:401959f72658 | 8 | #define RO_CLEAN_AIR_FACTOR 4.4f //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO, which is derived from the chart in datasheet |
renanbmx123 | 0:401959f72658 | 9 | #define CALIBARAION_SAMPLE_TIMES 5 //define how many samples you are going to take in the calibration phase |
renanbmx123 | 0:401959f72658 | 10 | #define CALIBRATION_SAMPLE_INTERVAL 50 //define the time interal(in milisecond) between each samples |
renanbmx123 | 0:401959f72658 | 11 | #define READ_SAMPLE_INTERVAL 50 //define how many samples you are going to take in normal operation |
renanbmx123 | 0:401959f72658 | 12 | #define READ_SAMPLE_TIMES 5 //define the time interal(in milisecond) between each samples |
renanbmx123 | 0:401959f72658 | 13 | |
renanbmx123 | 0:401959f72658 | 14 | //The curves |
renanbmx123 | 0:401959f72658 | 15 | // {log(X1) ,log(Y),[log(Y1) - log(Y2)] / [log(X2) - log(X1)]} get values from datasheet |
renanbmx123 | 0:401959f72658 | 16 | static float LPGCurve[] = {2.3f,0.43f,-0.33f}; |
renanbmx123 | 0:401959f72658 | 17 | static float H2Curve[] = {2.3f,0.57f,-0.16f}; |
renanbmx123 | 0:401959f72658 | 18 | static float CH4Curve[] = {2.3f,0.27f,-0.38f}; |
renanbmx123 | 0:401959f72658 | 19 | |
renanbmx123 | 0:401959f72658 | 20 | typedef struct { |
renanbmx123 | 0:401959f72658 | 21 | float lpg; |
renanbmx123 | 0:401959f72658 | 22 | float h2; |
renanbmx123 | 0:401959f72658 | 23 | float ch4; |
renanbmx123 | 0:401959f72658 | 24 | } MQ4_data_t; |
renanbmx123 | 0:401959f72658 | 25 | |
renanbmx123 | 0:401959f72658 | 26 | typedef enum { |
renanbmx123 | 0:401959f72658 | 27 | GAS_LPG = 0, |
renanbmx123 | 0:401959f72658 | 28 | GAS_H2 = 1, |
renanbmx123 | 0:401959f72658 | 29 | GAS_CH4 = 2 |
renanbmx123 | 0:401959f72658 | 30 | } GasType; |
renanbmx123 | 0:401959f72658 | 31 | |
renanbmx123 | 0:401959f72658 | 32 | |
renanbmx123 | 0:401959f72658 | 33 | class MQ4 { |
renanbmx123 | 0:401959f72658 | 34 | public: |
renanbmx123 | 0:401959f72658 | 35 | MQ4(PinName pin) : _pin(pin){ |
renanbmx123 | 0:401959f72658 | 36 | Ro = RO_DEFAULT; |
renanbmx123 | 0:401959f72658 | 37 | }; |
renanbmx123 | 0:401959f72658 | 38 | void read(MQ4_data_t *ptr); |
renanbmx123 | 0:401959f72658 | 39 | float readLPG(); |
renanbmx123 | 0:401959f72658 | 40 | float readH2(); |
renanbmx123 | 0:401959f72658 | 41 | float readCH4(); |
renanbmx123 | 0:401959f72658 | 42 | float get_Ro(); |
renanbmx123 | 0:401959f72658 | 43 | void begin(); |
renanbmx123 | 0:401959f72658 | 44 | private: |
renanbmx123 | 0:401959f72658 | 45 | AnalogIn _pin; |
renanbmx123 | 0:401959f72658 | 46 | float MQRead(); |
renanbmx123 | 0:401959f72658 | 47 | float MQGetGasPercentage(float rs_ro_ratio, GasType gas_id); |
renanbmx123 | 0:401959f72658 | 48 | int MQGetPercentage(float rs_ro_ratio, float *pcurve); |
renanbmx123 | 0:401959f72658 | 49 | float MQCalibration(); |
renanbmx123 | 0:401959f72658 | 50 | float MQResistanceCalculation(int raw_adc); |
renanbmx123 | 0:401959f72658 | 51 | float Ro; |
renanbmx123 | 0:401959f72658 | 52 | |
renanbmx123 | 0:401959f72658 | 53 | }; |
renanbmx123 | 0:401959f72658 | 54 | |
renanbmx123 | 0:401959f72658 | 55 | #endif |