bme280test.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /***************************************************************************
  2. This is a library for the BME280 humidity, temperature & pressure sensor
  3. Designed specifically to work with the Adafruit BME280 Breakout
  4. ----> http://www.adafruit.com/products/2652
  5. These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  6. to interface. The device's I2C address is either 0x76 or 0x77.
  7. Adafruit invests time and resources providing this open source code,
  8. please support Adafruit andopen-source hardware by purchasing products
  9. from Adafruit!
  10. Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  11. BSD license, all text above must be included in any redistribution
  12. See the LICENSE file for details.
  13. ***************************************************************************/
  14. #include <Wire.h>
  15. #include <SPI.h>
  16. #include <Adafruit_Sensor.h>
  17. #include <Adafruit_BME280.h>
  18. #define BME_SCK 13
  19. #define BME_MISO 12
  20. #define BME_MOSI 11
  21. #define BME_CS 10
  22. #define SEALEVELPRESSURE_HPA (1013.25)
  23. Adafruit_BME280 bme; // I2C
  24. //Adafruit_BME280 bme(BME_CS); // hardware SPI
  25. //Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
  26. unsigned long delayTime;
  27. void setup() {
  28. Serial.begin(9600);
  29. while(!Serial); // time to get serial running
  30. Serial.println(F("BME280 test"));
  31. unsigned status;
  32. // default settings
  33. status = bme.begin();
  34. // You can also pass in a Wire library object like &Wire2
  35. // status = bme.begin(0x76, &Wire2)
  36. if (!status) {
  37. Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
  38. Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
  39. Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
  40. Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
  41. Serial.print(" ID of 0x60 represents a BME 280.\n");
  42. Serial.print(" ID of 0x61 represents a BME 680.\n");
  43. while (1) delay(10);
  44. }
  45. Serial.println("-- Default Test --");
  46. delayTime = 1000;
  47. Serial.println();
  48. }
  49. void loop() {
  50. printValues();
  51. delay(delayTime);
  52. }
  53. void printValues() {
  54. Serial.print("Temperature = ");
  55. Serial.print(bme.readTemperature());
  56. Serial.println(" °C");
  57. Serial.print("Pressure = ");
  58. Serial.print(bme.readPressure() / 100.0F);
  59. Serial.println(" hPa");
  60. Serial.print("Approx. Altitude = ");
  61. Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  62. Serial.println(" m");
  63. Serial.print("Humidity = ");
  64. Serial.print(bme.readHumidity());
  65. Serial.println(" %");
  66. Serial.println();
  67. }