![](/media/cache/profiles/pista-2018.jpg.50x50_q85.jpg)
BME280 I2C sensor with OLED display
Dependencies: mbed OLED_SSD1306_SH1106 BME280
main.cpp@0:3a9b184392a0, 2022-02-03 (annotated)
- Committer:
- cspista
- Date:
- Thu Feb 03 14:52:47 2022 +0000
- Revision:
- 0:3a9b184392a0
final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cspista | 0:3a9b184392a0 | 1 | #include "mbed.h" |
cspista | 0:3a9b184392a0 | 2 | #include "BME280.h" |
cspista | 0:3a9b184392a0 | 3 | #include "Adafruit_SSD1306.h" |
cspista | 0:3a9b184392a0 | 4 | #include "Adafruit_SSD1306.h" |
cspista | 0:3a9b184392a0 | 5 | |
cspista | 0:3a9b184392a0 | 6 | I2C i2c(D14,D15); |
cspista | 0:3a9b184392a0 | 7 | |
cspista | 0:3a9b184392a0 | 8 | BME280 sensor(i2c, 0x76<<1); |
cspista | 0:3a9b184392a0 | 9 | Adafruit_SH1106_I2c oled(i2c, NC, 0x78, 64, 128); // SH1106 I2C 128x64, with no reset pin |
cspista | 0:3a9b184392a0 | 10 | // Adafruit_SSD1306_I2c oled(i2c, NC, 0x78, 64, 128); // SSD1306 I2C 128x64, with no reset pin |
cspista | 0:3a9b184392a0 | 11 | // Adafruit_SSD1306_I2c oled(i2c, NC, 0x78, 32, 128); // SSD1306 I2C 128x32, with no reset pin |
cspista | 0:3a9b184392a0 | 12 | |
cspista | 0:3a9b184392a0 | 13 | int main() |
cspista | 0:3a9b184392a0 | 14 | { |
cspista | 0:3a9b184392a0 | 15 | char fok = 9; |
cspista | 0:3a9b184392a0 | 16 | i2c.frequency(400000); |
cspista | 0:3a9b184392a0 | 17 | oled.setRotation(0); |
cspista | 0:3a9b184392a0 | 18 | oled.clearDisplay(); |
cspista | 0:3a9b184392a0 | 19 | oled.setTextColor(WHITE); |
cspista | 0:3a9b184392a0 | 20 | oled.setTextSize(2); |
cspista | 0:3a9b184392a0 | 21 | oled.setTextCursor(10,8); |
cspista | 0:3a9b184392a0 | 22 | oled.printf("BME280 \r\n demo"); |
cspista | 0:3a9b184392a0 | 23 | oled.display(); |
cspista | 0:3a9b184392a0 | 24 | wait(5.0); |
cspista | 0:3a9b184392a0 | 25 | while(1) { |
cspista | 0:3a9b184392a0 | 26 | float tempC = sensor.getTemperature(); |
cspista | 0:3a9b184392a0 | 27 | float pressure = sensor.getPressure(); |
cspista | 0:3a9b184392a0 | 28 | float humidity = sensor.getHumidity(); |
cspista | 0:3a9b184392a0 | 29 | oled.clearDisplay(); |
cspista | 0:3a9b184392a0 | 30 | oled.setTextCursor(20,4); |
cspista | 0:3a9b184392a0 | 31 | oled.printf("%.1f %cC",tempC,fok); |
cspista | 0:3a9b184392a0 | 32 | oled.setTextCursor(8,24); |
cspista | 0:3a9b184392a0 | 33 | oled.printf("%.1f hPa",pressure/0.985); |
cspista | 0:3a9b184392a0 | 34 | oled.setTextCursor(20,44); |
cspista | 0:3a9b184392a0 | 35 | oled.printf("%.0f %%",humidity); |
cspista | 0:3a9b184392a0 | 36 | oled.display(); |
cspista | 0:3a9b184392a0 | 37 | wait(2); |
cspista | 0:3a9b184392a0 | 38 | } |
cspista | 0:3a9b184392a0 | 39 | } |