{"id":134464,"date":"2023-09-27T22:14:10","date_gmt":"2023-09-27T22:14:10","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=134464"},"modified":"2023-09-27T22:32:01","modified_gmt":"2023-09-27T22:32:01","slug":"raspberry-pi-bme280-python","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-bme280-python\/","title":{"rendered":"Raspberry Pi: BME280 Temperature, Humidity and Pressure Sensor (Python)"},"content":{"rendered":"\n<p>Learn how to interface the BME280 sensor with the Raspberry Pi to get temperature, humidity, and pressure readings. You&#8217;ll learn how to connect the sensor to the Raspberry Pi board and write a Python script that gets sensor data and displays it on the terminal.<\/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\/2023\/08\/Raspberry-Pi-BME280-Temperature-Humidity-Tutorial.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Getting Started BME280 Pressure Temperature Humidity Sensor Python\" class=\"wp-image-134488\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/Raspberry-Pi-BME280-Temperature-Humidity-Tutorial.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/Raspberry-Pi-BME280-Temperature-Humidity-Tutorial.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/Raspberry-Pi-BME280-Temperature-Humidity-Tutorial.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/Raspberry-Pi-BME280-Temperature-Humidity-Tutorial.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p><strong>Table of Contents<\/strong><\/p>\n\n\n\n<p>Throughout this tutorial, we\u2019ll cover the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#prerequisites\" title=\"\">Prerequisites<\/a><\/li>\n\n\n\n<li><a href=\"#BME280-sensor\" title=\"\">Introducing BME280 Sensor Module<\/a><\/li>\n\n\n\n<li><a href=\"#BME280-sensor\" title=\"\">Parts Required<\/a><\/li>\n\n\n\n<li><a href=\"#enable-i2c\" title=\"\">Enable I2C on the Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"#bme280-rpi-wiring\" title=\"Wiring the BME280 to the Raspberry Pi\">Wiring the BME280 to the Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"#get-i2c-address\" title=\"\">Getting the Sensor I2C Address<\/a><\/li>\n\n\n\n<li><a href=\"#rpi-bme280-python-code\" title=\"\">Python Code for Raspberry Pi with BME280<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites<\/h2>\n\n\n\n<p>Before continuing with this tutorial, check the following prerequisites.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Get familiar with the Raspberry Pi board\u2014if you&#8217;re not familiar with the Raspberry Pi, you can read our <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-raspberry-pi\/\" title=\"\"><strong>Raspberry Pi Getting Started Guide here<\/strong>.<\/a><\/li>\n\n\n\n<li>You must know how to run and create Python files on your Raspberry Pi. We like to program our Raspberry Pi via SSH using an extension on VS Code. We have a detailed tutorial about that subject: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-remote-ssh-vs-code\/\" title=\"\"><strong>Programming Raspberry Pi Remotely using VS Code (Remote-SSH)<\/strong><\/a>.<\/li>\n\n\n\n<li>Know how to use the Raspberry Pi GPIOs so that you know how to wire the circuit properly. Read the following tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pinout-gpios\/\"><strong>Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs?<\/strong><\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"BME280-sensor\">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<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/bme280-sensor.jpg?resize=750%2C462&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 Sensor I2C Module reads pressure, temperature, and humidity\" class=\"wp-image-86437\" width=\"750\" height=\"462\"\/><\/figure><\/div>\n\n\n<p>This sensor communicates using I2C communication protocol, so the wiring is very simple. You can use the default Raspberry Pi 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>Raspberry<\/strong> Pi<\/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 rntcyellow\">GPIO 3<\/span><\/td><\/tr><tr><td>SDA<\/td><td><span class=\"rnthl rntclgray\">GPIO 2<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Learn more about the Raspberry Pi GPIOs<\/strong>: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pinout-gpios\/\">Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs?<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"parts-required\">Parts Required<\/h2>\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\/2023\/08\/BME280-with-Raspberry-Pi.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi with BME280 Sensor\" class=\"wp-image-134486\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/BME280-with-Raspberry-Pi.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/BME280-with-Raspberry-Pi.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>Here&#8217;s a list of parts you need to build the circuit:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-board\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi board<\/a> &#8211; read <a href=\"https:\/\/makeradvisor.com\/best-raspberry-pi-starter-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Best Raspberry Pi Starter Kits<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\" rel=\"noopener\" title=\"\">BME280 temperature and humidity sensor<\/a><\/li>\n\n\n\n<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>\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=\"enable-i2c\">Enable I2C on the Raspberry Pi<\/h2>\n\n\n\n<p>I2C communication is not enabled by default. You need to enable it manually. Open a terminal window on your Raspberry Pi and type the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo raspi-config<\/pre>\n\n\n\n<p>The following menu will open. Select <strong>Interface Options<\/strong>.<\/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=\"722\" height=\"292\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/raspi-config.png?resize=722%2C292&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Enable I2C communication\" class=\"wp-image-134472\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/raspi-config.png?w=722&amp;quality=100&amp;strip=all&amp;ssl=1 722w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/raspi-config.png?resize=300%2C121&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 722px) 100vw, 722px\" \/><\/figure><\/div>\n\n\n<p>Then, select the I2C option.<\/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=\"736\" height=\"288\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/raspi-config-i2c.png?resize=736%2C288&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Enable I2C communication\" class=\"wp-image-134473\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/raspi-config-i2c.png?w=736&amp;quality=100&amp;strip=all&amp;ssl=1 736w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/raspi-config-i2c.png?resize=300%2C117&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/figure><\/div>\n\n\n<p>Finally, enable I2C by selecting <strong>Yes<\/strong>.<\/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=\"483\" height=\"321\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/enabled-i2c.png?resize=483%2C321&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Enable I2C communication\" class=\"wp-image-134474\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/enabled-i2c.png?w=483&amp;quality=100&amp;strip=all&amp;ssl=1 483w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/enabled-i2c.png?resize=300%2C199&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 483px) 100vw, 483px\" \/><\/figure><\/div>\n\n\n<p>I2C should be successfully enabled.<\/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=\"485\" height=\"324\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/i2c-is-enabled.png?resize=485%2C324&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi I2C communication enabled\" class=\"wp-image-134475\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/i2c-is-enabled.png?w=485&amp;quality=100&amp;strip=all&amp;ssl=1 485w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/i2c-is-enabled.png?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 485px) 100vw, 485px\" \/><\/figure><\/div>\n\n\n<p>After enabling I2C, reboot your Raspberry Pi by running the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo reboot<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bme280-rpi-wiring\">Wiring the BME280 to the Raspberry Pi<\/h2>\n\n\n\n<p>Wire the BME280 to the Raspberry Pi default I2C pins.<\/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=\"900\" height=\"831\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/BME280-Raspberry-Pi_bb.png?resize=900%2C831&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Wiring BME280 Diagram\" class=\"wp-image-134476\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/BME280-Raspberry-Pi_bb.png?w=900&amp;quality=100&amp;strip=all&amp;ssl=1 900w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/BME280-Raspberry-Pi_bb.png?resize=300%2C277&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/BME280-Raspberry-Pi_bb.png?resize=768%2C709&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><\/figure><\/div>\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BME280<\/strong><\/td><td><strong>Raspberry Pi<\/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 rntclblue\">GPIO 3<\/span><\/td><\/tr><tr><td>SDA<\/td><td><span class=\"rnthl rntclgreen\">GPIO 2<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"get-i2c-address\">Getting the Sensor I2C Address<\/h2>\n\n\n\n<p>With the sensor connected to the Raspberry Pi, let&#8217;s check if the sensor is connected properly by searching for its I2C address.<\/p>\n\n\n\n<p>Open a Terminal window on your Raspberry Pi and run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo i2cdetect -y 1<\/pre>\n\n\n\n<p>It should show your sensor I2C address:<\/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=\"533\" height=\"210\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/detect-i2c-address-bme280.png?resize=533%2C210&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 Getting BME280 I2C Address\" class=\"wp-image-134477\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/detect-i2c-address-bme280.png?w=533&amp;quality=100&amp;strip=all&amp;ssl=1 533w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/detect-i2c-address-bme280.png?resize=300%2C118&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 533px) 100vw, 533px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Install BME280 Library<\/h2>\n\n\n\n<p>There are several libraries compatible with the Raspberry Pi to read from the BME280 sensor. We&#8217;ll be using the <strong>RPi.BME280<\/strong> library. This library is available to install through PIP (a package manager for Python packages).<\/p>\n\n\n\n<p>First, install or upgrade pip by running the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo pip install --upgrade pip<\/pre>\n\n\n\n<p>Then, run the following command to install the library using pip:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo pip install RPI.BME280<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"rpi-bme280-python-code\">Python Code for Raspberry Pi BME280 (Temperature, Humidity, and Pressure)<\/h2>\n\n\n\n<p>The following script gets temperature, humidity, and pressure from the BME280 sensor and prints the readings on the Python shell. Create a new Python file with a name of your choice, for example, <em>bme280_basic.py<\/em>, and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\">import time\nimport smbus2\nimport bme280\n\n# BME280 sensor address (default address)\naddress = 0x76\n\n# Initialize I2C bus\nbus = smbus2.SMBus(1)\n\n# Load calibration parameters\ncalibration_params = bme280.load_calibration_params(bus, address)\n\ndef celsius_to_fahrenheit(celsius):\n    return (celsius * 9\/5) + 32\n\nwhile True:\n    try:\n        # Read sensor data\n        data = bme280.sample(bus, address, calibration_params)\n\n        # Extract temperature, pressure, and humidity\n        temperature_celsius = data.temperature\n        pressure = data.pressure\n        humidity = data.humidity\n\n        # Convert temperature to Fahrenheit\n        temperature_fahrenheit = celsius_to_fahrenheit(temperature_celsius)\n\n        # Print the readings\n        print(&quot;Temperature: {:.2f} \u00b0C, {:.2f} \u00b0F&quot;.format(temperature_celsius, temperature_fahrenheit))\n        print(&quot;Pressure: {:.2f} hPa&quot;.format(pressure))\n        print(&quot;Humidity: {:.2f} %&quot;.format(humidity))\n\n        # Wait for a few seconds before the next reading\n        time.sleep(2)\n\n    except KeyboardInterrupt:\n        print('Program stopped')\n        break\n    except Exception as e:\n        print('An unexpected error occurred:', str(e))\n        break\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\/bme280_basic.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Continue reading to learn how the code works, or skip to the <a href=\"#demonstration\" title=\"\">Demonstration section<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Import Required Libraries<\/h4>\n\n\n\n<p>First, we import the required libraries: <span class=\"rnthl rntliteral\">time<\/span>, <span class=\"rnthl rntliteral\">smbus2<\/span>, and <span class=\"rnthl rntliteral\">bme280<\/span>. The <span class=\"rnthl rntliteral\">time<\/span> module lets us add delays in our code, <span class=\"rnthl rntliteral\">smbus2<\/span> helps us communicate with I2C devices like the BME280 sensor, and <span class=\"rnthl rntliteral\">bme280<\/span> provides functions to easily interact with the sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import time\nimport smbus2\nimport bme280<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Initialize the BME280 Sensor<\/h4>\n\n\n\n<p>We set the default address of the BME280 sensor to 0x<strong>76<\/strong>. This is the address that the sensor communicates with over the I2C bus (<a href=\"#get-i2c-address\" title=\"\">check this previous section<\/a>). <\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># BME280 sensor address (default address)\naddress = 0x76<\/code><\/pre>\n\n\n\n<p>We then initialize the I2C bus using the <span class=\"rnthl rntliteral\">smbus2.SMBus(1)<\/span> command.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Initialize I2C bus\nbus = smbus2.SMBus(1)<\/code><\/pre>\n\n\n\n<p>Then, we initialize the sensor by setting the I2C bus and its I2C address.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Load calibration parameters\ncalibration_params = bme280.load_calibration_params(bus, address)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Convert Celsius to Fahrenheit<\/h4>\n\n\n\n<p>We define a function called <span class=\"rnthl rntliteral\">celsius_to_fahrenheit(celsius)<\/span> which converts temperature from Celsius to Fahrenheit. Then, we can call this function later on in our code to convert the temperature.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def celsius_to_fahrenheit(celsius):\n    return (celsius * 9\/5) + 32<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Getting Sensor Readings<\/h4>\n\n\n\n<p>We enter an infinite loop using <span class=\"rnthl rntliteral\">while True<\/span> to repeatedly read and display sensor data.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>while True:<\/code><\/pre>\n\n\n\n<p>We use the <span class=\"rnthl rntliteral\">bme280.sample(bus, address, calibration_params)<\/span> function to read sensor data.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>data = bme280.sample(bus, address, calibration_params)<\/code><\/pre>\n\n\n\n<p>We extract the temperature, pressure, and humidity from the <span class=\"rnthl rntliteral\">data<\/span> returned by the sensor and save each reading on a variable: <span class=\"rnthl rntliteral\">temperature_celsius<\/span>, <span class=\"rnthl rntliteral\">pressure<\/span>, and <span class=\"rnthl rntliteral\">humidity<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Extract temperature, pressure, and humidity\ntemperature_celsius = data.temperature\npressure = data.pressure\nhumidity = data.humidity<\/code><\/pre>\n\n\n\n<p>We convert the temperature from Celsius to Fahrenheit using the function we created previously and save it in the <span class=\"rnthl rntliteral\">temperature_fahrenheit<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Convert temperature to Fahrenheit\ntemperature_fahrenheit = celsius_to_fahrenheit(temperature_celsius)<\/code><\/pre>\n\n\n\n<p>Finally, we print the readings formatted with two decimal places:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Print the readings\nprint(\"Temperature: {:.2f} \u00b0C, {:.2f} \u00b0F\".format(temperature_celsius, temperature_fahrenheit))\nprint(\"Pressure: {:.2f} hPa\".format(pressure))\nprint(\"Humidity: {:.2f} %\".format(humidity))<\/code><\/pre>\n\n\n\n<p>New readings are printed every two seconds. You can adjust the time between each sample by adding a different number to the <span class=\"rnthl rntliteral\">sleep()<\/span> method.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>time.sleep(2)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Handle Interruptions and Exceptions<\/h4>\n\n\n\n<p>We use exception handling to catch keyboard interrupts (when the user presses <strong>Ctr<\/strong>l+<strong>C<\/strong>) and other exceptions. If this happens, we print an error message and use the <span class=\"rnthl rntliteral\">break<\/span> command to get out of the loop.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>except KeyboardInterrupt:\n    print('Program stopped')\n    break\nexcept Exception as e:\n    print('An unexpected error occurred:', str(e))\n    break<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"demonstration\">Demonstration<\/h2>\n\n\n\n<p>Save your Python file. Then run it on your Raspberry Pi. Run the following command on the directory of your Python file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>python bme280_basic.py<\/em><\/pre>\n\n\n\n<p>You should get new temperature, humidity, and pressure readings on the Python Shell or on the Raspberry Pi Terminal every three 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=\"313\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/bme280-readings-python-terminal-shell.png?resize=750%2C313&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi BME280 Readings Python Terminal\" class=\"wp-image-134480\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/bme280-readings-python-terminal-shell.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/bme280-readings-python-terminal-shell.png?resize=300%2C125&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>You can stop the execution of the program by pressing <strong>CTRL<\/strong>+<strong>C<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to interface the BME280 temperature, humidity, and pressure sensor with the Raspberry Pi and how to write a Python program to get and display readings. This is one of the simplest examples to get you started using the BME280 sensor.<\/p>\n\n\n\n<p>We hope you found this tutorial useful. If you&#8217;re a beginner to the Raspberry Pi, you can get started with the following tutorials:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-raspberry-pi\/\">Getting Started with Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pinout-gpios\/\" title=\"\">Raspberry Pi Pinout Guide<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-remote-ssh-vs-code\/\">Programming Raspberry Pi Remotely using VS Code (Remote-SSH)<\/a><\/li>\n<\/ul>\n\n\n\n<p>You can check all our Raspberry Pi projects on the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi\/\" title=\"\">Free Raspberry Pi Projects and Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>Finally, if you would like to interface the BME280 with other microcontrollers, we have tutorials for ESP32, ESP8266, and Arduino:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/\" title=\"ESP32: Getting Started with the BME280 Sensor\"><strong>ESP32<\/strong>: Getting Started with the BME280 Sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><strong>ESP8266<\/strong>: Getting Started with the BME280 Sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/bme280-sensor-arduino-pressure-temperature-humidity\/\" title=\"\"><strong>Arduino<\/strong>: Getting Started with the BME280 Sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-bme280-esp32-esp8266\/\" title=\"\"><strong>ESP32\/ESP8266 (MicroPython)<\/strong>: Getting Started with the BME280 Sensor<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to interface the BME280 sensor with the Raspberry Pi to get temperature, humidity, and pressure readings. You&#8217;ll learn how to connect the sensor to the Raspberry Pi board &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi: BME280 Temperature, Humidity and Pressure Sensor (Python)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-bme280-python\/#more-134464\" aria-label=\"Read more about Raspberry Pi: BME280 Temperature, Humidity and Pressure Sensor (Python)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":134488,"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":[301,264,190,268],"tags":[],"class_list":["post-134464","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-raspberrypi","category-project","category-raspberry-pi","category-raspberry-pi-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/08\/Raspberry-Pi-BME280-Temperature-Humidity-Tutorial.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\/134464","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=134464"}],"version-history":[{"count":15,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/134464\/revisions"}],"predecessor-version":[{"id":136991,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/134464\/revisions\/136991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/134488"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=134464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=134464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=134464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}