{"id":86478,"date":"2019-07-04T16:48:25","date_gmt":"2019-07-04T16:48:25","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=86478"},"modified":"2020-07-30T14:11:39","modified_gmt":"2020-07-30T14:11:39","slug":"esp32-bme280-arduino-ide-pressure-temperature-humidity","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/","title":{"rendered":"ESP32 with BME280 Sensor using Arduino IDE (Pressure, Temperature, Humidity)"},"content":{"rendered":"\n<p>This guide shows how to use the BME280 sensor module with the ESP32 to read pressure, temperature, humidity and estimate altitude using Arduino IDE. The BME280 sensor uses I2C or SPI communication protocol to exchange data with a microcontroller.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with BME280 Sensor using Arduino IDE Pressure, Temperature, and Humidity\" class=\"wp-image-86631\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>We\u2019ll show you how to wire the sensor to the ESP32, install the required libraries, and write a simple sketch that displays the sensor readings. <\/p>\n\n\n\n<p><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\">ESP32 Web Server with BME280 \u2013 Weather Station<\/a> <\/p>\n\n\n\n<p>Before proceeding with this tutorial you should have the ESP32 add-on installed in your Arduino IDE. Follow the next tutorial to install the ESP32 on the Arduino IDE, if you haven\u2019t already.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\">Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, and Linux instructions)<\/a><\/li><\/ul>\n\n\n\n<p>You might also like reading other BME280 guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\">ESP32 Web Server with BME280 \u2013 Weather Station<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/\">ESP8266 with BME280 using Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-bme280-esp32-esp8266\/\">ESP32\/ESP8266 with BME280 using MicroPython<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/bme280-sensor-arduino-pressure-temperature-humidity\/\">Arduino Board with BME280<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing BME280 Sensor Module<\/h2>\n\n\n\n<p>The <a rel=\"noreferrer noopener\" aria-label=\"BME280 sensor (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\">BME280 sensor<\/a> module reads barometric pressure, temperature, and humidity. Because pressure changes with altitude, you can also estimate altitude. There are several versions of this sensor module. We\u2019re using the module illustrated in the figure below.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1200\" height=\"722\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-sensor.jpg?resize=1200%2C722&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 Sensor I2C Module reads pressure, temperature, and humidity\" class=\"wp-image-86437\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-sensor.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-sensor.jpg?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-sensor.jpg?resize=768%2C462&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-sensor.jpg?resize=1024%2C616&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>This sensor communicates using I2C communication protocol, so the wiring is very simple. You can use the default ESP32 I2C pins as shown in the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BME280<\/strong><\/td><td><strong>ESP32<\/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 rntcblue\">GPIO 22<\/span><\/td><\/tr><tr><td>SDA<\/td><td><span class=\"rnthl rntcgreen\">GPIO 21<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>There are other versions of this sensor that can use either SPI or I2C communication protocols, like the module shown in the next figure:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/BME280-SPI.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 Sensor Module SPI or I2C communication protocols\" class=\"wp-image-86630\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/BME280-SPI.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/BME280-SPI.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>If you&#8217;re using one of these sensors, to use I2C communication protocol, use the following pins:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BME280<\/strong><\/td><td><strong>ESP32<\/strong><\/td><\/tr><tr><td>SCK (SCL Pin)<\/td><td> <span class=\"rnthl rntcblue\">GPIO 22<\/span><\/td><\/tr><tr><td>SDI (SDA pin) <\/td><td> <span class=\"rnthl rntcgreen\">GPIO 21<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>If you use SPI communication protocol, you need to use the following pins:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BME280<\/strong><\/td><td><strong>ESP32<\/strong><\/td><\/tr><tr><td> SCK (SPI Clock)<\/td><td><span class=\"rnthl rntcblue\">GPIO 18<\/span><\/td><\/tr><tr><td> SDO&nbsp;(MISO)<\/td><td><span class=\"rnthl rntcyellow\">GPIO 19<\/span><\/td><\/tr><tr><td> SDI (MOSI)<\/td><td><span class=\"rnthl rntcgreen\">GPIO 23<\/span><\/td><\/tr><tr><td> CS (Chip Select) <\/td><td><span class=\"rnthl rntcbrown\">GPIO 5<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>To complete this tutorial you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\">BME280 sensor module<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a><span style=\"font-size: inherit; background-color: initial;\">&nbsp;(read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">Best ESP32 development boards<\/a>)<\/span><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Jumper wires<\/a><\/li><\/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\">Schematic &#8211; ESP32 with BME280 using I2C<\/h2>\n\n\n\n<p>We\u2019re going to use I2C communication with the BME280 sensor module. For that, wire the sensor to the ESP32&nbsp;<span class=\"rnthl rntcgreen\">SDA<\/span>&nbsp;and&nbsp;<span class=\"rnthl rntcblue\">SCL<\/span>&nbsp;pins, as shown in the following schematic diagram. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"669\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-bme280_schematic.jpg?resize=768%2C669&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with BME280 Wiring Schematic Diagram I2C\" class=\"wp-image-86439\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-bme280_schematic.jpg?w=768&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-bme280_schematic.jpg?resize=300%2C261&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference Guide<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing the BME280 library<\/h2>\n\n\n\n<p>To get readings from the BME280 sensor module you need to use the <a href=\"https:\/\/github.com\/adafruit\/Adafruit_BME280_Library\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Adafruit_BME280 library<\/a>. Follow the next steps to install the library in your Arduino IDE:<\/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 bme280<\/strong> \u201d on the Search box and install the library.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing BME280 library in Arduino IDE\" class=\"wp-image-70233\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/install-bme280.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Installing the Adafruit_Sensor library<\/h2>\n\n\n\n<p>To use the BME280 library, you also need to install the <a href=\"https:\/\/github.com\/adafruit\/Adafruit_Sensor\" target=\"_blank\" rel=\"noreferrer noopener\">Adafruit_Sensor library<\/a>. Follow the next steps to install the library in your Arduino IDE:<\/p>\n\n\n\n<p>Go to <strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong> and type \u201c<strong>Adafruit Unified Sensor<\/strong>\u201d in the search box. Scroll all the way down to find the library and install it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing Adafruit Unified Sensor Driver library\" class=\"wp-image-84295\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>After installing the libraries, restart your Arduino IDE.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reading Pressure, Temperature, and Humidity <\/h2>\n\n\n\n<p>To read pressure, temperature, and humidity we&#8217;ll use a sketch example from the library.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280-circuit.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with BME280 Barometric Pressure Sensor and programmed with Arduino IDE\" class=\"wp-image-86632\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280-circuit.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280-circuit.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>After installing the BME280 library, and the Adafruit_Sensor library, open the Arduino IDE and, go to <strong>File<\/strong> &gt; <strong>Examples<\/strong> &gt; <strong>Adafruit BME280 library<\/strong> &gt; <strong>bme280 test<\/strong>.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Complete project details at http:\/\/randomnerdtutorials.com  \n*********\/\n\n#include &lt;Wire.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n#include &lt;Adafruit_BME280.h&gt;\n\n\/*#include &lt;SPI.h&gt;\n#define BME_SCK 18\n#define BME_MISO 19\n#define BME_MOSI 23\n#define BME_CS 5*\/\n\n#define SEALEVELPRESSURE_HPA (1013.25)\n\nAdafruit_BME280 bme; \/\/ I2C\n\/\/Adafruit_BME280 bme(BME_CS); \/\/ hardware SPI\n\/\/Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); \/\/ software SPI\n\nunsigned long delayTime;\n\nvoid setup() {\n  Serial.begin(9600);\n  Serial.println(F(&quot;BME280 test&quot;));\n\n  bool status;\n\n  \/\/ default settings\n  \/\/ (you can also pass in a Wire library object like &amp;Wire2)\n  status = bme.begin(0x76);  \n  if (!status) {\n    Serial.println(&quot;Could not find a valid BME280 sensor, check wiring!&quot;);\n    while (1);\n  }\n\n  Serial.println(&quot;-- Default Test --&quot;);\n  delayTime = 1000;\n\n  Serial.println();\n}\n\n\nvoid loop() { \n  printValues();\n  delay(delayTime);\n}\n\nvoid printValues() {\n  Serial.print(&quot;Temperature = &quot;);\n  Serial.print(bme.readTemperature());\n  Serial.println(&quot; *C&quot;);\n  \n  \/\/ Convert temperature to Fahrenheit\n  \/*Serial.print(&quot;Temperature = &quot;);\n  Serial.print(1.8 * bme.readTemperature() + 32);\n  Serial.println(&quot; *F&quot;);*\/\n  \n  Serial.print(&quot;Pressure = &quot;);\n  Serial.print(bme.readPressure() \/ 100.0F);\n  Serial.println(&quot; hPa&quot;);\n\n  Serial.print(&quot;Approx. Altitude = &quot;);\n  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));\n  Serial.println(&quot; m&quot;);\n\n  Serial.print(&quot;Humidity = &quot;);\n  Serial.print(bme.readHumidity());\n  Serial.println(&quot; %&quot;);\n\n  Serial.println();\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-Course\/raw\/master\/code\/bme280test\/bme280test.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>We&#8217;ve made a few modifications to the sketch to make it fully compatible with the ESP32.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>Continue reading this section to learn how the code works, or skip to the &#8220;Demonstration&#8221; 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, and the <span class=\"rnthl rntliteral\">Adafruit_Sensor<\/span> and <span class=\"rnthl rntliteral\">Adafruit_BME280<\/span> libraries to interface with the BME280 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Wire.h>\n#include &lt;Adafruit_Sensor.h>\n#include &lt;Adafruit_BME280.h><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">SPI communication<\/h3>\n\n\n\n<p>As we&#8217;re going to use I2C communication, the following lines that define the SPI pins are commented:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/*#include &lt;SPI.h>\n#define BME_SCK 18\n#define BME_MISO 19\n#define BME_MOSI 23\n#define BME_CS 5*\/<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> if you&#8217;re using SPI communication, you use the ESP32 SPI pins. For SPI communication on the ESP32 you can use either the HSPI or VSPI pins, as shown in the following table.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>SPI<\/strong><\/td><td><strong>MOSI<\/strong><\/td><td><strong>MISO<\/strong><\/td><td><strong>CLK<\/strong><\/td><td><strong>CS<\/strong><\/td><\/tr><tr><td>HSPI <\/td><td>GPIO 13 <\/td><td>GPIO 12<\/td><td>GPIO 14<\/td><td>GPIO 15<\/td><\/tr><tr><td>VSPI <\/td><td>GPIO 23<\/td><td>GPIO 19<\/td><td>GPIO 18<\/td><td>GPIO 5<\/td><\/tr><\/tbody><\/table><\/figure>\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 more 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. As you can see, you just need to create an <span class=\"rnthl rntliteral\">Adafruit_BME280<\/span> object called <span class=\"rnthl rntliteral\">bme<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_BME280 bme; \/\/ I2C<\/code><\/pre>\n\n\n\n<p>To use SPI, you need to comment this previous line and uncomment one of the following lines depending on whether you\u2019re using hardware or software SPI (hardware SPI uses the ESP32 default HSPI pins; software SPI uses the pins defined on the code).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/Adafruit_BME280 bme(BME_CS); \/\/ hardware SPI\n\/\/Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); \/\/ software SPI<\/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(9600);<\/code><\/pre>\n\n\n\n<p>And initialize the sensor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>status = bme.begin(0x76); \nif (!status) {\n  Serial.println(\"Could not find a valid BME280 sensor, check wiring!\");\n  while (1);\n}<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclblue\">We initialize the sensor with the <span class=\"rnthl rntliteral\">0x76<\/span> address. In case you&#8217;re not getting sensor readings, check the I2C address of your sensor. With the BME280 sensor wired to your ESP32, run <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/master\/Projects\/LCD_I2C\/I2C_Scanner.ino\" target=\"_blank\">this I2C scanner sketch<\/a> to check the address of your sensor. Then, change the address if needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Printing values<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, the <span class=\"rnthl rntliteral\">printValues()<\/span> function reads the values from the BME280 and prints the results in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop() { \n  printValues();\n  delay(delayTime);\n}<\/code><\/pre>\n\n\n\n<p>Reading temperature, humidity, pressure, and estimate altitude is as simple as using the following methods on the <span class=\"rnthl rntliteral\">bme<\/span> object:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span class=\"rnthl rntliteral\">bme.readTemperature()<\/span> &#8211; reads temperature in Celsius;<\/li><li><span class=\"rnthl rntliteral\">bme.readHumidity()<\/span> &#8211; reads absolute humidity;<\/li><li><span class=\"rnthl rntliteral\">bme.readPressure()<\/span> &#8211; reads pressure in hPa (hectoPascal = millibar);<\/li><li><span class=\"rnthl rntliteral\">bme.readAltitude(SEALEVELPRESSURE_HPA)<\/span> &#8211; estimates altitude in meters based on the pressure at the sea level.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to your ESP32, and open the Serial Monitor at a baud rate of 9600. Press the on-board RST button to run the code. You should see the readings displayed on the Serial Monitor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"469\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-readings-arduino-ide.png?resize=666%2C469&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Printing BME280 pressure, temperature and humidity readings with ESP32 in Arduino IDE Serial Monitor\" class=\"wp-image-86486\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-readings-arduino-ide.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-readings-arduino-ide.png?resize=300%2C211&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32 Web Server Weather Station with BME280 Sensor<\/h2>\n\n\n\n<p>The BME280 sensor measures temperature, humidity, and pressure. So, you can easily build a <a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\">compact weather station<\/a> and monitor the measurements using a web server built with your ESP32.<\/p>\n\n\n\n<p>To do that, you can follow this tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\">ESP32 Web Server with BME280 Weather Station<\/a><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/BME_ESP32_WEB_SERVER.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Web Server with BME280 Sensor Weather Station\" class=\"wp-image-69102\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/BME_ESP32_WEB_SERVER.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/BME_ESP32_WEB_SERVER.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/BME_ESP32_WEB_SERVER.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/BME_ESP32_WEB_SERVER.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This article was a quick guide on how to get pressure, temperature and humidity readings from a BME280 sensor with the ESP32 using Arduino IDE. <\/p>\n\n\n\n<p>Now, you can take this project further and display your sensor readings in an <a href=\"https:\/\/randomnerdtutorials.com\/esp32-ssd1306-oled-display-arduino-ide\/\">OLED display<\/a>; create a datalogger; or <a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\">create a web server to display the latest sensor readings.<\/a> Here&#8217;s a list of projects that might help with these ideas:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-publish-sensor-readings-to-google-sheets\/\">ESP32 Publish Sensor Readings to Google Sheets<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-ssd1306-oled-display-arduino-ide\/\">ESP32 OLED Display with Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-with-bme280-mini-weather-station\/\">ESP32 Web Server with BME280 \u2013 Weather Station<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/low-power-weather-station-datalogger-using-esp8266-bme280-micropython\/\">Low Power Weather Station Datalogger (MicroPython) <\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-mysql-database-php\/\">ESP32\/ESP8266 Insert Data into MySQL Database using PHP and Arduino IDE<\/a><\/li><\/ul>\n\n\n\n<p class=\"rntbox rntclblue\">If you want to learn more about the ESP32, make sure you enroll in our course: &#8220;<a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\"><strong>Learn ESP32 with Arduino IDE<\/strong><\/a>&#8220;.<\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide shows how to use the BME280 sensor module with the ESP32 to read pressure, temperature, humidity and estimate altitude using Arduino IDE. The BME280 sensor uses I2C or &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 with BME280 Sensor using Arduino IDE (Pressure, Temperature, Humidity)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/#more-86478\" aria-label=\"Read more about ESP32 with BME280 Sensor using Arduino IDE (Pressure, Temperature, Humidity)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":86631,"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":[281,276,277,299,264],"tags":[],"class_list":["post-86478","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/ESP32-BME280.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/86478","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=86478"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/86478\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/86631"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=86478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=86478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=86478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}