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
79 #define START_DELAY 20000
84 #define READ_SAMPLE_INTERVAL 50
85 #define READ_SAMPLE_TIMES 5
87 float MQRead(int mq_pin)
94 for (i=0;i<READ_SAMPLE_TIMES;i++) {
95 rr = analogRead(mq_pin);
97 delay(READ_SAMPLE_INTERVAL);
100 rs = rs/READ_SAMPLE_TIMES;
105 /// Parameters to model temperature and humidity dependence
112 Serial.begin(115200);
113 Serial.println("START");
114 Serial1.begin(57600);
115 Serial1.println("START");
117 // Initialize the sensor (it is important to get calibration values stored on the device).
119 if (pressure.begin()) {
120 Serial.println("BMP180 init success");
121 Serial1.println("BMP180 init success");
124 Serial.println("BMP180 init fail\n\n");
125 Serial1.println("BMP180 init fail\n\n");
131 pinMode(GAS_PIN,INPUT);
132 pinMode(CO2_PIN,INPUT);
134 digitalWrite(GAS_PIN, LOW);
135 digitalWrite(CO2_PIN, LOW);
150 byte GotTemperature,GotPressure;
152 // Loop here getting pressure readings every 60 seconds.
159 status = pressure.startTemperature();
161 // Wait for the measurement to complete:
164 // Retrieve the completed temperature measurement:
165 // Note that the measurement is stored in the variable T.
166 // Function returns 1 if successful, 0 if failure.
168 status = pressure.getTemperature(T);
173 Serial.println("ERROR:TYPE=BMP180,MESSAGE=FAILED MEASURE TEMPERATURE\n");
174 Serial1.println("ERROR:TYPE=BMP180,MESSAGE=FAILED MEASURE TEMPERATURE\n");
177 Serial.println("ERROR:TYPE=BMP180,MESSAGE=FAILED START TEMPERATURE MEASUREMENT\n");
178 Serial1.println("ERROR:TYPE=BMP180,MESSAGE=FAILED START TEMPERATURE MEASUREMENT\n");
181 // Start a pressure measurement:
182 // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
183 // If request is successful, the number of ms to wait is returned.
184 // If request is unsuccessful, 0 is returned.
186 status = pressure.startPressure(3);
188 // Wait for the measurement to complete:
191 // Retrieve the completed pressure measurement:
192 // Note that the measurement is stored in the variable P.
193 // Note also that the function requires the previous temperature measurement (T).
194 // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
195 // Function returns 1 if successful, 0 if failure.
197 status = pressure.getPressure(P,LastTemp);
199 // Print out the measurement:
203 Serial.println("ERROR:TYPE=BMP180,MESSAGE=FAILED MEASURE PRESSURE\n");
204 Serial1.println("ERROR:TYPE=BMP180,MESSAGE=FAILED MEASURE PRESSURE\n");
207 Serial.println("ERROR:TYPE=BMP180,MESSAGE=FAILED START PRESSURE MEASUREMENT\n");
208 Serial1.println("ERROR:TYPE=BMP180,MESSAGE=FAILED START PRESSURE MEASUREMENT\n");
210 if (GotPressure || GotTemperature) {
211 Serial.print("SENSOR:TYPE=BMP180");
212 Serial1.print("SENSOR:TYPE=BMP180");
213 if (GotPressure) { Serial.print(",PRESSURE="); Serial.print(P); Serial1.print(",PRESSURE="); Serial1.print(P); }
214 if (GotTemperature) { Serial.print(",TEMPERATURE="); Serial.print(T); Serial1.print(",TEMPERATURE="); Serial1.print(T); }
215 Serial.println(); Serial1.println();
219 delay(dht.getMinimumSamplingPeriod());
221 DHT_H = dht.getHumidity();
222 DHT_T = dht.getTemperature();
224 DHTStatus=dht.getStatus();
226 if (DHTStatus == 0) {
227 Serial.print("SENSOR:TYPE=DHT22,TEMPERATURE=");
229 Serial.print(",HUMIDITY=");
230 Serial.println(DHT_H);
231 Serial1.print("SENSOR:TYPE=DHT22,TEMPERATURE=");
232 Serial1.print(DHT_T);
233 Serial1.print(",HUMIDITY=");
234 Serial1.println(DHT_H);
236 Serial.print("ERROR:TYPE=DHT22,MESSAGE=");
237 Serial.println(dht.getStatusString());
238 Serial1.print("ERROR:TYPE=DHT22,MESSAGE=");
239 Serial1.println(dht.getStatusString());
242 Gas = MQRead(GAS_PIN);
244 Serial.print("SENSOR:TYPE=MQ4,VALUE=");
246 Serial1.print("SENSOR:TYPE=MQ4,VALUE=");
247 Serial1.println(Gas);
249 CO2_raw = MQRead(CO2_PIN);
250 Serial.print("SENSOR:TYPE=MQ135,VALUE=");
251 Serial.println(CO2_raw);
252 Serial1.print("SENSOR:TYPE=MQ135,VALUE=");
253 Serial1.println(CO2_raw);
255 CO2_MHZ_raw = MQRead(MHZ14_PIN);
256 MHZ_ppm=2000*(CO2_MHZ_raw*5.0/1023)/2.5;
257 Serial.print("SENSOR:TYPE=MHZ14,PPM=");
258 Serial.println(MHZ_ppm);
259 Serial1.print("SENSOR:TYPE=MHZ14,PPM=");
260 Serial1.println(MHZ_ppm);
262 delay(DELAY); // Pause for 50 seconds.