{"id":161196,"date":"2024-08-27T13:36:04","date_gmt":"2024-08-27T13:36:04","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=161196"},"modified":"2024-08-30T12:43:33","modified_gmt":"2024-08-30T12:43:33","slug":"raspberry-pi-pico-bme680-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-bme680-arduino\/","title":{"rendered":"Raspberry Pi Pico: BME680 Environmental Sensor (Arduino IDE)"},"content":{"rendered":"\n<p>Get started with the BME680 environmental sensor module with the Raspberry Pi Pico board programmed with the Arduino IDE to get data about temperature, humidity, pressure, and gas (air quality). We\u2019ll show you how to wire the sensor, install the required libraries, and write a simple sketch to display sensor readings.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/Raspberry-Pi-Pico-BME680-Arduino-IDE.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico BME680 Environmental Sensor Arduino IDE\" class=\"wp-image-161197\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/Raspberry-Pi-Pico-BME680-Arduino-IDE.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/Raspberry-Pi-Pico-BME680-Arduino-IDE.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/Raspberry-Pi-Pico-BME680-Arduino-IDE.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/Raspberry-Pi-Pico-BME680-Arduino-IDE.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/Raspberry-Pi-Pico-BME680-Arduino-IDE.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\"><strong>New to the Raspberry Pi Pico?<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/\" title=\"\">Get started with the Raspberry Pi Pico here<\/a>.<\/p>\n\n\n\n<p><strong>Table of Contents<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#bme680-sensor-intro\" title=\"\">Introducing BME680 Environmental Sensor Module<\/a><\/li>\n\n\n\n<li><a href=\"#bme680-rpi-pico-wiring\" title=\"\">Wiring the BME680 to the Raspberry Pi Pico<\/a><\/li>\n\n\n\n<li><a href=\"#bme680-arduino-library\" title=\"\">Installing the BME680 Library for Raspberry Pi Pico &#8211; Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"#bme680-arduino-ide-code-raspberry-pi-pico\" title=\"\">Code &#8211; Reading BME680 Gas, Pressure, Humidity, and Temperature<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"rpi-pico-arduino-ide\">Raspberry Pi Pico with Arduino IDE<\/h2>\n\n\n\n<p>You need to install the Raspberry Pi Pico boards on Arduino IDE and you must know how to upload code to the board. Check out the following tutorial first if you haven\u2019t already:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/programming-raspberry-pi-pico-w-arduino-ide\/\">Programming Raspberry Pi Pico with Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bme680-sensor-intro\">Introducing BME680 Environmental Sensor Module<\/h2>\n\n\n\n<p>The BME680 is an environmental sensor that combines gas, pressure, humidity, and temperature sensors. The gas sensor can detect a broad range of gases like volatile organic compounds (VOC). For this reason, the BME680 can be used in indoor air quality control.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-sensor-humidity-barometric-pressure-ambient-temperature-gas-air-quality-front.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME680 Gas sensor humidity barometric pressure ambient temperature gas air quality front\" class=\"wp-image-98118\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-sensor-humidity-barometric-pressure-ambient-temperature-gas-air-quality-front.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-sensor-humidity-barometric-pressure-ambient-temperature-gas-air-quality-front.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">BME680 Measurements<\/h3>\n\n\n\n<p>The BME680 is a 4-in-1 digital sensor that measures:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Temperature<\/li>\n\n\n\n<li>Humidity<\/li>\n\n\n\n<li>Barometric pressure<\/li>\n\n\n\n<li>Gas: Volatile Organic Compounds (VOC) like ethanol and carbon monoxide<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Gas Sensor<\/h3>\n\n\n\n<p>The BME680 contains a MOX (Metal-oxide) sensor that detects VOCs in the air. This sensor gives you a qualitative idea of the<strong> sum of VOCs\/contaminants<\/strong> in the surrounding air &#8211; <strong>it is not specific<\/strong> for a specific gas molecule.<\/p>\n\n\n\n<p>MOX sensors are composed of a metal-oxide surface, a sensing chip to measure changes in conductivity and a heater. It detects VOCs by adsorption of oxygen molecules on its sensitive layer. The BME680 reacts to most VOCs polluting indoor air (except CO2).<\/p>\n\n\n\n<p>When the sensor comes into contact with the reducing gases, the oxygen molecules react and increase the conductivity across the surface. As a raw signal, the BME680 outputs resistance values. These values change due to variations in VOC concentrations:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1112\" height=\"726\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-Sensor-Resistance-How-It-Works.jpg?resize=1112%2C726&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME680 Gas Environmental Air Quality Sensor Resistance How It Works\" class=\"wp-image-98136\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-Sensor-Resistance-How-It-Works.jpg?w=1112&amp;quality=100&amp;strip=all&amp;ssl=1 1112w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-Sensor-Resistance-How-It-Works.jpg?resize=300%2C196&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-Sensor-Resistance-How-It-Works.jpg?resize=1024%2C669&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-Sensor-Resistance-How-It-Works.jpg?resize=768%2C501&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1112px) 100vw, 1112px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Higher <\/strong>concentration of VOCs \u00bb <strong>Lower <\/strong>resistance<\/li>\n\n\n\n<li><strong>Lower <\/strong>concentration of VOCs \u00bb <strong>Higher <\/strong>resistance<\/li>\n<\/ul>\n\n\n\n<p>The reactions that occur on the sensor surface (thus, the resistance) are influenced by parameters other than VOC concentration like temperature and humidity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Relevant Information Regarding Gas Sensor<\/h3>\n\n\n\n<p>The gas sensor gives you a qualitative idea of VOCs gases in the surrounding air. So, you can get trends, compare your results, and see if the air quality is increasing or decreasing. To get precise measurements, you must calibrate the sensor against known sources and build a calibration curve.<\/p>\n\n\n\n<p>When you first get the sensor, it is recommended to run it for 48 hours before starting to collect &#8220;real&#8221; data. After that, it is also recommended to run the sensor for 30 minutes before getting a gas reading.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">BME680 Accuracy<\/h3>\n\n\n\n<p>Here&#8217;s the accuracy of the temperature, humidity, and pressure sensors of the BME680:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sensor<\/strong><\/td><td><strong>Accuracy<\/strong><\/td><\/tr><tr><td>Temperature<\/td><td>+\/- 1.0\u00baC<\/td><\/tr><tr><td>Humidity<\/td><td>+\/- 3%<\/td><\/tr><tr><td>Pressure<\/td><td>+\/- 1 hPa<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">BME680 Operation Range<\/h3>\n\n\n\n<p>The following table shows the operation range for the temperature, humidity, and pressure sensors for the BME680.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Sensor<\/strong><\/td><td><strong>Operation <\/strong>Range<\/td><\/tr><tr><td>Temperature<\/td><td>-40 to 85 \u00baC<\/td><\/tr><tr><td>Humidity<\/td><td>0 to 100 %<\/td><\/tr><tr><td>Pressure<\/td><td>300 to 1100 hPa<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">BME680 Pinout<\/h3>\n\n\n\n<p>Here&#8217;s the BME680 Pinout:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>VCC<\/strong><\/td><td>Powers the sensor<\/td><\/tr><tr><td><strong>GND<\/strong><\/td><td>Common GND<\/td><\/tr><tr><td><strong>SCL<\/strong><\/td><td>SCL pin for I2C communication<br>SCK pin for SPI communication<\/td><\/tr><tr><td><strong>SDA<\/strong><\/td><td>SDA pin for I2C communication<br>SDI (MOSI) pin for SPI communication<\/td><\/tr><tr><td><strong>SDO<\/strong><\/td><td>SDO (MISO) pin for SPI communication<\/td><\/tr><tr><td><strong>CS<\/strong><\/td><td>Chip select pin for SPI communication<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">BME680 Interface<\/h3>\n\n\n\n<p>The BME680 supports I2C and SPI Interfaces.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-sensor-humidity-barometric-pressure-ambient-temperature-gas-air-quality-back.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME680 Gas sensor humidity barometric pressure ambient temperature gas air quality back\" class=\"wp-image-98119\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-sensor-humidity-barometric-pressure-ambient-temperature-gas-air-quality-back.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/BME680-Gas-sensor-humidity-barometric-pressure-ambient-temperature-gas-air-quality-back.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\">BME680 I2C<\/h4>\n\n\n\n<p>This sensor communicates using I2C communication protocol, so the wiring is straighforward. You can use any I2C pin combination of the Raspberry Pi Pico. We&#8217;ll be using GPIO 5 (SCL) and GPIO 4 (SDA). You can use any <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/#i2c\" title=\"\">other combination of I2C pins<\/a> as long as you add them to the code.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BME680<\/strong><\/td><td><strong>Raspberry Pi Pico<\/strong><\/td><\/tr><tr><td>Vin<\/td><td><span class=\"rnthl rntcred\">3.3V<\/span><\/td><\/tr><tr><td>GND<\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><tr><td>SCL<\/td><td><span class=\"rnthl rntclgray\">GPIO 5<\/span><\/td><\/tr><tr><td>SDA<\/td><td><span class=\"rnthl rntcyellow\">GPIO 4<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"rntbox rntclgreen\">Learn more about the Raspberry Pi Pico GPIOs: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/\">Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/Rpi-Pico-with-BME680-sensor.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with BME680 environmental sensor\" class=\"wp-image-161188\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/Rpi-Pico-with-BME680-sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/Rpi-Pico-with-BME680-sensor.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>For this project, you need to wire the BME680 sensor module to the Raspberry Pi Pico I2C pins. Here\u2019s a list of parts you need for this tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a style=\"font-size: inherit; background-color: initial;\" rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/bme680-gas-sensor-module\/\" target=\"_blank\">BME680 sensor module<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico or Pico W<\/a><\/li>\n\n\n\n<li><a aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li>\n\n\n\n<li><a aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bme680-arduino-library\">Installing the BME680 Library for Raspberry Pi Pico &#8211; Arduino IDE<\/h2>\n\n\n\n<p>There are different libraries you can use to get readings from the BME680 sensor. We&#8217;ll use the <span class=\"rnthl rntliteral\">Adafruit_BME680<\/span> library.<\/p>\n\n\n\n<p>Open your Arduino IDE and go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open.<\/p>\n\n\n\n<p>Search for \u201c<strong>adafruit<\/strong>_<strong>bme680<\/strong>&nbsp;\u201d in the Search box and install the library by Adafruit. Install any other required dependencies.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"445\" height=\"388\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/install-adafruit-bme680-arduino-ide.png?resize=445%2C388&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"install adafruit BME680 arduino ide\" class=\"wp-image-161198\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/install-adafruit-bme680-arduino-ide.png?w=445&amp;quality=100&amp;strip=all&amp;ssl=1 445w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/install-adafruit-bme680-arduino-ide.png?resize=300%2C262&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 445px) 100vw, 445px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"bme680-rpi-pico-wiring\">Wiring the BME680 to the Raspberry Pi Pico<\/h2>\n\n\n\n<p>Wire the BME680 to any <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/#i2c\" title=\"\">combination of the Pico I2C pins<\/a>\u2014we&#8217;ll be using <span class=\"rnthl rntcyellow\">GPIO 4<\/span> (SDA) and <span class=\"rnthl rntclgray\">GPIO 5<\/span> (SCL).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"836\" height=\"634\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/RPi-Pico-BME680-wiring-circuit.png?resize=836%2C634&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico wiring to BME680 sensor circuit diagram\" class=\"wp-image-161189\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/RPi-Pico-BME680-wiring-circuit.png?w=836&amp;quality=100&amp;strip=all&amp;ssl=1 836w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/RPi-Pico-BME680-wiring-circuit.png?resize=300%2C228&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/RPi-Pico-BME680-wiring-circuit.png?resize=768%2C582&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 836px) 100vw, 836px\" \/><\/figure><\/div>\n\n\n<p><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/\">Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bme680-arduino-ide-code-raspberry-pi-pico\">Code &#8211; Reading BME680 Gas, Pressure, Humidity, and Temperature<\/h2>\n\n\n\n<p>To show you how to read temperature, humidity, and pressure from the BME680, we\u2019ll use a simple sketch that prints the current readings every second to the Serial Monitor. The following example was adapted from the Adafruit BME680 library examples.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-bme680-arduino\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*********\/\n\n#include &lt;Wire.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n#include &quot;Adafruit_BME680.h&quot;\n\n#define SEALEVELPRESSURE_HPA (1013.25)\n\nAdafruit_BME680 bme; \/\/ I2C (default pins for Raspberry Pi Pico: GPIO 4 (SDA), GPIO 5(SCL)\n\nvoid setup() {\n  Serial.begin(115200);\n  while (!Serial);\n  Serial.println(F(&quot;BME680 async test&quot;));\n\n  if (!bme.begin()) {\n    Serial.println(F(&quot;Could not find a valid BME680 sensor, check wiring!&quot;));\n    while (1);\n  }\n\n  \/\/ Set up oversampling and filter initialization\n  bme.setTemperatureOversampling(BME680_OS_8X);\n  bme.setHumidityOversampling(BME680_OS_2X);\n  bme.setPressureOversampling(BME680_OS_4X);\n  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);\n  bme.setGasHeater(320, 150); \/\/ 320*C for 150 ms\n}\n\nvoid loop() {\n  \/\/ Tell BME680 to begin measurement.\n  unsigned long endTime = bme.beginReading();\n  if (endTime == 0) {\n    Serial.println(F(&quot;Failed to begin reading :(&quot;));\n    return;\n  }\n  Serial.print(F(&quot;Reading started at &quot;));\n  Serial.print(millis());\n  Serial.print(F(&quot; and will finish at &quot;));\n  Serial.println(endTime);\n\n  Serial.println(F(&quot;You can do other work during BME680 measurement.&quot;));\n  delay(50); \/\/ This represents parallel work.\n  \/\/ There's no need to delay() until millis() &gt;= endTime: bme.endReading()\n  \/\/ takes care of that. It's okay for parallel work to take longer than\n  \/\/ BME680's measurement time.\n\n  \/\/ Obtain measurement results from BME680. Note that this operation isn't\n  \/\/ instantaneous even if milli() &gt;= endTime due to I2C\/SPI latency.\n  if (!bme.endReading()) {\n    Serial.println(F(&quot;Failed to complete reading :(&quot;));\n    return;\n  }\n  Serial.print(F(&quot;Reading completed at &quot;));\n  Serial.println(millis());\n\n  Serial.print(F(&quot;Temperature = &quot;));\n  Serial.print(bme.temperature);\n  Serial.println(F(&quot; *C&quot;));\n\n  Serial.print(F(&quot;Pressure = &quot;));\n  Serial.print(bme.pressure \/ 100.0);\n  Serial.println(F(&quot; hPa&quot;));\n\n  Serial.print(F(&quot;Humidity = &quot;));\n  Serial.print(bme.humidity);\n  Serial.println(F(&quot; %&quot;));\n\n  Serial.print(F(&quot;Gas = &quot;));\n  Serial.print(bme.gas_resistance \/ 1000.0);\n  Serial.println(F(&quot; KOhms&quot;));\n\n  Serial.print(F(&quot;Approx. Altitude = &quot;));\n  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));\n  Serial.println(F(&quot; m&quot;));\n\n  Serial.println();\n  delay(2000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/Arduino\/BME680_Basic.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Does the Code Work<\/h2>\n\n\n\n<p>Continue reading this section to learn how the code works, or skip to the <a href=\"#demonstration\">Demonstration <\/a>section.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Libraries<\/h3>\n\n\n\n<p>The code starts by including the needed libraries: the <span class=\"rnthl rntliteral\">wire<\/span> library to use I2C, the <span class=\"rnthl rntliteral\">Adafruit_Sensor<\/span> and <span class=\"rnthl rntliteral\">Adafruit_BME680<\/span> libraries to interface with the BME680 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Wire.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n#include \"Adafruit_BME680.h\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Sea level pressure<\/h3>\n\n\n\n<p>A variable called <span class=\"rnthl rntliteral\">SEALEVELPRESSURE_HPA<\/span> is created.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SEALEVELPRESSURE_HPA (1013.25)<\/code><\/pre>\n\n\n\n<p>This variable saves the pressure at the sea level in hectopascal (is equivalent to milibar). This variable is used to estimate the altitude for a given pressure by comparing it with the sea level pressure. This example uses the default value, but for accurate results, replace the value with the current sea level pressure at your location.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">I2C<\/h3>\n\n\n\n<p>This example uses I2C communication protocol by default. The following line creates an <span class=\"rnthl rntliteral\">Adafruit_BME680<\/span> object called <span class=\"rnthl rntliteral\">bme<\/span> on the following RPi Pico GPIOs: <span class=\"rnthl rntcyellow\">GPIO 5<\/span> (SCL), <span class=\"rnthl rntclgray\">GPIO 4<\/span> (SDA).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_BME680 bme; \/\/ I2C<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span> start a serial communication.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Init BME680 Sensor<\/h4>\n\n\n\n<p>Initialize the BME680 sensor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (!bme.begin()) {\n  Serial.println(F(\"Could not find a valid BME680 sensor, check wiring!\"));\n  while (1);\n}<\/code><\/pre>\n\n\n\n<p>Set up the following parameters (oversampling, filter, and gas heater) for the sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Set up oversampling and filter initialization\nbme.setTemperatureOversampling(BME680_OS_8X);\nbme.setHumidityOversampling(BME680_OS_2X);\nbme.setPressureOversampling(BME680_OS_4X);\nbme.setIIRFilterSize(BME680_FILTER_SIZE_3);\nbme.setGasHeater(320, 150); \/\/ 320*C for 150 ms<\/code><\/pre>\n\n\n\n<p>To increase the resolution of the raw sensor data, it supports oversampling. We&#8217;ll use the default oversampling parameters, but you can change them.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">setTemperatureOversampling()<\/span>: set temperature oversampling.<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">setHumidityOversampling()<\/span>: set humidity oversampling.<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">setPressureOversampling()<\/span>: set pressure oversampling.<\/li>\n<\/ul>\n\n\n\n<p>These methods can accept one of the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">BME680_OS_NONE<\/span>: turn off reading;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_OS_1X<\/span><\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_OS_2X<\/span><\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_OS_4X<\/span><\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_OS_8X<\/span><\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_OS_16X<\/span><\/li>\n<\/ul>\n\n\n\n<p>The BME680 sensor integrates an internal IIR filter to reduce short-term changes in sensor output values caused by external disturbances. The <span class=\"rnthl rntliteral\">setIIRFilterSize()<\/span> method sets the IIR filter. It accepts the filter size as a parameter:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">BME680_FILTER_SIZE_0<\/span> (no filtering)<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_FILTER_SIZE_1<\/span><\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_FILTER_SIZE_3<\/span><\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_FILTER_SIZE_7<\/span> <\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_FILTER_SIZE_15<\/span> <\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_FILTER_SIZE_31<\/span><\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_FILTER_SIZE_63<\/span><\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">BME680_FILTER_SIZE_127<\/span><\/li>\n<\/ul>\n\n\n\n<p>The gas sensor integrates a heater. Set the heater profile using the <span class=\"rnthl rntliteral\">setGasHeater()<\/span> method that accepts as arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the heater temperature (in degrees Centigrade) <\/li>\n\n\n\n<li>the time the heater should be on (in milliseconds)<\/li>\n<\/ul>\n\n\n\n<p>We&#8217;ll use the default settings: 320 \u00baC for 150 ms.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">loop()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we&#8217;ll get measurements from the BME680 sensor.<\/p>\n\n\n\n<p>First, tell the sensor to start an asynchronous reading with <span class=\"rnthl rntliteral\">bme.beginReading()<\/span>. This returns the time when the reading would be ready. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Tell BME680 to begin measurement.\nunsigned long endTime = bme.beginReading();\nif (endTime == 0) {\n  Serial.println(F(\"Failed to begin reading :(\"));\n  return;\n}\nSerial.print(F(\"Reading started at \"));\nSerial.print(millis());\nSerial.print(F(\" and will finish at \"));\nSerial.println(endTime);<\/code><\/pre>\n\n\n\n<p>Then, call the <span class=\"rnthl rntliteral\">endReading()<\/span> method to end an asynchronous reading. If the asynchronous reading is still in progress, block until it ends.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (!bme.endReading()) {\n  Serial.println(F(\"Failed to complete reading :(\"));\n  return;\n}<\/code><\/pre>\n\n\n\n<p>After this, we can get the readings as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">bme.temperature<\/span>: returns temperature reading<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">bme.pressure<\/span>: returns pressure reading<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">bme.humidity<\/span>: returns humidity reading<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">bme.gas_resistance<\/span>: returns gas resistance<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(F(\"Temperature = \"));\nSerial.print(bme.temperature);\nSerial.println(F(\" *C\"));\n\nSerial.print(F(\"Pressure = \"));\nSerial.print(bme.pressure \/ 100.0);\nSerial.println(F(\" hPa\"));\n\nSerial.print(F(\"Humidity = \"));\nSerial.print(bme.humidity);\nSerial.println(F(\" %\"));\n\nSerial.print(F(\"Gas = \"));\nSerial.print(bme.gas_resistance \/ 1000.0);\nSerial.println(F(\" KOhms\"));\n\nSerial.print(F(\"Approx. Altitude = \"));\nSerial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));\nSerial.println(F(\" m\"));<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclgray\">For more information about the library methods, take a look at the <a rel=\"noreferrer noopener\" href=\"https:\/\/adafruit.github.io\/Adafruit_BME680\/html\/class_adafruit___b_m_e680.html#aaae0839c68035001f491661cb2d012fc\" target=\"_blank\">Adafruit_BME<\/a><a href=\"https:\/\/adafruit.github.io\/Adafruit_BME680\/html\/class_adafruit___b_m_e680.html#aaae0839c68035001f491661cb2d012fc\" target=\"_blank\" rel=\"noreferrer noopener\">6<\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/adafruit.github.io\/Adafruit_BME680\/html\/class_adafruit___b_m_e680.html#aaae0839c68035001f491661cb2d012fc\" target=\"_blank\">80 Class Reference<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Uploading the Code<\/h2>\n\n\n\n<p>Upload the code to the Raspberry Pi Pico.<\/p>\n\n\n\n<p>To upload code to the Raspberry Pi Pico, it needs to be in bootloader mode.<\/p>\n\n\n\n<p>If the Raspberry Pi is currently running MicroPython firmware, you need to manually put it into bootloader mode. For that, connect the Raspberry Pi Pico to your computer while holding the BOOTSEL button at the same time.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-Bottloader-mode.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Bootloader mode\" class=\"wp-image-130903\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-Bottloader-mode.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-Bottloader-mode.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>For future uploads using Arduino IDE, the board should go automatically into bootloader mode without the need to press the BOOTSEL button.<\/p>\n\n\n\n<p>Now, select your COM port in&nbsp;<strong>Tools&nbsp;<\/strong>&gt;&nbsp;<strong>Port<\/strong>. It may be the case that the COM port is grayed out. If that\u2019s the case, don\u2019t worry it will automatically find the port once you hit the upload button.<\/p>\n\n\n\n<p>Upload the code.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"30\" height=\"30\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/05\/arduino-2-0-upload-button.png?resize=30%2C30&amp;quality=100&amp;strip=all&amp;ssl=1\" alt=\"Arduino 2.0 Upload Button\" class=\"wp-image-103678\"\/><\/figure><\/div>\n\n\n<p>If you don&#8217;t know how to upload code to the Raspberry Pi Pico using Arduino IDE, check this tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/programming-raspberry-pi-pico-w-arduino-ide\/\" title=\"\">Programming the Raspberry Pi Pico with Arduino IDE<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"demonstration\">Demonstration<\/h2>\n\n\n\n<p>After uploading the code to the board, open the Serial Monitor at a baud rate of 115200. The sensor measurements will be displayed every 5 seconds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"566\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/rpi-pico-bme680-arduino-ide-demonstration.png?resize=750%2C566&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with BME680 using Arduino IDE - demonstration\" class=\"wp-image-161199\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/rpi-pico-bme680-arduino-ide-demonstration.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/rpi-pico-bme680-arduino-ide-demonstration.png?resize=300%2C226&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This tutorial was a getting started guide to the BME680 environmental and air quality sensor with the Raspberry Pi Pico using Arduino IDE.<\/p>\n\n\n\n<p>We hope you\u2019ve found this tutorial useful. We have tutorials for other popular environmental sensors:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-ds18b20-arduino\/\">Raspberry Pi Pico:&nbsp;<strong>DS18B20 Temperature Sensor<\/strong>&nbsp;(Arduino IDE) \u2013 Single and Multiple<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dht11-dht22-arduino\/\">Raspberry Pi Pico:&nbsp;<strong>DHT11\/DHT22 Temperature and Humidity Sensor<\/strong>&nbsp;(Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-bme280-arduino\/\" title=\"\">Raspberry Pi Pico: <strong>BME280 Get Temperature, Humidity, and Pressure<\/strong> (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the Raspberry Pi Pico with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\" title=\"\">Free Raspberry Pi Pico Projects and Tutorials<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico with MicroPython (eBook)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get started with the BME680 environmental sensor module with the Raspberry Pi Pico board programmed with the Arduino IDE to get data about temperature, humidity, pressure, and gas (air quality). &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: BME680 Environmental Sensor (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-bme680-arduino\/#more-161196\" aria-label=\"Read more about Raspberry Pi Pico: BME680 Environmental Sensor (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":161197,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324,325],"tags":[],"class_list":["post-161196","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-arduino-ide"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/07\/Raspberry-Pi-Pico-BME680-Arduino-IDE.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/161196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=161196"}],"version-history":[{"count":3,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/161196\/revisions"}],"predecessor-version":[{"id":161899,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/161196\/revisions\/161899"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/161197"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=161196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=161196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=161196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}