1 /* SFE_BMP180 library example sketch
3 This sketch shows how to use the SFE_BMP180 library to read the
4 Bosch BMP180 barometric pressure sensor.
5 https://www.sparkfun.com/products/11824
7 Like most pressure sensors, the BMP180 measures absolute pressure.
8 This is the actual ambient pressure seen by the device, which will
9 vary with both altitude and weather.
11 Before taking a pressure reading you must take a temparture reading.
12 This is done with startTemperature() and getTemperature().
13 The result is in degrees C.
15 Once you have a temperature reading, you can take a pressure reading.
16 This is done with startPressure() and getPressure().
17 The result is in millibar (mb) aka hectopascals (hPa).
19 If you'll be monitoring weather patterns, you will probably want to
20 remove the effects of altitude. This will produce readings that can
21 be compared to the published pressure readings from other locations.
22 To do this, use the sealevel() function. You will need to provide
23 the known altitude at which the pressure was measured.
25 If you want to measure altitude, you will need to know the pressure
26 at a baseline altitude. This can be average sealevel pressure, or
27 a previous pressure reading at your altitude, in which case
28 subsequent altitude readings will be + or - the initial baseline.
29 This is done with the altitude() function.
36 (WARNING: do not connect + to 5V or the sensor will be damaged!)
38 You will also need to connect the I2C pins (SCL and SDA) to your
39 Arduino. The pins are different on different Arduinos:
41 Any Arduino pins labeled: SDA SCL
42 Uno, Redboard, Pro: A4 A5
46 Leave the IO (VDDIO) pin unconnected. This pin is for connecting
47 the BMP180 to systems with lower logic levels such as 1.8V
49 Have fun! -Your friends at SparkFun.
51 The SFE_BMP180 library uses floating-point equations developed by the
52 Weather Station Data Logger project: http://wmrx00.sourceforge.net/
54 Our example code uses the "beerware" license. You can do anything
55 you like with this code. No really, anything. If you find it useful,
56 buy me a beer someday.
58 V10 Mike Grusin, SparkFun Electronics 10/24/2013
61 // Your sketch must #include this library, and the Wire library.
62 // (Wire is a standard library included with Arduino.):
64 #include <SFE_BMP180.h>
68 // You will need to create an SFE_BMP180 object, here called "pressure":
72 // DHT object for humidity sensor
78 #define START_DELAY 20000
83 #define READ_SAMPLE_INTERVAL 50
84 #define READ_SAMPLE_TIMES 5
86 #define MQ135_RES 1000
88 float MQRead(int mq_pin)
95 for (i=0;i<READ_SAMPLE_TIMES;i++) {
96 rr = analogRead(mq_pin);
98 delay(READ_SAMPLE_INTERVAL);
101 rs = rs/READ_SAMPLE_TIMES;
106 /// Parameters to model temperature and humidity dependence
113 float getMQ135CorrectionFactor(float t, float h) {
114 return CORA * t * t - CORB * t + CORC - (h-33.)*CORD;
118 #define PARA 116.6020682
119 #define PARB 2.769034857
121 float getMQ135ppm(float Resistance) {
122 return PARA * pow((Resistance/RZERO), -PARB);
129 Serial1.begin(57600);
130 Serial1.println("START");
132 // Initialize the sensor (it is important to get calibration values stored on the device).
134 if (pressure.begin()) {
135 Serial1.println("BMP180 init success");
138 Serial1.println("BMP180 init fail\n\n");
144 pinMode(GAS_PIN,INPUT);
145 pinMode(CO2_PIN,INPUT);
147 digitalWrite(GAS_PIN, LOW);
148 digitalWrite(CO2_PIN, LOW);
160 byte GotTemperature,GotPressure;
162 // Loop here getting pressure readings every 60 seconds.
169 status = pressure.startTemperature();
171 // Wait for the measurement to complete:
174 // Retrieve the completed temperature measurement:
175 // Note that the measurement is stored in the variable T.
176 // Function returns 1 if successful, 0 if failure.
178 status = pressure.getTemperature(T);
183 Serial1.println("ERROR:TYPE=BMP180,MESSAGE=FAILED MEASURE TEMPERATURE\n");
186 Serial1.println("ERROR:TYPE=BMP180,MESSAGE=FAILED START TEMPERATURE MEASUREMENT\n");
189 // Start a pressure measurement:
190 // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
191 // If request is successful, the number of ms to wait is returned.
192 // If request is unsuccessful, 0 is returned.
194 status = pressure.startPressure(3);
196 // Wait for the measurement to complete:
199 // Retrieve the completed pressure measurement:
200 // Note that the measurement is stored in the variable P.
201 // Note also that the function requires the previous temperature measurement (T).
202 // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
203 // Function returns 1 if successful, 0 if failure.
205 status = pressure.getPressure(P,LastTemp);
207 // Print out the measurement:
211 Serial1.println("ERROR:TYPE=BMP180,MESSAGE=FAILED MEASURE PRESSURE\n");
214 Serial1.println("ERROR:TYPE=BMP180,MESSAGE=FAILED START PRESSURE MEASUREMENT\n");
216 if (GotPressure || GotTemperature) {
217 Serial1.print("SENSOR:TYPE=BMP180");
218 if (GotPressure) { Serial1.print(",PRESSURE="); Serial1.print(P); }
219 if (GotTemperature) { Serial1.print(",TEMPERATURE="); Serial1.print(T); }
224 delay(dht.getMinimumSamplingPeriod());
226 DHT_H = dht.getHumidity();
227 DHT_T = dht.getTemperature();
229 DHTStatus=dht.getStatus();
231 if (DHTStatus == 0) {
232 Serial1.print("SENSOR:TYPE=DHT22,TEMPERATURE=");
233 Serial1.print(DHT_T);
234 Serial1.print(",HUMIDITY=");
235 Serial1.println(DHT_H);
237 Serial1.print("ERROR:TYPE=DHT22,MESSAGE=");
238 Serial1.println(dht.getStatusString());
241 Gas = MQRead(GAS_PIN);
243 Serial1.print("SENSOR:TYPE=MQ4,VALUE=");
244 Serial1.println(Gas);
246 float MQ135_Resistance;
250 float CO2_resistance;
252 CO2_raw = MQRead(CO2_PIN);
253 Serial1.print("DEBUG:RAW=");
254 Serial1.println(CO2_raw);
256 MQ135_Resistance=MQ135_RES*(1023./CO2_raw-1);
258 Serial1.print("DEBUG:RESISTANCE=");
259 Serial1.println(MQ135_Resistance);
261 MQ135_Resistance=MQ135_Resistance/getMQ135CorrectionFactor(DHT_T,DHT_H);
262 Serial1.print("DEBUG:CORR_RESISTANCE=");
263 Serial1.println(MQ135_Resistance);
265 CO2_ppm = getMQ135ppm(MQ135_Resistance);
267 Serial1.print("SENSOR:TYPE=MQ135,VALUE=");
268 Serial1.println(CO2_ppm);
270 delay(DELAY); // Pause for 50 seconds.