Advertisement
microrobotics

BMP280 High precision Atmospheric Pressure Sensor

Apr 17th, 2023 (edited)
3,289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Here's some sample Arduino code to read data from the BMP280 High Precision Atmospheric Pressure Sensor:
  3.  
  4. This code uses the Adafruit BMP280 library to interface with the sensor over I2C. The library provides functions to read temperature and pressure values from the sensor.
  5.  
  6. In the setup() function, the I2C bus is initialized and the BMP280 sensor is initialized with the sampling mode set to MODE_NORMAL, which provides a balance of accuracy and power consumption.
  7.  
  8. In the loop() function, temperature and pressure values are read from the sensor using the readTemperature() and readPressure() functions, respectively. The values are then printed to the serial monitor using the Serial.print() and Serial.println() functions.
  9.  
  10. The code then delays for 1 second using the delay() function before taking the next measurement. This sequence repeats indefinitely.
  11.  
  12. Note that the specific I2C address of the BMP280 sensor may vary depending on the specific module you have. In the example code, the address is set to 0x76. You should refer to the documentation or specifications of your BMP280 module to determine the correct I2C address.
  13. */
  14.  
  15. #include <Wire.h>
  16. #include <Adafruit_Sensor.h>
  17. #include <Adafruit_BMP280.h>
  18.  
  19. // create a BMP280 object
  20. Adafruit_BMP280 bmp;
  21.  
  22. void setup() {
  23.   // initialize the serial port for debugging
  24.   Serial.begin(9600);
  25.   while (!Serial);
  26.  
  27.   // initialize the I2C bus
  28.   Wire.begin();
  29.  
  30.   // initialize the BMP280 sensor
  31.   if (!bmp.begin(0x76)) {
  32.     Serial.println("Could not find a valid BMP280 sensor, check wiring!");
  33.     while (1);
  34.   }
  35.  
  36.   // set the sampling mode
  37.   bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
  38.                   Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
  39.                   Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
  40.                   Adafruit_BMP280::FILTER_X16,      /* Filtering. */
  41.                   Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
  42. }
  43.  
  44. void loop() {
  45.   // read temperature and pressure values from the sensor
  46.   float temperature = bmp.readTemperature();
  47.   float pressure = bmp.readPressure() / 100.0F;
  48.  
  49.   // print the values to the serial monitor
  50.   Serial.print("Temperature = ");
  51.   Serial.print(temperature);
  52.   Serial.println(" *C");
  53.  
  54.   Serial.print("Pressure = ");
  55.   Serial.print(pressure);
  56.   Serial.println(" hPa");
  57.  
  58.   // delay for 1 second before taking the next measurement
  59.   delay(1000);
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement