|
| 1 | +// Include the library |
| 2 | +#include "MQ-Sensor-SOLDERED.h" |
| 3 | + |
| 4 | +// Wiring NOT required, only an arduino or esp8266 board |
| 5 | + |
| 6 | +/* |
| 7 | + This program was made to test the algorithm that from the ratio obtains |
| 8 | + the PPM (The core of this library), regardless of the plate in which the |
| 9 | + result is executed should give a small error (ideally less than 5%), is |
| 10 | + our way of self-evaluation, we tell the library what is the value of the |
| 11 | + ratio and ask him to calculate the PPM and compare it against the value |
| 12 | + that should really give, these values were taken from the same datasheet |
| 13 | + with which the library was made. |
| 14 | +*/ |
| 15 | + |
| 16 | +// Include the library |
| 17 | +#include "MQ-Sensor-SOLDERED.h" |
| 18 | + |
| 19 | +// Declare Sensor, any MQ will work |
| 20 | +MQ2 sensor(A1); |
| 21 | + |
| 22 | +/*****************************Globals***********************************************/ |
| 23 | +double ratio[4] = {}; |
| 24 | +double expectedValue[4] = {}; |
| 25 | +double calculatedValues[4] = {}; |
| 26 | +double error[4] = {}; |
| 27 | +/**************************Object_Sensor********************************************/ |
| 28 | + |
| 29 | +void setup() |
| 30 | +{ |
| 31 | + // Init serial port |
| 32 | + Serial.begin(115200); |
| 33 | + // init the sensor |
| 34 | + sensor.begin(); |
| 35 | + |
| 36 | + // Print in serial monitor |
| 37 | + Serial.println("MQ2 to MQ9 - test program"); |
| 38 | +} |
| 39 | + |
| 40 | +void loop() |
| 41 | +{ |
| 42 | + // Testing MQ2 LPG |
| 43 | + ratio[0] = 1.8; |
| 44 | + ratio[1] = 1.05; |
| 45 | + ratio[2] = 0.8; |
| 46 | + ratio[3] = 0.5; |
| 47 | + expectedValue[0] = 200; |
| 48 | + expectedValue[1] = 500; |
| 49 | + expectedValue[2] = 1000; |
| 50 | + expectedValue[3] = 3000; |
| 51 | + testSensor("MQ-2", 574.25, -2.222); |
| 52 | + |
| 53 | + // Testing MQ3 Benzene |
| 54 | + ratio[0] = 4; |
| 55 | + ratio[1] = 3; |
| 56 | + ratio[2] = 1; |
| 57 | + ratio[3] = 0.8; |
| 58 | + expectedValue[0] = 0.1; |
| 59 | + expectedValue[1] = 0.21; |
| 60 | + expectedValue[2] = 4; |
| 61 | + expectedValue[3] = 10; |
| 62 | + testSensor("MQ-3", 4.8387, -2.68); |
| 63 | + |
| 64 | + // Testing MQ4 CH4 |
| 65 | + ratio[0] = 1.9; |
| 66 | + ratio[1] = 1; |
| 67 | + ratio[2] = 0.6; |
| 68 | + ratio[3] = 0.42; |
| 69 | + expectedValue[0] = 200; |
| 70 | + expectedValue[1] = 1000; |
| 71 | + expectedValue[2] = 5000; |
| 72 | + expectedValue[3] = 10000; |
| 73 | + testSensor("MQ-4", 1012.7, -2.786); |
| 74 | + |
| 75 | + // Testing MQ5 H2 |
| 76 | + ratio[0] = 1.8; |
| 77 | + ratio[1] = 1.3; |
| 78 | + ratio[2] = 0.8; |
| 79 | + ratio[3] = 0.68; |
| 80 | + expectedValue[0] = 200; |
| 81 | + expectedValue[1] = 500; |
| 82 | + expectedValue[2] = 3000; |
| 83 | + expectedValue[3] = 10000; |
| 84 | + testSensor("MQ-5", 1163.8, -3.874); |
| 85 | + |
| 86 | + // Testing MQ6 CH4 |
| 87 | + ratio[0] = 2.6; |
| 88 | + ratio[1] = 1.9; |
| 89 | + ratio[2] = 1; |
| 90 | + ratio[3] = 0.8; |
| 91 | + expectedValue[0] = 200; |
| 92 | + expectedValue[1] = 500; |
| 93 | + expectedValue[2] = 2000; |
| 94 | + expectedValue[3] = 5000; |
| 95 | + testSensor("MQ-6", 2127.2, -2.526); |
| 96 | + |
| 97 | + // Testing MQ7 CO |
| 98 | + ratio[0] = 1.8; |
| 99 | + ratio[1] = 1; |
| 100 | + ratio[2] = 0.4; |
| 101 | + ratio[3] = 0.25; |
| 102 | + expectedValue[0] = 50; |
| 103 | + expectedValue[1] = 100; |
| 104 | + expectedValue[2] = 400; |
| 105 | + expectedValue[3] = 1000; |
| 106 | + testSensor("MQ-7", 99.042, -1.518); |
| 107 | + |
| 108 | + // Testing MQ8 H2 |
| 109 | + ratio[0] = 2.6; |
| 110 | + ratio[1] = 1; |
| 111 | + ratio[2] = 0.21; |
| 112 | + ratio[3] = 0.03; |
| 113 | + expectedValue[0] = 500; |
| 114 | + expectedValue[1] = 1000; |
| 115 | + expectedValue[2] = 3000; |
| 116 | + expectedValue[3] = 10000; |
| 117 | + testSensor("MQ-8", 976.97, -0.688); |
| 118 | + |
| 119 | + // Testing MQ9 LPG |
| 120 | + ratio[0] = 2; |
| 121 | + ratio[1] = 1.4; |
| 122 | + ratio[2] = 0.7; |
| 123 | + ratio[3] = 0.23; |
| 124 | + expectedValue[0] = 200; |
| 125 | + expectedValue[1] = 500; |
| 126 | + expectedValue[2] = 2000; |
| 127 | + expectedValue[3] = 10000; |
| 128 | + testSensor("MQ-9", 1000.5, -2.186); |
| 129 | + |
| 130 | + // Testing MQ131 O3 |
| 131 | + ratio[0] = 5; |
| 132 | + ratio[1] = 2; |
| 133 | + ratio[2] = 0.7; |
| 134 | + ratio[3] = 0.5; |
| 135 | + expectedValue[0] = 5; |
| 136 | + expectedValue[1] = 20; |
| 137 | + expectedValue[2] = 70; |
| 138 | + expectedValue[3] = 100; |
| 139 | + testSensor("MQ-131", 23.943, -1.11); |
| 140 | + |
| 141 | + // Testing MQ135 NH4 |
| 142 | + ratio[0] = 2.6; |
| 143 | + ratio[1] = 1.9; |
| 144 | + ratio[2] = 1.1; |
| 145 | + ratio[3] = 0.75; |
| 146 | + expectedValue[0] = 10; |
| 147 | + expectedValue[1] = 20; |
| 148 | + expectedValue[2] = 100; |
| 149 | + expectedValue[3] = 200; |
| 150 | + testSensor("MQ-135", 102.2, -2.473); |
| 151 | + |
| 152 | + // Testing MQ303 Isobutano |
| 153 | + ratio[0] = 0.4; |
| 154 | + ratio[1] = 0.3; |
| 155 | + ratio[2] = 0.19; |
| 156 | + ratio[3] = 0.08; |
| 157 | + expectedValue[0] = 100; |
| 158 | + expectedValue[1] = 300; |
| 159 | + expectedValue[2] = 1000; |
| 160 | + expectedValue[3] = 10000; |
| 161 | + testSensor("MQ-303", 6.2144, -2.894); |
| 162 | + |
| 163 | + // Testing MQ309 CO |
| 164 | + ratio[0] = 6; |
| 165 | + ratio[1] = 5; |
| 166 | + ratio[2] = 4.3; |
| 167 | + ratio[3] = 4; |
| 168 | + expectedValue[0] = 1000; |
| 169 | + expectedValue[1] = 3000; |
| 170 | + expectedValue[2] = 5000; |
| 171 | + expectedValue[3] = 7000; |
| 172 | + testSensor("MQ-309", 1000000, -4.01); |
| 173 | + |
| 174 | + // Testing linear equation for MQ-4 |
| 175 | + sensor.setRegressionMethod(0); //_PPM = pow(10, (log10(ratio)-b)/a) |
| 176 | + ratio[0] = 2.5; |
| 177 | + ratio[1] = 1.5; |
| 178 | + ratio[2] = 0.9; |
| 179 | + ratio[3] = 0.65; |
| 180 | + expectedValue[0] = 200; |
| 181 | + expectedValue[1] = 1000; |
| 182 | + expectedValue[2] = 5000; |
| 183 | + expectedValue[3] = 10000; |
| 184 | + testSensor("MQ-4", -0.318, 1.133); |
| 185 | + |
| 186 | + while (1) |
| 187 | + ; |
| 188 | +} |
| 189 | + |
| 190 | + |
| 191 | +void testSensor(String SensorName, float A, float B) |
| 192 | +{ |
| 193 | + sensor.setA(A); |
| 194 | + sensor.setB(B); |
| 195 | + calculatedValues[0] = sensor.validateEcuation(ratio[0]); |
| 196 | + calculatedValues[1] = sensor.validateEcuation(ratio[1]); |
| 197 | + calculatedValues[2] = sensor.validateEcuation(ratio[2]); |
| 198 | + calculatedValues[3] = sensor.validateEcuation(ratio[3]); |
| 199 | + error[0] = calculatePercentualError(expectedValue[0], calculatedValues[0]); |
| 200 | + error[1] = calculatePercentualError(expectedValue[1], calculatedValues[1]); |
| 201 | + error[2] = calculatePercentualError(expectedValue[2], calculatedValues[2]); |
| 202 | + error[3] = calculatePercentualError(expectedValue[3], calculatedValues[3]); |
| 203 | + |
| 204 | + Serial.print("Error(%) "); |
| 205 | + Serial.print(SensorName); |
| 206 | + Serial.print(": "); |
| 207 | + Serial.print(error[0]); |
| 208 | + Serial.print(" "); |
| 209 | + Serial.print(error[1]); |
| 210 | + Serial.print(" "); |
| 211 | + Serial.print(error[2]); |
| 212 | + Serial.print(" "); |
| 213 | + Serial.print(error[3]); |
| 214 | + Serial.print(" | Calculated(PPM): "); |
| 215 | + Serial.print(calculatedValues[0]); |
| 216 | + Serial.print(" "); |
| 217 | + Serial.print(calculatedValues[1]); |
| 218 | + Serial.print(" "); |
| 219 | + Serial.print(calculatedValues[2]); |
| 220 | + Serial.print(" "); |
| 221 | + Serial.print(calculatedValues[3]); |
| 222 | + Serial.print(" | Expected(PPM): "); |
| 223 | + Serial.print(expectedValue[0]); |
| 224 | + Serial.print(" "); |
| 225 | + Serial.print(expectedValue[1]); |
| 226 | + Serial.print(" "); |
| 227 | + Serial.print(expectedValue[2]); |
| 228 | + Serial.print(" "); |
| 229 | + Serial.print(expectedValue[3]); |
| 230 | + Serial.println(" | "); |
| 231 | +} |
| 232 | + |
| 233 | +double calculatePercentualError(double expectedValue, double calculatedValue) |
| 234 | +{ |
| 235 | + // Return the diference between two measures |
| 236 | + return abs(calculatedValue - expectedValue) / expectedValue; |
| 237 | +} |
0 commit comments