Then, the BME280 sensor gets initialized and in case of failure, an error message is printed on the serial monitor. This initiates the serial communication at a baud rate of 115200 by using the begin() function. If using SPI protocol instead, make sure to comment on this line and uncomment the lines which are initializing the SPI pins. Then, it defines the Adafruit_BME280 object named bme by setting it on the default I2C GPIO pins. The 1013.25 is the value that is set in default. You have to pass your sea-level pressure in hPa according to your location. It takes into account the sea level pressure of your current location and compares it with a given pressure to estimate the altitude.
![labview arduino i2c example labview arduino i2c example](https://1.bp.blogspot.com/-WyMq9C5u8q0/UTxGyZOjVyI/AAAAAAAACHo/oWn0d_k5ynU/s1600/sd-card-sdcard-module-reader-pic-microcontroller-arduino-wiring-circuit-guide-board-.jpg)
Next, the code defines the SEALEVELPRESSURE_HPA by creating a variable by the set name. If you want to use SPI protocol instead, just uncomment the following block of code which defines the default pins: /*#define BME_SCK 18 This code is using the I2C protocol to communicate with Arduino and BME280. The Adafruit_Sensor and the Adafruit_BME280 libraries which we installed in Arduino IDE earlier are also included as they are necessary to interface with the sensor. Both communication protocols are included. The Wire.h will allow us to communicate through the I2C protocol and the SPI.h allows us to communicate through the SPI protocol. The code starts with including all the necessary libraries which are needed for the proper functionality of the code. Now, let us understand how each part of the code works. Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)) Serial.print(bme.readPressure() / 100.0F) Serial.print(1.8 * bme.readTemperature() + 32) Serial.println("- Print BME280 readings-") Serial.println("Could not detect a BME280 sensor, Fix wiring Connections!") (you can also pass in a Wire library object like &Wire2) Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK) // software SPI Adafruit_BME280 bme(BME_CS) // hardware SPI *#include // uncomment his if you are using SPI interface This BM280 example sketch displays temperature, pressure, approximate altitude, and humidity readings on Arduino serial monitor. The figure below shows the BME280 sensor and its pinout.Īrduino Code – Getting BME280 Readings on Arduino Serial Monitor The ESP development boards communicate with the BME280 sensor through the I2C protocol to get temperature, barometric pressure, and relative humidity. With BME280 and the ESP boards, the Arduino acts as a master, and the BME280 sensor as a slave because it is an external device, acts as a slave. I2C means Inter-Integrated Circuit and works on the principle of the synchronous, multi-master multi-slave system.
![labview arduino i2c example labview arduino i2c example](https://i.ytimg.com/vi/_B2d3fVCwj8/maxresdefault.jpg)
![labview arduino i2c example labview arduino i2c example](https://4.bp.blogspot.com/-mEa1qNRD6xI/TjoXkWtej_I/AAAAAAAAAIk/WssILBt3C2U/s1600/arduino.png)
Although there are several different versions of BME280 available in the market, the one we will be studying uses the I2C communication protocol and SPI. This sensor uses I2C or SPI to communicate data with the micro-controllers. It is mostly used in web and mobile applications where low power consumption is key. The BME280 sensor is used to measure readings regarding ambient temperature, barometric pressure, and relative humidity.