In this video lesson we take you through the project of building an Arduino Barometric Pressure Weather Station. It builds on the last two lessons in this series. We measure and display instantaneous Barometric Pressure, normalized to Sea Level. Then in the lower portion on the OLED SSD1306 display, we show a graph of the barometric pressure over the last 24 hours.
Below we include the code we develop in the above video. Make sure to set the ‘alt’ variable to your elevation at your location, in meters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
#include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <Adafruit_BMP085.h> int lastUpdate = 0; int updateInterval = 600000; float rawXdata[128]; int normXdata[128]; float rawYdata[128]; int normYdata[128]; float xMin = 0; float xMax = 127; float yMin = 28; float yMax = 31; float Pt = 14; float Pb = 63; float Pl = 0; float Pr = 127; int numPoints = 128; Adafruit_BMP085 BP; bool BMPconnected = false; float BarPress = 30; float BarPressNow = 30; float alt = 1127; int screenWidth = 128; int screenHeight = 64; int oledReset = -1; #define SCREEN_ADDRESS 0x3C Adafruit_SSD1306 oled(screenWidth, screenHeight, &Wire, oledReset); void setup() { // put your setup code here, to run once: Serial.begin(9600); BMPconnected = BP.begin(); if (BMPconnected == true) { Serial.println("BMP180 found and Connected"); } if (BMPconnected == false) { Serial.println("BMP180 not found, Check Connections"); } oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); oled.clearDisplay(); oled.display(); for (int i = 0; i < numPoints; i= i + 1) { rawXdata[i] = i; } } void loop() { // put your main code here, to run repeatedly: BarPressNow = normPress(alt); BarPress = .95 * BarPress + .05 * BarPressNow; if (millis() - lastUpdate >= updateInterval) { for (int i = 0; i < numPoints - 1; i = i + 1) { rawYdata[i] = rawYdata[i + 1]; } lastUpdate=millis(); rawYdata[numPoints-1]=BarPress; normData(); oled.clearDisplay(); plotGraph(); } oled.clearDisplay(); oled.setCursor(0, 0); oled.setTextColor(WHITE); oled.setTextSize(2); oled.print(BarPress); oled.setTextSize(1); oled.println(" In HG"); normData(); plotGraph(); oled.display(); delay(.1); } float normPress(float elev) { float BPRaw; float BPNorm; float T0 = 288.15; float L = .0065; float R = 8.3144598; float M = .0289644; float g = 9.8; float c = M * g / (R * L); BPRaw = BP.readPressure() * .0002953; BPNorm = BPRaw / pow(1 - L * elev / T0, c); return BPNorm; } void normData() { for (int i = 0; i < numPoints; i = i + 1) { normYdata[i] = (Pb - Pt) / (yMin - yMax) * (rawYdata[i] - yMax) + Pt; normXdata[i] = (Pr - Pl) / (xMax - xMin) * (rawXdata[i] - xMin) + Pl; } } void plotGraph() { oled.drawLine(Pl, Pt, Pl, Pb, WHITE); oled.drawLine(Pl, Pb, Pr, Pb, WHITE); int MID = (Pb - Pt) / (yMin - yMax) * (29.92 - yMax) + Pt; oled.drawLine(Pl, MID, Pr, MID, WHITE); for (int i = 0; i < numPoints - 1; i = i + 1) { oled.drawLine(normXdata[i], normYdata[i], normXdata[i + 1], normYdata[i + 1], WHITE); } } |