The next thing to add is a barometric pressure sensor which will allow our experiment to measure
pressure or altitude. The first thing we need to do is install a libraries, like a package that gives the code more capabilities.
Please note, we now have two types of sensors that are almost identical, but use different libraries. Please select the correct one below.
Using BMP180: In the Arduino IDE, click Sketch >> Include Library >> Manage Libraries and allow the window time to full load. In the Search bar at the top, type "BMP180" and the first result should be AdaFruit (see below) click on it once, then click Install.
Using BMP280: Download this zip file to your computer. In the Arduino IDE, click Sketch >> Include Library >> Add .ZIP Library and select the zip file you just downloaded.
Then use this example code for BMP180:
#include <Wire.h>
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;
void setup() {
Serial.begin(9600);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
}
void loop() {
Serial.print("Temperature = ");
Serial.print(bmp.readTemperature());
Serial.println(" *C");
// Calculate altitude assuming 'standard' barometric
// pressure of 1013.25 millibar = 101325 Pascal
Serial.print("Altitude = ");
Serial.print(bmp.readAltitude());
Serial.println(" meters");
// you can get a more precise measurement of altitude
// if you know the current sea level pressure which will
// vary with weather and such. If it is 1015 millibars
// that is equal to 101500 Pascals.
Serial.print("Real altitude = ");
Serial.print(bmp.readAltitude(101500));
Serial.println(" meters");
Serial.println();
delay(500);
}
Use this example code for BMP280:
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Renton_BMP280.h>
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10
Renton_BMP280 bme;
void setup() {
Serial.begin(9600);
Serial.println(F("BMP280 test"));
if (!bme.begin()) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
}
void loop() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure());
Serial.println(" Pa");
Serial.print("Approx altitude = ");
Serial.print(bme.readAltitude(1013.25)); // this should be adjusted to your local forcase
Serial.println(" m");
Serial.println();
delay(2000);
}
After uploading the code, go to the Arduino IDE and click Tools >> Serial Monitor. This brings up a window where you can see the values from the sensor. Try raising or lowering the sensor to see if it changes. Can you find the current pressure so the "Real Altitude" reads 0 when the sensor is on the floor?
Connect VCC to 3.3V (NOT 5.0V!)
Connect GND to Ground
Connect SCL to Analog Pin 5 (A5 on board)
Connect SDA to Analog Pin 4 (A4 on board)