// Install from the Library Manager: // "Adafruit BME280 Library" (with the // extra libraries it asks for) and // "LiquidCrystal I2C" #include #include #include // The button that turns the page const int BUTTON_PIN = 2; // Time between two readings (ms) const unsigned long READ_EVERY = 2000; // Sea-level pressure where you are (hPa): // put in the pressure from the weather // forecast for a truer altitude const float SEA_LEVEL_HPA = 1013.25; // I2C address: 0x27 on most, 0x3F on some LiquidCrystal_I2C lcd(0x27, 16, 2); Adafruit_BME280 bme; // Pages: temperature, humidity, pressure, // altitude const int PAGES = 4; int page = 0; float temperature = 0; float humidity = 0; float pressure = 0; float altitude = 0; unsigned long lastRead = 0; bool lastButton = HIGH; unsigned long lastChange = 0; // Writes a row and blanks the rest of it // (no flicker) void printLine(int row, const char *text) { lcd.setCursor(0, row); int n = 0; while (text[n] && n < 16) lcd.print(text[n++]); while (n++ < 16) lcd.print(' '); } // The sensor's id: 0x60 on a BME280, // 0x58 on a BMP280, 0 if none answers byte chipId(byte address) { Wire.beginTransmission(address); Wire.write(0xD0); if (Wire.endTransmission() != 0) return 0; Wire.requestFrom((int)address, 1); return Wire.available() ? Wire.read() : 0; } // Says why the sensor was not found void showSensorError() { byte id = chipId(0x76); if (id == 0) id = chipId(0x77); if (id == 0x58) { printLine(0, "This is a BMP280"); printLine(1, "No humidity"); Serial.println("The sensor is a BMP280: it has no humidity, a BME280 is needed."); } else { printLine(0, "No sensor"); printLine(1, "Check the wires"); Serial.println("BME280 not found: check the SDA, SCL, VIN and GND wires."); } } // Address 0x76 or 0x77, by the module bool startSensor() { return bme.begin(0x76) || bme.begin(0x77); } void readSensor() { bme.takeForcedMeasurement(); temperature = bme.readTemperature(); humidity = bme.readHumidity(); pressure = bme.readPressure() / 100.0; // Pressure falls as you go up (near sea // level 1 hPa every ~8 m): the altitude altitude = 44330.0 * (1.0 - pow(pressure / SEA_LEVEL_HPA, 0.1903)); Serial.print("Temperature: "); Serial.print(temperature, 1); Serial.print(" °C Humidity: "); Serial.print(humidity, 0); Serial.print("% Pressure: "); Serial.print(pressure, 1); Serial.print(" hPa Altitude: "); Serial.print(altitude, 0); Serial.println(" m"); } void showPage() { // Top row: the page's name and number const char *titles[PAGES] = {"Temperature", "Humidity", "Pressure", "Altitude"}; char top[17]; snprintf(top, sizeof(top), "%-12s%d/%d", titles[page], page + 1, PAGES); printLine(0, top); // Bottom row: the value and its unit char value[10]; char bottom[17]; if (page == 0) { dtostrf(temperature, 1, 1, value); // \xDF is the display's degree sign snprintf(bottom, sizeof(bottom), "%s \xDF" "C", value); } else if (page == 1) { dtostrf(humidity, 1, 0, value); snprintf(bottom, sizeof(bottom), "%s%%", value); } else if (page == 2) { dtostrf(pressure, 1, 1, value); snprintf(bottom, sizeof(bottom), "%s hPa", value); } else { dtostrf(altitude, 1, 0, value); snprintf(bottom, sizeof(bottom), "about %s m", value); } printLine(1, bottom); } void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); lcd.init(); lcd.backlight(); // No sensor: keeps trying until one is // found, so fixing a wire starts it while (!startSensor()) { showSensorError(); delay(2000); } // Weather-station setting: one reading // when asked, asleep in between, so it // does not warm up and read high bme.setSampling(Adafruit_BME280::MODE_FORCED, Adafruit_BME280::SAMPLING_X1, Adafruit_BME280::SAMPLING_X1, Adafruit_BME280::SAMPLING_X1, Adafruit_BME280::FILTER_OFF); readSensor(); showPage(); lastRead = millis(); } void loop() { // A press turns to the next page bool button = digitalRead(BUTTON_PIN); if (button != lastButton && millis() - lastChange > 30) { lastChange = millis(); lastButton = button; if (button == LOW) { page = (page + 1) % PAGES; showPage(); } } if (millis() - lastRead >= READ_EVERY) { lastRead = millis(); readSensor(); showPage(); } }