diff --git a/Adafruit_BMP280.cpp b/Adafruit_BMP280.cpp new file mode 100644 index 0000000..59bc4e2 --- /dev/null +++ b/Adafruit_BMP280.cpp @@ -0,0 +1,325 @@ +/*************************************************************************** + This is a library for the BMP280 pressure sensor + + Designed specifically to work with the Adafruit BMP280 Breakout + ----> http://www.adafruit.com/products/2651 + + These sensors use I2C to communicate, 2 pins are required to interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit andopen-source hardware by purchasing products + from Adafruit! + + Written by Kevin Townsend for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ***************************************************************************/ +#include "Arduino.h" +#include +#include +#include "Adafruit_BMP280.h" + + +/*************************************************************************** + PRIVATE FUNCTIONS + ***************************************************************************/ + + +Adafruit_BMP280::Adafruit_BMP280() + : _cs(-1), _mosi(-1), _miso(-1), _sck(-1) +{ } + +Adafruit_BMP280::Adafruit_BMP280(int8_t cspin) + : _cs(cspin), _mosi(-1), _miso(-1), _sck(-1) +{ } + +Adafruit_BMP280::Adafruit_BMP280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin) + : _cs(cspin), _mosi(mosipin), _miso(misopin), _sck(sckpin) +{ } + + +bool Adafruit_BMP280::begin(uint8_t a, uint8_t chipid) { + _i2caddr = a; + + if (_cs == -1) { + // i2c + Wire.begin(); + } else { + digitalWrite(_cs, HIGH); + pinMode(_cs, OUTPUT); + + if (_sck == -1) { + // hardware SPI + SPI.begin(); + } else { + // software SPI + pinMode(_sck, OUTPUT); + pinMode(_mosi, OUTPUT); + pinMode(_miso, INPUT); + } + } + + if (read8(BMP280_REGISTER_CHIPID) != chipid) + return false; + + readCoefficients(); + write8(BMP280_REGISTER_CONTROL, 0x3F); + return true; +} + +uint8_t Adafruit_BMP280::spixfer(uint8_t x) { + if (_sck == -1) + return SPI.transfer(x); + + // software spi + //Serial.println("Software SPI"); + uint8_t reply = 0; + for (int i=7; i>=0; i--) { + reply <<= 1; + digitalWrite(_sck, LOW); + digitalWrite(_mosi, x & (1<> 8) | (temp << 8); + +} + +/**************************************************************************/ +/*! + @brief Reads a signed 16 bit value over I2C/SPI +*/ +/**************************************************************************/ +int16_t Adafruit_BMP280::readS16(byte reg) +{ + return (int16_t)read16(reg); + +} + +int16_t Adafruit_BMP280::readS16_LE(byte reg) +{ + return (int16_t)read16_LE(reg); + +} + + +/**************************************************************************/ +/*! + @brief Reads a 24 bit value over I2C/SPI +*/ +/**************************************************************************/ +uint32_t Adafruit_BMP280::read24(byte reg) +{ + uint32_t value; + + if (_cs == -1) { + Wire.beginTransmission((uint8_t)_i2caddr); + Wire.write((uint8_t)reg); + Wire.endTransmission(); + Wire.requestFrom((uint8_t)_i2caddr, (byte)3); + + value = Wire.read(); + value <<= 8; + value |= Wire.read(); + value <<= 8; + value |= Wire.read(); + + } else { + if (_sck == -1) + SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); + digitalWrite(_cs, LOW); + spixfer(reg | 0x80); // read, bit 7 high + + value = spixfer(0); + value <<= 8; + value |= spixfer(0); + value <<= 8; + value |= spixfer(0); + + digitalWrite(_cs, HIGH); + if (_sck == -1) + SPI.endTransaction(); // release the SPI bus + } + + return value; +} + +/**************************************************************************/ +/*! + @brief Reads the factory-set coefficients +*/ +/**************************************************************************/ +void Adafruit_BMP280::readCoefficients(void) +{ + _bmp280_calib.dig_T1 = read16_LE(BMP280_REGISTER_DIG_T1); + _bmp280_calib.dig_T2 = readS16_LE(BMP280_REGISTER_DIG_T2); + _bmp280_calib.dig_T3 = readS16_LE(BMP280_REGISTER_DIG_T3); + + _bmp280_calib.dig_P1 = read16_LE(BMP280_REGISTER_DIG_P1); + _bmp280_calib.dig_P2 = readS16_LE(BMP280_REGISTER_DIG_P2); + _bmp280_calib.dig_P3 = readS16_LE(BMP280_REGISTER_DIG_P3); + _bmp280_calib.dig_P4 = readS16_LE(BMP280_REGISTER_DIG_P4); + _bmp280_calib.dig_P5 = readS16_LE(BMP280_REGISTER_DIG_P5); + _bmp280_calib.dig_P6 = readS16_LE(BMP280_REGISTER_DIG_P6); + _bmp280_calib.dig_P7 = readS16_LE(BMP280_REGISTER_DIG_P7); + _bmp280_calib.dig_P8 = readS16_LE(BMP280_REGISTER_DIG_P8); + _bmp280_calib.dig_P9 = readS16_LE(BMP280_REGISTER_DIG_P9); +} + +/**************************************************************************/ +/*! + +*/ +/**************************************************************************/ +float Adafruit_BMP280::readTemperature(void) +{ + int32_t var1, var2; + + int32_t adc_T = read24(BMP280_REGISTER_TEMPDATA); + adc_T >>= 4; + + var1 = ((((adc_T>>3) - ((int32_t)_bmp280_calib.dig_T1 <<1))) * + ((int32_t)_bmp280_calib.dig_T2)) >> 11; + + var2 = (((((adc_T>>4) - ((int32_t)_bmp280_calib.dig_T1)) * + ((adc_T>>4) - ((int32_t)_bmp280_calib.dig_T1))) >> 12) * + ((int32_t)_bmp280_calib.dig_T3)) >> 14; + + t_fine = var1 + var2; + + float T = (t_fine * 5 + 128) >> 8; + return T/100; +} + +/**************************************************************************/ +/*! + +*/ +/**************************************************************************/ +float Adafruit_BMP280::readPressure(void) { + int64_t var1, var2, p; + + // Must be done first to get the t_fine variable set up + readTemperature(); + + int32_t adc_P = read24(BMP280_REGISTER_PRESSUREDATA); + adc_P >>= 4; + + var1 = ((int64_t)t_fine) - 128000; + var2 = var1 * var1 * (int64_t)_bmp280_calib.dig_P6; + var2 = var2 + ((var1*(int64_t)_bmp280_calib.dig_P5)<<17); + var2 = var2 + (((int64_t)_bmp280_calib.dig_P4)<<35); + var1 = ((var1 * var1 * (int64_t)_bmp280_calib.dig_P3)>>8) + + ((var1 * (int64_t)_bmp280_calib.dig_P2)<<12); + var1 = (((((int64_t)1)<<47)+var1))*((int64_t)_bmp280_calib.dig_P1)>>33; + + if (var1 == 0) { + return 0; // avoid exception caused by division by zero + } + p = 1048576 - adc_P; + p = (((p<<31) - var2)*3125) / var1; + var1 = (((int64_t)_bmp280_calib.dig_P9) * (p>>13) * (p>>13)) >> 25; + var2 = (((int64_t)_bmp280_calib.dig_P8) * p) >> 19; + + p = ((p + var1 + var2) >> 8) + (((int64_t)_bmp280_calib.dig_P7)<<4); + return (float)p/256; +} + +float Adafruit_BMP280::readAltitude(float seaLevelhPa) { + float altitude; + + float pressure = readPressure(); // in Si units for Pascal + pressure /= 100; + + altitude = 44330 * (1.0 - pow(pressure / seaLevelhPa, 0.1903)); + + return altitude; +} diff --git a/Adafruit_BMP280.h b/Adafruit_BMP280.h new file mode 100644 index 0000000..e034a28 --- /dev/null +++ b/Adafruit_BMP280.h @@ -0,0 +1,158 @@ +/*************************************************************************** + This is a library for the BMP280 pressure sensor + + Designed specifically to work with the Adafruit BMP280 Breakout + ----> http://www.adafruit.com/products/2651 + + These sensors use I2C to communicate, 2 pins are required to interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit andopen-source hardware by purchasing products + from Adafruit! + + Written by Kevin Townsend for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ***************************************************************************/ +#ifndef __BMP280_H__ +#define __BMP280_H__ + +#if (ARDUINO >= 100) + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include + +#ifdef __AVR_ATtiny85__ + #include "TinyWireM.h" + #define Wire TinyWireM +#else + #include +#endif + +/*========================================================================= + I2C ADDRESS/BITS/SETTINGS + -----------------------------------------------------------------------*/ + #define BMP280_ADDRESS (0x76) + #define BMP280_CHIPID (0x58) +/*=========================================================================*/ + +/*========================================================================= + REGISTERS + -----------------------------------------------------------------------*/ + enum + { + BMP280_REGISTER_DIG_T1 = 0x88, + BMP280_REGISTER_DIG_T2 = 0x8A, + BMP280_REGISTER_DIG_T3 = 0x8C, + + BMP280_REGISTER_DIG_P1 = 0x8E, + BMP280_REGISTER_DIG_P2 = 0x90, + BMP280_REGISTER_DIG_P3 = 0x92, + BMP280_REGISTER_DIG_P4 = 0x94, + BMP280_REGISTER_DIG_P5 = 0x96, + BMP280_REGISTER_DIG_P6 = 0x98, + BMP280_REGISTER_DIG_P7 = 0x9A, + BMP280_REGISTER_DIG_P8 = 0x9C, + BMP280_REGISTER_DIG_P9 = 0x9E, + + BMP280_REGISTER_CHIPID = 0xD0, + BMP280_REGISTER_VERSION = 0xD1, + BMP280_REGISTER_SOFTRESET = 0xE0, + + BMP280_REGISTER_CAL26 = 0xE1, // R calibration stored in 0xE1-0xF0 + + BMP280_REGISTER_CONTROL = 0xF4, + BMP280_REGISTER_CONFIG = 0xF5, + BMP280_REGISTER_PRESSUREDATA = 0xF7, + BMP280_REGISTER_TEMPDATA = 0xFA, + }; + +/*=========================================================================*/ + +/*========================================================================= + CALIBRATION DATA + -----------------------------------------------------------------------*/ + typedef struct + { + uint16_t dig_T1; + int16_t dig_T2; + int16_t dig_T3; + + uint16_t dig_P1; + int16_t dig_P2; + int16_t dig_P3; + int16_t dig_P4; + int16_t dig_P5; + int16_t dig_P6; + int16_t dig_P7; + int16_t dig_P8; + int16_t dig_P9; + + uint8_t dig_H1; + int16_t dig_H2; + uint8_t dig_H3; + int16_t dig_H4; + int16_t dig_H5; + int8_t dig_H6; + } bmp280_calib_data; +/*=========================================================================*/ + +/* +class Adafruit_BMP280_Unified : public Adafruit_Sensor +{ + public: + Adafruit_BMP280_Unified(int32_t sensorID = -1); + + bool begin(uint8_t addr = BMP280_ADDRESS, uint8_t chipid = BMP280_CHIPID); + void getTemperature(float *temp); + void getPressure(float *pressure); + float pressureToAltitude(float seaLevel, float atmospheric, float temp); + float seaLevelForAltitude(float altitude, float atmospheric, float temp); + void getEvent(sensors_event_t*); + void getSensor(sensor_t*); + + private: + uint8_t _i2c_addr; + int32_t _sensorID; +}; + +*/ + +class Adafruit_BMP280 +{ + public: + Adafruit_BMP280(); + Adafruit_BMP280(int8_t cspin); + Adafruit_BMP280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin); + + bool begin(uint8_t addr = BMP280_ADDRESS, uint8_t chipid = BMP280_CHIPID); + float readTemperature(void); + float readPressure(void); + float readAltitude(float seaLevelhPa = 1013.25); + + private: + + void readCoefficients(void); + uint8_t spixfer(uint8_t x); + + void write8(byte reg, byte value); + uint8_t read8(byte reg); + uint16_t read16(byte reg); + uint32_t read24(byte reg); + int16_t readS16(byte reg); + uint16_t read16_LE(byte reg); // little endian + int16_t readS16_LE(byte reg); // little endian + + uint8_t _i2caddr; + int32_t _sensorID; + int32_t t_fine; + + int8_t _cs, _mosi, _miso, _sck; + + bmp280_calib_data _bmp280_calib; + +}; + +#endif diff --git a/Adafruit_Sensor.h b/Adafruit_Sensor.h new file mode 100644 index 0000000..7742afc --- /dev/null +++ b/Adafruit_Sensor.h @@ -0,0 +1,154 @@ +/* +* Copyright (C) 2008 The Android Open Source Project +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software< /span> +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* Update by K. Townsend (Adafruit Industries) for lighter typedefs, and + * extended sensor support to include color, voltage and current */ + +#ifndef _ADAFRUIT_SENSOR_H +#define _ADAFRUIT_SENSOR_H + +#if ARDUINO >= 100 + #include "Arduino.h" + #include "Print.h" +#else + #include "WProgram.h" +#endif + +/* Intentionally modeled after sensors.h in the Android API: + * https://github.com/android/platform_hardware_libhardware/blob/master/include/hardware/sensors.h */ + +/* Constants */ +#define SENSORS_GRAVITY_EARTH (9.80665F) /**< Earth's gravity in m/s^2 */ +#define SENSORS_GRAVITY_MOON (1.6F) /**< The moon's gravity in m/s^2 */ +#define SENSORS_GRAVITY_SUN (275.0F) /**< The sun's gravity in m/s^2 */ +#define SENSORS_GRAVITY_STANDARD (SENSORS_GRAVITY_EARTH) +#define SENSORS_MAGFIELD_EARTH_MAX (60.0F) /**< Maximum magnetic field on Earth's surface */ +#define SENSORS_MAGFIELD_EARTH_MIN (30.0F) /**< Minimum magnetic field on Earth's surface */ +#define SENSORS_PRESSURE_SEALEVELHPA (1013.25F) /**< Average sea level pressure is 1013.25 hPa */ +#define SENSORS_DPS_TO_RADS (0.017453293F) /**< Degrees/s to rad/s multiplier */ +#define SENSORS_GAUSS_TO_MICROTESLA (100) /**< Gauss to micro-Tesla multiplier */ + +/** Sensor types */ +typedef enum +{ + SENSOR_TYPE_ACCELEROMETER = (1), /**< Gravity + linear acceleration */ + SENSOR_TYPE_MAGNETIC_FIELD = (2), + SENSOR_TYPE_ORIENTATION = (3), + SENSOR_TYPE_GYROSCOPE = (4), + SENSOR_TYPE_LIGHT = (5), + SENSOR_TYPE_PRESSURE = (6), + SENSOR_TYPE_PROXIMITY = (8), + SENSOR_TYPE_GRAVITY = (9), + SENSOR_TYPE_LINEAR_ACCELERATION = (10), /**< Acceleration not including gravity */ + SENSOR_TYPE_ROTATION_VECTOR = (11), + SENSOR_TYPE_RELATIVE_HUMIDITY = (12), + SENSOR_TYPE_AMBIENT_TEMPERATURE = (13), + SENSOR_TYPE_VOLTAGE = (15), + SENSOR_TYPE_CURRENT = (16), + SENSOR_TYPE_COLOR = (17) +} sensors_type_t; + +/** struct sensors_vec_s is used to return a vector in a common format. */ +typedef struct { + union { + float v[3]; + struct { + float x; + float y; + float z; + }; + /* Orientation sensors */ + struct { + float roll; /**< Rotation around the longitudinal axis (the plane body, 'X axis'). Roll is positive and increasing when moving downward. -90°<=roll<=90° */ + float pitch; /**< Rotation around the lateral axis (the wing span, 'Y axis'). Pitch is positive and increasing when moving upwards. -180°<=pitch<=180°) */ + float heading; /**< Angle between the longitudinal axis (the plane body) and magnetic north, measured clockwise when viewing from the top of the device. 0-359° */ + }; + }; + int8_t status; + uint8_t reserved[3]; +} sensors_vec_t; + +/** struct sensors_color_s is used to return color data in a common format. */ +typedef struct { + union { + float c[3]; + /* RGB color space */ + struct { + float r; /**< Red component */ + float g; /**< Green component */ + float b; /**< Blue component */ + }; + }; + uint32_t rgba; /**< 24-bit RGBA value */ +} sensors_color_t; + +/* Sensor event (36 bytes) */ +/** struct sensor_event_s is used to provide a single sensor event in a common format. */ +typedef struct +{ + int32_t version; /**< must be sizeof(struct sensors_event_t) */ + int32_t sensor_id; /**< unique sensor identifier */ + int32_t type; /**< sensor type */ + int32_t reserved0; /**< reserved */ + int32_t timestamp; /**< time is in milliseconds */ + union + { + float data[4]; + sensors_vec_t acceleration; /**< acceleration values are in meter per second per second (m/s^2) */ + sensors_vec_t magnetic; /**< magnetic vector values are in micro-Tesla (uT) */ + sensors_vec_t orientation; /**< orientation values are in degrees */ + sensors_vec_t gyro; /**< gyroscope values are in rad/s */ + float temperature; /**< temperature is in degrees centigrade (Celsius) */ + float distance; /**< distance in centimeters */ + float light; /**< light in SI lux units */ + float pressure; /**< pressure in hectopascal (hPa) */ + float relative_humidity; /**< relative humidity in percent */ + float current; /**< current in milliamps (mA) */ + float voltage; /**< voltage in volts (V) */ + sensors_color_t color; /**< color in RGB component values */ + }; +} sensors_event_t; + +/* Sensor details (40 bytes) */ +/** struct sensor_s is used to describe basic information about a specific sensor. */ +typedef struct +{ + char name[12]; /**< sensor name */ + int32_t version; /**< version of the hardware + driver */ + int32_t sensor_id; /**< unique sensor identifier */ + int32_t type; /**< this sensor's type (ex. SENSOR_TYPE_LIGHT) */ + float max_value; /**< maximum value of this sensor's value in SI units */ + float min_value; /**< minimum value of this sensor's value in SI units */ + float resolution; /**< smallest difference between two values reported by this sensor */ + int32_t min_delay; /**< min delay in microseconds between events. zero = not a constant rate */ +} sensor_t; + +class Adafruit_Sensor { + public: + // Constructor(s) + Adafruit_Sensor() {} + virtual ~Adafruit_Sensor() {} + + // These must be defined by the subclass + virtual void enableAutoRange(bool enabled) {}; + virtual bool getEvent(sensors_event_t*) = 0; + virtual void getSensor(sensor_t*) = 0; + + private: + bool _autoRange; +}; + +#endif diff --git a/README.md b/README.md index 17afce2..49a9725 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,45 @@ -# BMP280-Tester-ESP32 +#Adafruit BMP280 Driver (Barometric Pressure Sensor) # -TestovacĂ­ program pre ESP32, alebo inĂș dosku pod ArduinoIDE pre testovanie BMP280 senzora. \ No newline at end of file +This driver is for the Adafruit BMP280 Breakout (http://www.adafruit.com/products/2651) + +## About the BMP280 ## + +This precision sensor from Bosch is the best low-cost sensing solution for measuring barometric pressure and temperature. Because pressure changes with altitude you can also use it as an altimeter! + +## About this Driver ## + +Adafruit invests time and resources providing this open source code. Please support Adafruit and open-source hardware by purchasing products from Adafruit! + +Written by Kevin (KTOWN) Townsend for Adafruit Industries. + + + +## Compatibility + +MCU | Tested Works | Doesn't Work | Not Tested | Notes +------------------ | :----------: | :----------: | :---------: | ----- +Atmega328 @ 16MHz | X | | | +Atmega328 @ 12MHz | X | | | +Atmega32u4 @ 16MHz | X | | | Use SDA/SCL on pins D2 & D3 +Atmega32u4 @ 8MHz | X | | | Use SDA/SCL on pins D2 & D3 +ESP8266 | X | | | SDA/SCL default to pins 4 & 5 but any two pins can be assigned as SDA/SCL using Wire.begin(SDA,SCL) +Atmega2560 @ 16MHz | X | | | Use SDA/SCL on pins 20 & 21 +ATSAM3X8E | X | | | Use SDA/SCL on pins 20 & 21 +ATSAM21D | X | | | +ATtiny85 @ 16MHz | | X | | +ATtiny85 @ 8MHz | | X | | +Intel Curie @ 32MHz | | | X | +STM32F2 | | | X | + + * ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini + * ATmega328 @ 12MHz : Adafruit Pro Trinket 3V + * ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0 + * ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro + * ESP8266 : Adafruit Huzzah + * ATmega2560 @ 16MHz : Arduino Mega + * ATSAM3X8E : Arduino Due + * ATSAM21D : Arduino Zero, M0 Pro + * ATtiny85 @ 16MHz : Adafruit Trinket 5V + * ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V + + diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..337758d --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=Adafruit BMP280 Library +version=1.0.2 +author=Adafruit +maintainer=Adafruit +sentence=Arduino library for BMP280 sensors. +paragraph=Arduino library for BMP280 pressure and altitude sensors. +category=Sensors +url=https://github.com/adafruit/Adafruit_BMP280_Library +architectures=*