Skip to content

Instantly share code, notes, and snippets.

@ghtomcat
Last active September 7, 2021 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghtomcat/31544ddf7a34f2217c45acec5cca365f to your computer and use it in GitHub Desktop.
Save ghtomcat/31544ddf7a34f2217c45acec5cca365f to your computer and use it in GitHub Desktop.
/**************************************************************************
read bme280 data and display on ssd1306
**************************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_BME280 bme; // I2C
volatile byte revolutions;
// interrupt routine to count revolutions
void IRAM_ATTR isr() {
revolutions += 1;
}
void setup() {
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
unsigned status;
// default settings
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}
// attach pin 14 to interrupt routine
pinMode(14, INPUT);
attachInterrupt(14, isr, FALLING);
}
void loop() {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0);
display.print("Temperatur: ");
display.setCursor(70,0);
display.print(bme.readTemperature());
display.setCursor(100,0);
display.print(" *C");
display.setCursor(0,10);
display.print("Luftdruck: ");
display.setCursor(65,10);
display.print(bme.readPressure() / 100.0F);
display.setCursor(100,10);
display.print(" hPa");
display.setCursor(0,20);
display.print("Feuchtigkeit: ");
display.setCursor(80,20);
display.print(bme.readHumidity());
display.setCursor(110,20);
display.print(" %");
display.setCursor(0,30);
display.print("Wind: ");
display.setCursor(50,30);
display.print(revolutions*6*16*0.001885); // revolutions in 60sec to km/h at 16cm diameter
display.setCursor(80,30);
display.print(" km/h");
display.display();
revolutions=0;
// wait 10s
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment