|
1 | 1 | /*
|
2 |
| - NOTE - the order of functions is important for correct |
3 |
| - calibration. |
| 2 | + # THIS SKETCH IS NOT NEEDED WITH AUTOCALIBRATION # |
| 3 | + |
| 4 | + Unlike typical sensors, calibration() refers to the zero point where CO2 is 400ppm. |
| 5 | + This 400ppm comes from the average atmospheric value of 400ppm (or atleast was). |
4 | 6 |
|
5 |
| - Where other CO2 sensors require an nitrogen atmosphere to "zero" |
6 |
| - the sensor CO2 reference, the MHZ19 "zero" (confusingly) refers to the |
7 |
| - background CO2 level hardcoded into the device at 400ppm (getBackgroundCO2() |
8 |
| - sends a command to the device to retrieve the value); |
| 7 | + Depending on the sensor model, the harcoded value can usually be found by |
| 8 | + calling getBackgroundCO2(); |
9 | 9 |
|
10 |
| - The best start for your sensor is to wait till CO2 values are as close to background |
11 |
| - levels as possible (currently an average of ~418ppm). Usually at night time and outside |
12 |
| - if possible, otherwise when the house has been unoccupied for as long as possible such. |
| 10 | + So if you intend to manually calibrate your sensor, it's usually best to do so at |
| 11 | + night and outside after 20 minutes of run time. |
| 12 | + |
| 13 | + Instead if you're using autocalibration, then the sensor takes the lowest value observed |
| 14 | + in the last 24 hours and adjusts it's self accordingly over a few weeks. |
13 | 15 |
|
14 | 16 | HOW TO USE:
|
15 | 17 |
|
16 | 18 | ----- Hardware Method -----
|
17 | 19 | By pulling the zero HD low (0V) for 7 Secs as per the datasheet.
|
18 | 20 |
|
19 | 21 | ----- Software Method -----
|
20 |
| - Run this example while in an ideal environment (I.e. restarting the device). Once |
21 |
| - restarted, disconnect MHZ19 from device and upload a new sketch to avoid |
22 |
| - recalibration. |
| 22 | + Run this sketch, disconnect MHZ19 from device after sketch ends (20+ minutes) and upload new |
| 23 | + code to avoid recalibration. |
23 | 24 |
|
24 | 25 | ----- Auto calibration ----
|
25 |
| - If you are using auto calibration, the sensor will adjust its self every 24 hours |
26 |
| - (note here, the auto calibration algorithm uses the lowest observe CO2 value |
27 |
| - for that set of 24 hours as the zero - so, if your device is under an environment |
28 |
| - which does not fall to these levels, consider turning this off in your setup code). |
| 26 | + As mentioned above if this is set to true, the sensor will adjust it's self over a few weeks |
| 27 | + according to the lowest observed CO2 values each day. *You don't need to run this sketch! |
29 | 28 |
|
30 |
| - If autoCalibration is set to "false", then getting the background calibration levels |
31 |
| - correct at first try is essential. |
32 | 29 | */
|
33 | 30 |
|
34 | 31 | #include <Arduino.h>
|
35 | 32 | #include "MHZ19.h"
|
36 |
| -#include <SoftwareSerial.h> // Remove if using HardwareSerial or non-uno library compatable device |
| 33 | +#include <SoftwareSerial.h> // Remove if using HardwareSerial or non-uno library compatable device |
37 | 34 |
|
38 |
| -#define RX_PIN 10 |
| 35 | +#define RX_PIN 10 |
39 | 36 | #define TX_PIN 11
|
40 |
| -#define BAUDRATE 9600 // Native to the sensor (do not change) |
| 37 | +#define BAUDRATE 9600 |
41 | 38 |
|
42 | 39 | MHZ19 myMHZ19;
|
43 | 40 | SoftwareSerial mySerial(RX_PIN, TX_PIN); // Uno example
|
44 |
| -//HardwareSerial mySerial(1); // ESP32 Example |
45 | 41 |
|
46 |
| -unsigned long getDataTimer = 0; |
| 42 | +unsigned long timeElapse = 0; |
47 | 43 |
|
48 | 44 | void verifyRange(int range);
|
49 | 45 |
|
50 | 46 | void setup()
|
51 | 47 | {
|
52 | 48 | Serial.begin(9600);
|
53 | 49 |
|
54 |
| - mySerial.begin(BAUDRATE); // Uno example: Begin Stream with MHZ19 baudrate |
55 |
| - //mySerial.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN); // ESP32 Example |
56 |
| - |
57 |
| - myMHZ19.begin(mySerial); // *Important, Pass your Stream reference |
58 |
| - |
59 |
| - /* ### setRange(value)### |
60 |
| - Basic: |
61 |
| - setRange(value) - set range to value (advise 2000 or 5000). |
62 |
| - setRange() - set range to 2000. |
63 |
| -
|
64 |
| - Advanced: |
65 |
| - Use verifyRange(int range) from this code at the bottom. |
66 |
| - */ |
67 |
| - |
68 |
| - myMHZ19.setRange(2000); |
69 |
| - |
70 |
| - /* ###calibrateZero()### |
71 |
| - Basic: |
72 |
| - calibrateZero() - request zero calibration |
73 |
| -
|
74 |
| - Advanced: |
75 |
| - In Testing. |
76 |
| - */ |
77 |
| - |
78 |
| - myMHZ19.calibrateZero(); |
79 |
| - |
80 |
| - /* ### setSpan(value)### |
81 |
| - Basic: |
82 |
| - setSpan(value) - set span to value (strongly recommend 2000) |
83 |
| - setSpan() - set span to 2000; |
84 |
| -
|
85 |
| - */ |
86 |
| - |
87 |
| - myMHZ19.setSpan(2000); |
| 50 | + mySerial.begin(BAUDRATE); // sensor serial |
| 51 | + myMHZ19.begin(mySerial); // pass to library |
88 | 52 |
|
89 |
| - /* ###autoCalibration(false)### |
90 |
| - Basic: |
91 |
| - autoCalibration(false) - turns auto calibration OFF. (automatically sent before defined period elapses) |
92 |
| - autoCalibration(true) - turns auto calibration ON. |
93 |
| - autoCalibration() - turns auto calibration ON. |
94 |
| -
|
95 |
| - Advanced: |
96 |
| - autoCalibration(true, 12) - turns autocalibration ON and calibration period to 12 hrs (maximum 24hrs). |
97 |
| - */ |
98 |
| - |
99 |
| - myMHZ19.autoCalibration(false); |
100 |
| - Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF"); |
101 |
| - |
| 53 | + myMHZ19.autoCalibration(false); // make sure auto calibration is off |
| 54 | + Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF"); // now print it's status |
| 55 | + |
| 56 | + Serial.println("Waiting 20 minutes to stabalise..."); |
| 57 | + /* if you don't need to wait (it's already been this amount of time), remove the next 2 lines */ |
| 58 | + timeElapse = 12e5; // 20 minutes in milliseconds |
| 59 | + while(millis() < timeElapse) {}; // wait this duration |
| 60 | + |
| 61 | + Serial.println("Calibrating.."); |
| 62 | + myMHZ19.calibrate(); // Take a reading which be used as the zero point for 400 ppm |
| 63 | + |
102 | 64 | }
|
103 | 65 |
|
104 | 66 | void loop()
|
105 | 67 | {
|
106 |
| - if (millis() - getDataTimer >= 2000) // Check if interval has elapsed (non-blocking delay() equivilant) |
| 68 | + if (millis() - timeElapse >= 2000) // Check if interval has elapsed (non-blocking delay() equivilant) |
107 | 69 | {
|
108 | 70 | int CO2;
|
109 | 71 | CO2 = myMHZ19.getCO2();
|
110 | 72 |
|
111 | 73 | Serial.print("CO2 (ppm): ");
|
112 | 74 | Serial.println(CO2);
|
113 | 75 |
|
114 |
| - int8_t Temp; // Buffer for temperature |
115 |
| - Temp = myMHZ19.getTemperature(); // Request Temperature (as Celsius) |
| 76 | + int8_t Temp; // Buffer for temperature |
| 77 | + Temp = myMHZ19.getTemperature(); // Request Temperature (as Celsius) |
116 | 78 |
|
117 | 79 | Serial.print("Temperature (C): ");
|
118 | 80 | Serial.println(Temp);
|
119 | 81 |
|
120 |
| - getDataTimer = millis(); // Update inerval |
| 82 | + timeElapse = millis(); // Update inerval |
121 | 83 | }
|
122 | 84 | }
|
123 |
| - |
124 |
| -void verifyRange(int range) |
125 |
| -{ |
126 |
| - Serial.println("Requesting new range."); |
127 |
| - |
128 |
| - myMHZ19.setRange(range); // request new range write |
129 |
| - |
130 |
| - if (myMHZ19.getRange() == range) // Send command to device to return it's range value. |
131 |
| - Serial.println("Range successfully applied."); // Success |
132 |
| - |
133 |
| - else |
134 |
| - Serial.println("Failed to apply range."); // Failed |
135 |
| -} |
0 commit comments