Sensor – BME280 – Temperatuur, Luchtvochtigheid en Barometrische druk sensor
Hardware
Informatie (ENG)
Description:
The BME280 Breakout Board is the easy way to measure pressure and humidity, and without taking up a lot of room. It gives you easy to solder 0.1″ headers, runs I2C or SPI, takes measurements at less than 1mA and idles less than 5uA (yes, microamps!).
The BME280 can be used to take pressure, humidity, and temperature readings. Use the data to get relative altitude changes, or absolute altitude if the locally reported barometric pressure is known.
Specificaties:
Temp: -40C to 85C
Humidity: 0 – 100% RH, =-3% from 20-80%
Pressure: 30,000Pa to 110,000Pa, relative accuracy of 12Pa, absolute accuracy of 100Pa
Altitude: 0 to 30,000 ft (9.2 km), relative accuracy of 3.3 ft (1 m) at sea level, 6.6 (2 m) at 30,000 ft.
Typical application:
– Enhancement of GPS navigation (e.g. time-to-first-fix improvement,dead-reckoning,slope detection)
– Indoor navigation (floor detection,elevator detection)
– Outdoor navigation,leisure and sports applications
– Weather forecast
– Health care application (e.g. sirometry)
– Vertical velocity indication (e.g. risk/sink speed)
Mini module – change address
To change the I2C address from 0x76 to 0x77:
Pinout
Pin: | Functie: |
---|---|
1 | +5V (VCC) |
2 | GND |
3 | SCL Serial Clock (line) |
4 | SCA Serial Clock (data) |
5 | CSB (Chip Select) |
6 | SDO (Serial Data Out) |
Arduino
Sluit de module aan volgens onderstaand overzicht:
Arduino pin: | BMP280 pin: |
---|---|
+5V | +5V (VCC) |
GND | GND |
A5 | SCL Serial Clock (line) |
A4 | SCA Serial Clock (data) |
Script met BME280 bibliotheek
Wat heb je nodig?
1) BME280 bibliotheek
Het script leest de Temperatuur, Luchtvochtigheid en Luchtdruk uit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <BME280I2C.h> #include <Wire.h> #define SERIAL_BAUD 9600 BME280I2C bme; // Default : forced mode, standby time = 1000 ms // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off, float pres, temp, hum; void setup(){ Serial.begin(SERIAL_BAUD); while(!Serial) {} // Wait Wire.begin(); while(!bme.begin()) { Serial.println("Could not find BME280 sensor!"); delay(1000); } } void loop() { BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); BME280::PresUnit presUnit(BME280::PresUnit_Pa); bme.read(pres, temp, hum, tempUnit, presUnit); Serial.print("\tTemp: "); Serial.print(temp); Serial.print("C"); Serial.print("\tHum: "); Serial.print(hum); Serial.print("% RH"); Serial.print("\tPres: "); Serial.print(pres); Serial.println(" Pa"); delay(1000); } |
Het resultaat
1 2 3 4 5 6 7 8 9 10 11 |
Temp: 24.50C Hum: 32.08% RH Pres: 100314.98 Pa Temp: 24.45C Hum: 32.15% RH Pres: 100317.16 Pa Temp: 24.45C Hum: 32.16% RH Pres: 100312.70 Pa Temp: 24.44C Hum: 32.15% RH Pres: 100315.46 Pa Temp: 24.44C Hum: 32.17% RH Pres: 100313.67 Pa Temp: 24.45C Hum: 32.21% RH Pres: 100312.70 Pa Temp: 24.43C Hum: 32.30% RH Pres: 100311.98 Pa Temp: 24.43C Hum: 32.47% RH Pres: 100314.63 Pa Temp: 24.42C Hum: 32.59% RH Pres: 100307.70 Pa Temp: 24.43C Hum: 32.60% RH Pres: 100314.63 Pa Temp: 24.42C Hum: 32.54% RH Pres: 100309.48 Pa |
Script met alleen wire bibliotheek
Wat heb je nodig?
1) Alleen de Wire bibliotheek (wordt standaard bijgeleverd bij ArduinoIDE)
Het standaard adres is 0x76, pas deze regel aan indien een afwijkend adres is gebruikt:
#define BME280_ADDRESS 0x76
Het script leest de Temperatuur, Luchtvochtigheid en Luchtdruk uit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
#include <Wire.h> #define BME280_ADDRESS 0x76 unsigned long int hum_raw,temp_raw,pres_raw; signed long int t_fine; 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; int8_t dig_H1; int16_t dig_H2; int8_t dig_H3; int16_t dig_H4; int16_t dig_H5; int8_t dig_H6; void setup() { uint8_t osrs_t = 1; //Temperature oversampling x 1 uint8_t osrs_p = 1; //Pressure oversampling x 1 uint8_t osrs_h = 1; //Humidity oversampling x 1 uint8_t mode = 3; //Normal mode uint8_t t_sb = 5; //Tstandby 1000ms uint8_t filter = 0; //Filter off uint8_t spi3w_en = 0; //3-wire SPI Disable uint8_t ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | mode; uint8_t config_reg = (t_sb << 5) | (filter << 2) | spi3w_en; uint8_t ctrl_hum_reg = osrs_h; Serial.begin(9600); Wire.begin(); writeReg(0xF2,ctrl_hum_reg); writeReg(0xF4,ctrl_meas_reg); writeReg(0xF5,config_reg); readTrim(); // } void loop() { double temp_act = 0.0, press_act = 0.0,hum_act=0.0; signed long int temp_cal; unsigned long int press_cal,hum_cal; readData(); temp_cal = calibration_T(temp_raw); press_cal = calibration_P(pres_raw); hum_cal = calibration_H(hum_raw); temp_act = (double)temp_cal / 100.0; press_act = (double)press_cal / 100.0; hum_act = (double)hum_cal / 1024.0; Serial.print("TEMP : "); Serial.print(temp_act); Serial.print(" DegC PRESS : "); Serial.print(press_act); Serial.print(" hPa HUM : "); Serial.print(hum_act); Serial.println(" %"); delay(1000); } void readTrim() { uint8_t data[32],i=0; Wire.beginTransmission(BME280_ADDRESS); Wire.write(0x88); Wire.endTransmission(); Wire.requestFrom(BME280_ADDRESS,24); while(Wire.available()) { data[i] = Wire.read(); i++; } Wire.beginTransmission(BME280_ADDRESS); Wire.write(0xA1); Wire.endTransmission(); Wire.requestFrom(BME280_ADDRESS,1); data[i] = Wire.read(); i++; Wire.beginTransmission(BME280_ADDRESS); Wire.write(0xE1); Wire.endTransmission(); Wire.requestFrom(BME280_ADDRESS,7); while(Wire.available()) { data[i] = Wire.read(); i++; } dig_T1 = (data[1] << 8) | data[0]; dig_T2 = (data[3] << 8) | data[2]; dig_T3 = (data[5] << 8) | data[4]; dig_P1 = (data[7] << 8) | data[6]; dig_P2 = (data[9] << 8) | data[8]; dig_P3 = (data[11]<< 8) | data[10]; dig_P4 = (data[13]<< 8) | data[12]; dig_P5 = (data[15]<< 8) | data[14]; dig_P6 = (data[17]<< 8) | data[16]; dig_P7 = (data[19]<< 8) | data[18]; dig_P8 = (data[21]<< 8) | data[20]; dig_P9 = (data[23]<< 8) | data[22]; dig_H1 = data[24]; dig_H2 = (data[26]<< 8) | data[25]; dig_H3 = data[27]; dig_H4 = (data[28]<< 4) | (0x0F & data[29]); dig_H5 = (data[30] << 4) | ((data[29] >> 4) & 0x0F); dig_H6 = data[31]; } void writeReg(uint8_t reg_address, uint8_t data) { Wire.beginTransmission(BME280_ADDRESS); Wire.write(reg_address); Wire.write(data); Wire.endTransmission(); } void readData() { int i = 0; uint32_t data[8]; Wire.beginTransmission(BME280_ADDRESS); Wire.write(0xF7); Wire.endTransmission(); Wire.requestFrom(BME280_ADDRESS,8); while(Wire.available()){ data[i] = Wire.read(); i++; } pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); hum_raw = (data[6] << 8) | data[7]; } signed long int calibration_T(signed long int adc_T) { signed long int var1, var2, T; var1 = ((((adc_T >> 3) - ((signed long int)dig_T1<<1))) * ((signed long int)dig_T2)) >> 11; var2 = (((((adc_T >> 4) - ((signed long int)dig_T1)) * ((adc_T>>4) - ((signed long int)dig_T1))) >> 12) * ((signed long int)dig_T3)) >> 14; t_fine = var1 + var2; T = (t_fine * 5 + 128) >> 8; return T; } unsigned long int calibration_P(signed long int adc_P) { signed long int var1, var2; unsigned long int P; var1 = (((signed long int)t_fine)>>1) - (signed long int)64000; var2 = (((var1>>2) * (var1>>2)) >> 11) * ((signed long int)dig_P6); var2 = var2 + ((var1*((signed long int)dig_P5))<<1); var2 = (var2>>2)+(((signed long int)dig_P4)<<16); var1 = (((dig_P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3) + ((((signed long int)dig_P2) * var1)>>1))>>18; var1 = ((((32768+var1))*((signed long int)dig_P1))>>15); if (var1 == 0) { return 0; } P = (((unsigned long int)(((signed long int)1048576)-adc_P)-(var2>>12)))*3125; if(P<0x80000000) { P = (P << 1) / ((unsigned long int) var1); } else { P = (P / (unsigned long int)var1) * 2; } var1 = (((signed long int)dig_P9) * ((signed long int)(((P>>3) * (P>>3))>>13)))>>12; var2 = (((signed long int)(P>>2)) * ((signed long int)dig_P8))>>13; P = (unsigned long int)((signed long int)P + ((var1 + var2 + dig_P7) >> 4)); return P; } unsigned long int calibration_H(signed long int adc_H) { signed long int v_x1; v_x1 = (t_fine - ((signed long int)76800)); v_x1 = (((((adc_H << 14) -(((signed long int)dig_H4) << 20) - (((signed long int)dig_H5) * v_x1)) + ((signed long int)16384)) >> 15) * (((((((v_x1 * ((signed long int)dig_H6)) >> 10) * (((v_x1 * ((signed long int)dig_H3)) >> 11) + ((signed long int) 32768))) >> 10) + (( signed long int)2097152)) * ((signed long int) dig_H2) + 8192) >> 14)); v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * ((signed long int)dig_H1)) >> 4)); v_x1 = (v_x1 < 0 ? 0 : v_x1); v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1); return (unsigned long int)(v_x1 >> 12); } |
Het resultaat
1 2 3 4 5 6 7 8 9 10 11 |
TEMP : 25.54 DegC PRESS : 1019.16 hPa HUM : 47.29 % TEMP : 25.51 DegC PRESS : 1019.19 hPa HUM : 47.31 % TEMP : 25.51 DegC PRESS : 1019.24 hPa HUM : 47.32 % TEMP : 26.66 DegC PRESS : 1019.18 hPa HUM : 50.57 % TEMP : 27.47 DegC PRESS : 1019.16 hPa HUM : 50.83 % TEMP : 27.75 DegC PRESS : 1019.17 hPa HUM : 51.09 % TEMP : 27.89 DegC PRESS : 1019.20 hPa HUM : 51.41 % TEMP : 28.04 DegC PRESS : 1019.18 hPa HUM : 51.69 % TEMP : 27.39 DegC PRESS : 1019.13 hPa HUM : 53.10 % TEMP : 26.75 DegC PRESS : 1019.18 hPa HUM : 50.68 % TEMP : 26.50 DegC PRESS : 1019.12 hPa HUM : 49.25 % |
Bronnen:
osoyoo.com
Script met BME280 bibliotheek op SPI
Wat heb je nodig?
Sluit de BME280 module aan op de arduino aan zoals hieronder aangegeven:
Het script leest de Temperatuur, Luchtvochtigheid en Luchtdruk uit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <SPI.h> #include <BME280Spi.h> #define DEVICE_PIN 10 BME280Spi::Settings settings(DEVICE_PIN); // Default : forced mode, standby time = 1000 ms // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off, BME280Spi bme(settings); void setup() { Serial.begin(115200); while(!Serial) {} // Wait SPI.begin(); while(!bme.begin()) { Serial.println("Could not find BME280 sensor!"); delay(1000); } } void loop() { printBME280Data(&Serial); delay(500); } void printBME280Data (Stream* client) { float temp(NAN), hum(NAN), pres(NAN); BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); BME280::PresUnit presUnit(BME280::PresUnit_Pa); bme.read(pres, temp, hum, tempUnit, presUnit); client->print("Temp: "); client->print(temp); client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F')); client->print("\t\tHumidity: "); client->print(hum); client->print("% RH"); client->print("\t\tPressure: "); client->print(pres); client->println("Pa"); delay(1000); } |
1 2 3 4 5 |
Temp: 27.53oC Humidity: 37.72% RH Pressure: 102430.53Pa Temp: 27.49oC Humidity: 37.77% RH Pressure: 102431.69Pa Temp: 27.51oC Humidity: 37.76% RH Pressure: 102427.92Pa Temp: 27.53oC Humidity: 37.71% RH Pressure: 102427.85Pa Temp: 27.53oC Humidity: 37.70% RH Pressure: 102432.36Pa |
Arduino Library
Installatie van Arduino IDE libraries: Arduino info
BME280 Library
Informatie (ENG)
Summary
Reads temperature, humidity, and pressure. Calculates altitude, equivalent sea level pressure and dew point. Provides functions for english and metric. Also reads pressure in Pa, hPa, inHg, atm, bar, torr, N/m^2 and psi.
Installation
To use this library download the zip file, decompress it to a folder named BME280. Move the folder to {Arduino Path}/libraries.
Usage
Include the library at the top of your Arduino script. #include <BME280>
Create a global or local variable. BME280 bme
In your start up call bme.begin()
. Read the temperature, humidity, pressure, altitude and/or dew point.
float pres, temp, hum
bme.read(pres, temp, hum)
or
temp = bme.temp()
hum = bme.hum()
pres = bme.pres()
Enumerations
TempUnit Enum
- TempUnit_Celsius
- TempUnit_Fahrenheit
PresUnit Enum
- PresUnit_Pa
- PresUnit_hPa
- PresUnit_inHg
- PresUnit_atm
- PresUnit_bar
- PresUnit_torr
- PresUnit_psi
OSR Enum
- OSR_Off
- OSR_X1
- OSR_X2
- OSR_X4
- OSR_X8
- OSR_X16
Mode Enum
- Mode_Sleep
- Mode_Forced
- Mode_Normal
StandbyTime Enum
- StandbyTime_500us
- StandbyTime_62500us
- StandbyTime_125ms
- StandbyTime_250ms
- StandbyTime_50ms
- StandbyTime_1000ms
- StandbyTime_10ms
- StandbyTime_20ms
Filter Enum
- Filter_Off
- Filter_1
- Filter_2
- Filter_4
- Filter_8
- Filter_16
ChipModel Enum
- ChipModel_Unknown
- ChipModel_BME280
- ChipModel_BMP280
Settings
BME280::Settings Struct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
* Temperature Oversampling Rate (tempOSR): OSR Enum, default = OSR_X1 * Humidity Oversampling Rate (humOSR): OSR Enum, default = OSR_X1 * Pressure Oversampling Rate (presOSR): OSR Enum, default = OSR_X1 * Mode (mode): Mode Enum, default = Mode_Forced * Standby Time (standbyTime): StandbyTime Enum, default = StandbyTime_1000ms * Filter (filter): Filter Enum, default = Filter_16 * SPI Enable: SpiEnable Enum, default = false values: true = enable, false = disable |
BME280I2C::Settings Struct
- Includes all fields in BME280 settings.
1 2 |
* BME 280 Address (bme280Addr): uint8_t, default = 0x76 |
BME280Spi::Settings Struct
- Includes all fields in BME280 settings.
1 2 3 |
* SPI Chip Select Pin (spiCsPin): uint8_t values: Any pin 0-31 |
BME280Spi::Settings Struct
- Includes all fields in BME280 settings.
1 2 3 4 5 6 7 8 9 10 11 12 |
* SPI Chip Select Pin (spiCsPin): uint8_t values: Any pin 0-31 * SPI Master Out Slave In Pin (spiMosiPin): uint8_t values: Any pin 0-31 * SPI Master In Slave Out Pin (spiMisoPin): uint8_t values: Any pin 0-31 * SPI Serial Clock Pin (spiSckPin): uint8_t values: Any pin 0-31 |
Methods
BME280I2C(const BME280I2C::Settings& settings)
Constructor used to create the I2C Bme class. All parameters have default values.
BME280Spi(const BME280Spi::Settings& settings)
Constructor used to create the Spi Bme class. All parameters have default values except chip select.
BME280SpiSw(const BME280SpiSw::Settings& settings)
Constructor used to create the software Spi Bme class. All parameters have default values except chip select, mosi, miso and sck.
bool begin()
Method used at start up to initialize the class. Starts the I2C or SPI interface. Can be called again to re-initialize the mode settings.
1 2 |
* return: bool, true = success, false = failure (no device found) |
void setSettings(const Settings& settings)
Method to set the sensor settings.
const Settings& getSettings() const
Method to get the sensor settings.
float temp(TempUnit unit)
Read the temperature from the BME280 and return a float.
1 2 3 4 |
return: float = temperature * unit: tempUnit, default = TempUnit_Celsius |
float pres(PresUnit unit)
Read the pressure from the BME280 and return a float with the specified unit.
1 2 3 4 |
return: float = pressure * presUnit: uint8_t, default = PresUnit_hPa |
float hum()
Read the humidity from the BME280 and return a percentage as a float.
1 2 |
* return: float = percent relative humidity |
void read(float& pressure, float& temp, float& humidity, TempUnit tempUnit, PresUnit presUnit)
Read the data from the BME280 with the specified units.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
return: None, however, pressure, temp and humidity are changed. * Pressure: float, reference values: reference to storage float for pressure * Temperature: float, reference values: reference to storage float for temperature * Humidity: float, reference values: reference to storage float for humidity * tempUnit: tempUnit, default = TempUnit_Celsius * presUnit: uint8_t, default = PresUnit_hPa |
ChipModel chipModel()
1 2 |
* return: [ChipModel](#chipmodel-enum) enum |
Environment Calculations
float Altitude(float pressure, AltitudeUnit = AltitudeUnit_Meters, float seaLevelPressure = 1013.25, outsideTemp = 15.0, TempUnit = TempUnit_Celsius)
Calculate the altitude based on the pressure with the specified units.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Return: float altitude * Pressure: float values: unit independent * AltitudeUnit: default = AltitudeUnit_Meters values: AltitudeUnit_Meters, AltitudeUnit_Feet * Sea Level Pressure: float, default = 1013.25 values: unit independent * outsideTemp: float, default = 15.0 values: any float related to TempUnit * TempUnit: default = TempUnit_Celsius values: TempUnit_Celsius, TempUnit_Fahrenheit Note: The formula evaluates the height difference based on difference of pressure. - May be used to evaluate altitude over MSL. (default set) (default referencePressure, default outsideTemp parameters ~ ISA standard) The altitude is derived from QNH, used in aviation. - May be used to evaluate height over MSL (referencePressure should be equal to QFF read in meteorologic synoptic maps, outsideTemp should be equal to local temperature) - May be used to evaluate the height difference between two points (referencePressure should be set to the pressure on the lower point.) |
float EquivalentSeaLevelPressure(float altitude, float temp, float pres, AltitudeUnit altUnit, TempUnit tempUnit )
Convert current pressure to equivalent sea-level pressure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Return: float equivalent pressure at sea level. * altitude: float values: meters * temp: float values: celsius * pres: float values: unit independent * AltitudeUnit: default = AltitudeUnit_Meters values: AltitudeUnit_Meters, AltitudeUnit_Feet * TempUnit: default = TempUnit_Celsius values: TempUnit_Celsius, TempUnit_Fahrenheit Note: To get correct EquivalentSeaLevel pressure (QNH or QFF) the altitude value should be independent on measured pressure. It is necessary to use fixed altitude point e.g. the altitude of barometer read in map. |
float DewPoint(float temp, float hum, TempUnit = TempUnit_Celsius)
Calculate the dew point based on the temperature and humidity with the specified units.
1 2 3 4 5 6 7 8 9 10 11 |
return: float dew point * Temperature: float values: any float related to TempUnit * Humidity: float, unit = % relative humidity values: any float * TempUnit: TempUnit, default = TempUnit_Celsius values: TempUnit_Celsius = return degrees Celsius, TempUnit_Fahrenheit = return degrees Fahrenheit |
float AbsoluteHumidity(float temperature, float humidity, TempUnit = TempUnit_Celsius)
Calculate the absolute humidity based on the temperature and humidity with the specified units.
1 2 3 4 5 6 7 8 9 10 11 |
return: float absolute humidity * Temperature: float values: any float related to TempUnit * Humidity: float, unit = % relative humidity values: any float * TempUnit: TempUnit, default = TempUnit_Celsius values: TempUnit_Celsius = return degrees Celsius, TempUnit_Fahrenheit = return degrees Fahrenheit |
int HeatIndex(float temperature, float humidity, TempUnit tempUnit = TempUnit_Celsius)
Calculate the heat index based on the temperature and humidity with the specified units. The heat index is only calculated if the temperature is above 26.7°C or 80°F and humidity above 40%. The precision is +/- 0.7°C / 1.3°F.
1 2 3 4 5 6 7 8 9 10 11 |
return: int heat index * Temperature: float values: any float related to TempUnit * Humidity: float, unit = % relative humidity values: any float * TempUnit: TempUnit, default = TempUnit_Celsius values: TempUnit_Celsius = return degrees Celsius, TempUnit_Fahrenheit = return degrees Fahrenheit |
History
- Jan 1, 2016 – Version 1.0.0 released
- Sep 19, 2016 – Version 2.0.0 released (Restructure for I2C and SPI)
- Nov 21, 2016 – Version 2.0.1 released (Set mode support)
- Dec 19, 2016 – Version 2.1.0 released (Support for SPI)
- Dec 21, 2016 – Version 2.1.1 released (Bugs)
- Feb 17, 2017 – Version 2.1.2 released (Docs)
- Sept 9, 2017 – Version 2.1.3 released (Formatting, reorg)
- Sept 13, 2017 – Version 2.1.4 released (Examples update, bug fixes)
- Oct 7, 2017 – Version 2.2.0 released (Enums, begin restructure)
- Oct 10, 2017 – Version 2.2.1 released (Bug fixes)
Download bibliotheek @ finitespace * Github
- BME280-Release_Version_2.3.0.7z 22,29 kb
- BME280-Release_Version_2.2.1.7z 21,95 kb
- BME280-Release_Version_2.1.4.7z 21,28 kb
- BME280-Release_Version_2.1.3.7z 21,41 kb
- BME280-Release_Version_2.1.2.7z 19,85 kb
- BME280-Release_Version_2.1.01.7z 19,77 kb
- BME280-Release_Version_2.0.1.7z 20,97 kb
- BME280-Release_Version_2.0.01.7z 18,25 kb
- BME280-Release_Version_2.0.0.7z 18,06 kb
- BME280-Release_Version_1.0.02.7z 17,65 kb
- BME280-Release_Version_1.0.01.7z 17,63 kb
- BME280-Release_Version_1.0.0.7z 17,61 kb
Raspberry Pi
Sluit de module aan zoals aangegeven op onderstaand schema:
Raspberry Pi pin: | I2C module pin: |
---|---|
+5v | +5v |
GND | GND |
3 (GPIO2) SDA | SDA |
5 (GPIO3) SCL | SCL |
Ps. Altijd de pinout van je Raspberry Pi controleren, deze kan verschillen per versie.
Wat moet je doen?
1) I2C aanzetten op de Pi.
2) SMBUS module installeren voor Python:
Om de I2C bus in Python te gebruiken moet je een module installeren, genaamd SMBUS, dit kan via APT-GET met het commando: sudo apt-get install python-smbus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: python-smbus 0 upgraded, 1 newly installed, 0 to remove and 79 not upgraded. Need to get 11.9 kB of archives. After this operation, 95.2 kB of additional disk space will be used. Get:1 http://archive.raspberrypi.org/debian/ wheezy/main python-smbus armhf 3.1.1+svn-1 [11.9 kB] Fetched 11.9 kB in 0s (131 kB/s) Selecting previously unselected package python-smbus. (Reading database ... 78301 files and directories currently installed.) Unpacking python-smbus (from .../python-smbus_3.1.1+svn-1_armhf.deb) ... Setting up python-smbus (3.1.1+svn-1) ... |
Je kan dan de module importeren in python door middel van: import smbus
Controleren van de aansluiting
Om de aansluiting van de module te controleren voor je het volgende commando uit: sudo i2cdetect -y 1, je ziet dan adres 0x77 staan in de lijst:
1 2 3 4 5 6 7 8 9 10 |
pi@raspberrypi:~ $ sudo i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- 76 -- |
Het script bme280.py om de module uit te lezen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import smbus import time # Get I2C bus bus = smbus.SMBus(1) # BME280 address, 0x76(118) # Read data back from 0x88(136), 24 bytes b1 = bus.read_i2c_block_data(0x76, 0x88, 24) # Convert the data # Temp coefficients dig_T1 = b1[1] * 256 + b1[0] dig_T2 = b1[3] * 256 + b1[2] if dig_T2 > 32767 : dig_T2 -= 65536 dig_T3 = b1[5] * 256 + b1[4] if dig_T3 > 32767 : dig_T3 -= 65536 # Pressure coefficients dig_P1 = b1[7] * 256 + b1[6] dig_P2 = b1[9] * 256 + b1[8] if dig_P2 > 32767 : dig_P2 -= 65536 dig_P3 = b1[11] * 256 + b1[10] if dig_P3 > 32767 : dig_P3 -= 65536 dig_P4 = b1[13] * 256 + b1[12] if dig_P4 > 32767 : dig_P4 -= 65536 dig_P5 = b1[15] * 256 + b1[14] if dig_P5 > 32767 : dig_P5 -= 65536 dig_P6 = b1[17] * 256 + b1[16] if dig_P6 > 32767 : dig_P6 -= 65536 dig_P7 = b1[19] * 256 + b1[18] if dig_P7 > 32767 : dig_P7 -= 65536 dig_P8 = b1[21] * 256 + b1[20] if dig_P8 > 32767 : dig_P8 -= 65536 dig_P9 = b1[23] * 256 + b1[22] if dig_P9 > 32767 : dig_P9 -= 65536 # BME280 address, 0x76(118) # Read data back from 0xA1(161), 1 byte dig_H1 = bus.read_byte_data(0x76, 0xA1) # BME280 address, 0x76(118) # Read data back from 0xE1(225), 7 bytes b1 = bus.read_i2c_block_data(0x76, 0xE1, 7) # Convert the data # Humidity coefficients dig_H2 = b1[1] * 256 + b1[0] if dig_H2 > 32767 : dig_H2 -= 65536 dig_H3 = (b1[2] & 0xFF) dig_H4 = (b1[3] * 16) + (b1[4] & 0xF) if dig_H4 > 32767 : dig_H4 -= 65536 dig_H5 = (b1[4] / 16) + (b1[5] * 16) if dig_H5 > 32767 : dig_H5 -= 65536 dig_H6 = b1[6] if dig_H6 > 127 : dig_H6 -= 256 # BME280 address, 0x76(118) # Select control humidity register, 0xF2(242) # 0x01(01) Humidity Oversampling = 1 bus.write_byte_data(0x76, 0xF2, 0x01) # BME280 address, 0x76(118) # Select Control measurement register, 0xF4(244) # 0x27(39) Pressure and Temperature Oversampling rate = 1 # Normal mode bus.write_byte_data(0x76, 0xF4, 0x27) # BME280 address, 0x76(118) # Select Configuration register, 0xF5(245) # 0xA0(00) Stand_by time = 1000 ms bus.write_byte_data(0x76, 0xF5, 0xA0) time.sleep(0.5) # BME280 address, 0x76(118) # Read data back from 0xF7(247), 8 bytes # Pressure MSB, Pressure LSB, Pressure xLSB, Temperature MSB, Temperature LSB # Temperature xLSB, Humidity MSB, Humidity LSB data = bus.read_i2c_block_data(0x76, 0xF7, 8) # Convert pressure and temperature data to 19-bits adc_p = ((data[0] * 65536) + (data[1] * 256) + (data[2] & 0xF0)) / 16 adc_t = ((data[3] * 65536) + (data[4] * 256) + (data[5] & 0xF0)) / 16 # Convert the humidity data adc_h = data[6] * 256 + data[7] # Temperature offset calculations var1 = ((adc_t) / 16384.0 - (dig_T1) / 1024.0) * (dig_T2) var2 = (((adc_t) / 131072.0 - (dig_T1) / 8192.0) * ((adc_t)/131072.0 - (dig_T1)/8192.0)) * (dig_T3) t_fine = (var1 + var2) cTemp = (var1 + var2) / 5120.0 fTemp = cTemp * 1.8 + 32 # Pressure offset calculations var1 = (t_fine / 2.0) - 64000.0 var2 = var1 * var1 * (dig_P6) / 32768.0 var2 = var2 + var1 * (dig_P5) * 2.0 var2 = (var2 / 4.0) + ((dig_P4) * 65536.0) var1 = ((dig_P3) * var1 * var1 / 524288.0 + ( dig_P2) * var1) / 524288.0 var1 = (1.0 + var1 / 32768.0) * (dig_P1) p = 1048576.0 - adc_p p = (p - (var2 / 4096.0)) * 6250.0 / var1 var1 = (dig_P9) * p * p / 2147483648.0 var2 = p * (dig_P8) / 32768.0 pressure = (p + (var1 + var2 + (dig_P7)) / 16.0) / 100 # Humidity offset calculations var_H = ((t_fine) - 76800.0) var_H = (adc_h - (dig_H4 * 64.0 + dig_H5 / 16384.0 * var_H)) * (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * var_H * (1.0 + dig_H3 / 67108864.0 * var_H))) humidity = var_H * (1.0 - dig_H1 * var_H / 524288.0) if humidity > 100.0 : humidity = 100.0 elif humidity < 0.0 : humidity = 0.0 # Output data to screen print "Temperature in Celsius : %.2f C" %cTemp print "Temperature in Fahrenheit : %.2f F" %fTemp print "Pressure : %.2f hPa " %pressure print "Relative Humidity : %.2f %%" %humidity |
Start het script met: sudo python bme280.py, resultaat:
1 2 3 4 |
Temperature in Celsius : 25.50 C Temperature in Fahrenheit : 77.89 F Pressure : 1019.44 hPa Relative Humidity : 49.21 % |
Script #2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
#!/usr/bin/python #-------------------------------------- # ___ ___ _ ____ # / _ \/ _ \(_) __/__ __ __ # / , _/ ___/ /\ \/ _ \/ // / # /_/|_/_/ /_/___/ .__/\_, / # /_/ /___/ # # bme280.py # Read data from a digital pressure sensor. # # Official datasheet available from : # https://www.bosch-sensortec.com/bst/products/all_products/bme280 # # Author : Matt Hawkins # Date : 25/07/2016 # # http://www.raspberrypi-spy.co.uk/ # #-------------------------------------- import smbus import time from ctypes import c_short from ctypes import c_byte from ctypes import c_ubyte DEVICE = 0x76 # Default device I2C address bus = smbus.SMBus(1) # Rev 2 Pi, Pi 2 & Pi 3 uses bus 1 # Rev 1 Pi uses bus 0 def getShort(data, index): # return two bytes from data as a signed 16-bit value return c_short((data[index+1] << 8) + data[index]).value def getUShort(data, index): # return two bytes from data as an unsigned 16-bit value return (data[index+1] << 8) + data[index] def getChar(data,index): # return one byte from data as a signed char result = data[index] if result > 127: result -= 256 return result def getUChar(data,index): # return one byte from data as an unsigned char result = data[index] & 0xFF return result def readBME280ID(addr=DEVICE): # Chip ID Register Address REG_ID = 0xD0 (chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2) return (chip_id, chip_version) def readBME280All(addr=DEVICE): # Register Addresses REG_DATA = 0xF7 REG_CONTROL = 0xF4 REG_CONFIG = 0xF5 REG_CONTROL_HUM = 0xF2 REG_HUM_MSB = 0xFD REG_HUM_LSB = 0xFE # Oversample setting - page 27 OVERSAMPLE_TEMP = 2 OVERSAMPLE_PRES = 2 MODE = 1 # Oversample setting for humidity register - page 26 OVERSAMPLE_HUM = 2 bus.write_byte_data(addr, REG_CONTROL_HUM, OVERSAMPLE_HUM) control = OVERSAMPLE_TEMP<<5 | OVERSAMPLE_PRES<<2 | MODE bus.write_byte_data(addr, REG_CONTROL, control) # Read blocks of calibration data from EEPROM # See Page 22 data sheet cal1 = bus.read_i2c_block_data(addr, 0x88, 24) cal2 = bus.read_i2c_block_data(addr, 0xA1, 1) cal3 = bus.read_i2c_block_data(addr, 0xE1, 7) # Convert byte data to word values dig_T1 = getUShort(cal1, 0) dig_T2 = getShort(cal1, 2) dig_T3 = getShort(cal1, 4) dig_P1 = getUShort(cal1, 6) dig_P2 = getShort(cal1, 8) dig_P3 = getShort(cal1, 10) dig_P4 = getShort(cal1, 12) dig_P5 = getShort(cal1, 14) dig_P6 = getShort(cal1, 16) dig_P7 = getShort(cal1, 18) dig_P8 = getShort(cal1, 20) dig_P9 = getShort(cal1, 22) dig_H1 = getUChar(cal2, 0) dig_H2 = getShort(cal3, 0) dig_H3 = getUChar(cal3, 2) dig_H4 = getChar(cal3, 3) dig_H4 = (dig_H4 << 24) >> 20 dig_H4 = dig_H4 | (getChar(cal3, 4) & 0x0F) dig_H5 = getChar(cal3, 5) dig_H5 = (dig_H5 << 24) >> 20 dig_H5 = dig_H5 | (getUChar(cal3, 4) >> 4 & 0x0F) dig_H6 = getChar(cal3, 6) # Wait in ms (Datasheet Appendix B: Measurement time and current calculation) wait_time = 1.25 + (2.3 * OVERSAMPLE_TEMP) + ((2.3 * OVERSAMPLE_PRES) + 0.575) + ((2.3 * OVERSAMPLE_HUM)+0.575) time.sleep(wait_time/1000) # Wait the required time # Read temperature/pressure/humidity data = bus.read_i2c_block_data(addr, REG_DATA, 8) pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4) temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4) hum_raw = (data[6] << 8) | data[7] #Refine temperature var1 = ((((temp_raw>>3)-(dig_T1<<1)))*(dig_T2)) >> 11 var2 = (((((temp_raw>>4) - (dig_T1)) * ((temp_raw>>4) - (dig_T1))) >> 12) * (dig_T3)) >> 14 t_fine = var1+var2 temperature = float(((t_fine * 5) + 128) >> 8); # Refine pressure and adjust for temperature var1 = t_fine / 2.0 - 64000.0 var2 = var1 * var1 * dig_P6 / 32768.0 var2 = var2 + var1 * dig_P5 * 2.0 var2 = var2 / 4.0 + dig_P4 * 65536.0 var1 = (dig_P3 * var1 * var1 / 524288.0 + dig_P2 * var1) / 524288.0 var1 = (1.0 + var1 / 32768.0) * dig_P1 if var1 == 0: pressure=0 else: pressure = 1048576.0 - pres_raw pressure = ((pressure - var2 / 4096.0) * 6250.0) / var1 var1 = dig_P9 * pressure * pressure / 2147483648.0 var2 = pressure * dig_P8 / 32768.0 pressure = pressure + (var1 + var2 + dig_P7) / 16.0 # Refine humidity humidity = t_fine - 76800.0 humidity = (hum_raw - (dig_H4 * 64.0 + dig_H5 / 16384.0 * humidity)) * (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * humidity * (1.0 + dig_H3 / 67108864.0 * humidity))) humidity = humidity * (1.0 - dig_H1 * humidity / 524288.0) if humidity > 100: humidity = 100 elif humidity < 0: humidity = 0 return temperature/100.0,pressure/100.0,humidity def main(): (chip_id, chip_version) = readBME280ID() print "Chip ID :", chip_id print "Version :", chip_version temperature,pressure,humidity = readBME280All() print "Temperature : ", temperature, "C" print "Pressure : ", pressure, "hPa" print "Humidity : ", humidity, "%" if __name__=="__main__": main() |
Output:
Importeren vanuit een ander script is mogelijk, gebruik onderstaand voorbeeld:
1 2 3 4 5 6 7 8 9 10 11 |
import bme280 (chip_id, chip_version) = bme280.readBME280ID() print "Chip ID :", chip_id print "Version :", chip_version temperature,pressure,humidity = bme280.readBME280All() print "Temperature : ", temperature, "C" print "Pressure : ", pressure, "hPa" print "Humidity : ", humidity, "%" |
Bronnen:
ControlEverythongCommunity @ Github.com
raspberrypi-spy.co.uk
Schema
Teardown
GEEN GEGEVENS
Datasheet
Fritzing
Downloads
GEEN GEGEVENS