Weather station with BME280 and OLED screen
An updated version of my previous project with OLED screen
Components and supplies
1
OLED SSD1306
1
BME280/BMP280
1
Pushbutton
1
Arduino Nano
Apps and platforms
1
Cirkit Designer
1
Arduino IDE
Project description
Code
Arduino Nano weather station with OLED screen
cpp
1/*This code is about a weather station consisting of an Arduino Nano, BME280, OLED 128x64 and a pushbutton. 2Sensor BME280 and OLED screen are connected with I2C protocol. Library <ezButton> is used for checking pushbutton. 3Measurements are made every 10 seconds and the values are rounded. 4Starting screen is temperature and if you press button once display shows humidity. One more press and display shows pressure. 5With the next press display shows sequentially for three seconds temperature, after humidity and at the end pressure. 6If we press one more time pushbutton displays begins showing temperature again. 7 8*/ 9 10#include <Wire.h> 11#include <Adafruit_GFX.h> 12#include <Adafruit_SSD1306.h> 13#include <Adafruit_Sensor.h> 14#include <Adafruit_BME280.h> 15#include <ezButton.h> 16 17 18float temp; //VARIABLE FOR MEASURING TEMPERATURE 19float hum; //VARIABLE FOR MEASURING HUMIDITY 20float pressure; //VARIABLE FOR MEASURING PRESSURE 21 22float roundtemp; //VARIABLE FOR ROUNDING TEMPERATURE 23float roundhum; //VARIABLE FOR ROUNDING HUMIDITY 24int roundpressure; //VARIABLE FOR ROUNDING PRESSURE 25 26int i = 0; //VARIABLE HELPING FOR AUTO SEQUENCE DISPLAY 27 28unsigned long prevTime = 0; 29long intervalSample = 10000; //TIME PERIOD FOR MEASURING TEMPERATURE, HUMIDITY, PRESSURE 30 31unsigned long prevT2 = 0; 32long intervalShow = 3000; //TIME PERIOD FOR SHOWING MEASUREMENTS IN SEQUENCE 33 34 35int toprint = 0; //VARIABLE FOR SELECTING WHAT TO PRINT ON OLED SCREEN 36 //0 = TEMP 1=HUM 2=PRESSURE 3=AUTO SEQUENCE 37 38 39ezButton button1(6); // create ezButton object that attach to pin 6; 40 41#define SCREEN_WIDTH 128 // OLED display width, in pixels 42#define SCREEN_HEIGHT 64 // OLED display height, in pixels 43 44 45#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) 46Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 47 48Adafruit_BME280 bme; // I2C 49 //Adafruit_BMP280 bme(BMP_CS); // hardware SPI 50 //Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK); 51 52void setup() { 53 54 button1.setDebounceTime(50); // set debounce time to 50 milliseconds 55 56 if (!bme.begin()) { 57 Serial.println("Could not find a valid BMP280 sensor, check wiring!"); 58 while (1) 59 ; 60 } 61 62 63 // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally 64 if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 65 Serial.println(F("SSD1306 allocation failed")); 66 for (;;) 67 ; // Don't proceed, loop forever 68 } 69 display.display(); //display initial Adafruit logo 70 71 delay(2000); 72 73 // Clear the buffer 74 display.clearDisplay(); 75 display.display(); 76} 77 78void loop() { 79 80 button1.loop(); // MUST call the loop() function first 81 unsigned long currentTime = millis(); 82 83 if (currentTime - prevTime >= intervalSample) { //GET MEASUREMENTS EVERY 10 SECONDS 84 85 hum = bme.readHumidity(); //READ, STORE AND ROUND HUMIDITY 86 delay(10); 87 hum = hum + 0.05; 88 hum = hum * 10; 89 int newhum = (int)hum; 90 roundhum = (float)newhum / 10; 91 92 temp = bme.readTemperature(); //READ, STORE AND ROUND TEMPERATURE 93 delay(10); 94 temp = temp + 0.05; 95 temp = temp * 10; 96 int newtemp = (int)temp; 97 roundtemp = (float)newtemp / 10; 98 99 pressure = bme.readPressure() / 100; //READ, STORE AND ROUND BAROMETRIC PRESSURE 100 delay(10); 101 pressure = pressure + 0.5; 102 roundpressure = (int)pressure; 103 prevTime = currentTime; 104 } 105 106 if (button1.isPressed()) //CHECK IF BUTTON IS PRESSED 107 toprint = toprint + 1; 108 if (toprint == 4) { 109 toprint = 0; 110 } 111 112 switch (toprint) { 113 case 0: 114 standartext(); 115 temperaturetext(); //PRINT TEMPERATURE 116 display.clearDisplay(); 117 break; 118 119 case 1: 120 standartext(); 121 humiditytext(); //PRINT HUMIDITY 122 display.clearDisplay(); 123 break; 124 125 case 2: 126 standartext(); 127 pressuretext(); //PRINT BAROMETRIC PRESSURE 128 display.clearDisplay(); 129 break; 130 131 case 3: 132 if (i == 0) { 133 standartext(); 134 humiditytext(); //PRINT HUMIDITY 135 display.clearDisplay(); 136 } 137 138 if (i == 1) { 139 standartext(); 140 temperaturetext(); //PRINT TEMPERATURE 141 display.clearDisplay(); 142 } 143 144 if (i == 2) { 145 standartext(); 146 pressuretext(); //PRINT PRESSURE 147 display.clearDisplay(); 148 } 149 if (currentTime - prevT2 >= intervalShow) { 150 i = i + 1; 151 prevT2 = currentTime; 152 if (i == 3) { 153 i = 0; 154 } 155 } 156 break; 157 } 158} 159 160void standartext() { 161 162 display.fillRect(0, 0, 128, 16, SSD1306_BLACK); 163 display.fillRect(0, 34, 128, 10, SSD1306_WHITE); 164 165 display.setCursor(22, 1); 166 display.setTextSize(2); 167 display.setTextColor(SSD1306_WHITE); 168 display.println("WEATHER"); 169 170 display.setCursor(22, 18); 171 display.setTextSize(2); 172 display.setTextColor(SSD1306_WHITE); 173 display.println("STATION"); 174} 175 176void temperaturetext() { 177 178 display.setCursor(29, 35); 179 display.setTextSize(1); 180 display.setTextColor(BLACK, WHITE); 181 display.println("Temperature"); 182 183 display.setCursor(2, 48); 184 display.setTextSize(2); 185 display.setTextColor(SSD1306_WHITE); 186 display.println("T="); 187 188 display.setCursor(27, 48); 189 display.setTextSize(2); 190 display.setTextColor(SSD1306_WHITE); 191 display.println(roundtemp); 192 193 display.drawCircle(92, 50, 2, WHITE); 194 display.setCursor(97, 48); 195 display.setTextSize(2); 196 display.setTextColor(SSD1306_WHITE); 197 display.println("C"); 198 199 display.display(); 200} 201 202void humiditytext() { 203 204 display.setCursor(29, 35); 205 display.setTextSize(1); 206 display.setTextColor(BLACK, WHITE); 207 display.println("Humidity "); 208 209 display.setCursor(2, 48); 210 display.setTextSize(2); 211 display.setTextColor(SSD1306_WHITE); 212 display.println("H="); 213 214 display.setCursor(27, 48); 215 display.setTextSize(2); 216 display.setTextColor(SSD1306_WHITE); 217 display.println(roundhum); 218 219 display.setCursor(92, 48); 220 display.setTextSize(2); 221 display.setTextColor(SSD1306_WHITE); 222 display.println("%"); 223 224 display.display(); 225} 226 227void pressuretext() { 228 229 display.setCursor(10, 35); 230 display.setTextSize(1); 231 display.setTextColor(BLACK, WHITE); 232 display.println("Barometric pressure"); 233 234 display.setCursor(2, 48); 235 display.setTextSize(2); 236 display.setTextColor(SSD1306_WHITE); 237 display.println("P="); 238 239 display.setCursor(27, 48); 240 display.setTextSize(2); 241 display.setTextColor(SSD1306_WHITE); 242 display.println(roundpressure); 243 244 display.setCursor(84, 48); 245 display.setTextSize(2); 246 display.setTextColor(SSD1306_WHITE); 247 display.println("hPa"); 248 249 display.display(); 250}
Comments
Only logged in users can leave comments