{"id":136805,"date":"2024-02-08T15:18:53","date_gmt":"2024-02-08T15:18:53","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=136805"},"modified":"2024-05-06T10:51:42","modified_gmt":"2024-05-06T10:51:42","slug":"raspberry-pi-pico-bme280-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-bme280-micropython\/","title":{"rendered":"Raspberry Pi Pico: BME280 Get Temperature, Humidity, and Pressure (MicroPython)"},"content":{"rendered":"\n<p>Learn how to use the BME280 sensor module with the Raspberry Pi Pico board with MicroPython to get temperature, humidity, and pressure readings. This is a short tutorial to get you started quickly interfacing the BME280 sensor with the Raspberry Pi Pico board.<\/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\/09\/Raspberry-Pi-Pico-BME280-MicroPython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with BME280 Get Temperature Humidity and Pressure MicroPython\" class=\"wp-image-136808\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-BME280-MicroPython.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-BME280-MicroPython.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-BME280-MicroPython.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-BME280-MicroPython.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 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=\"#prerequisites\" title=\"\">MicroPython firmware<\/a><\/li>\n\n\n\n<li><a href=\"#intro-bme280\" title=\"\">Introducing the BME280 Sensor Module<\/a><\/li>\n\n\n\n<li><a href=\"#bme280-raspberry-pi-pico-circuit\" title=\"\">Wiring the BME280 to the Raspberry Pi Pico<\/a><\/li>\n\n\n\n<li><a href=\"#bme280-micropython-library\" title=\"\">BME280 MicroPython Library<\/a><\/li>\n\n\n\n<li><a href=\"#bme280-micropython-library\" title=\"\">BME280 Pressure, Temperature, and Humidity &#8211; MicroPython Code<\/a><\/li>\n<\/ul>\n\n\n\n<p>Do you prefer to program the Raspberry Pi Pico using Arduino IDE? Check this tutorial instead: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-bme280-arduino\/\">Raspberry Pi Pico &#8211; BME280 Get Temperature, Humidity, and Pressure (Arduino IDE)<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites &#8211; MicroPython Firmware<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board. <\/p>\n\n\n\n<p>The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\" title=\"\">Programming Raspberry Pi Pico using MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"intro-bme280\">Introducing the BME280 Sensor Module<\/h2>\n\n\n\n<p>The <a aria-label=\"BME280 sensor module (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\" rel=\"noreferrer noopener\">BME280 sensor module<\/a> reads barometric pressure, temperature, and humidity. Because pressure changes with altitude, you can also estimate altitude. There are several versions of this sensor module, but we\u2019re using the one shown in the figure below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><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 temperature, humidity, and pressure\" class=\"wp-image-86437\" style=\"width:750px;height:408px\" 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<p>This sensor communicates using I2C communication protocol, so the wiring is very simple. You can use any Raspberry Pi Pico I2C pins to connect the BME280 sensor. We&#8217;ll be using <span class=\"rnthl rntcyellow\">GPIO 4<\/span> (SDA) and <span class=\"rnthl rntclgray\">GPIO 5<\/span> (SCL)\u2014<a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/\" title=\"\">learn more about the Raspberry Pi Pico GPIOs<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BME280<\/strong><\/td><td><strong>Raspberry Pi Pico<\/strong><\/td><\/tr><tr><td>Vin<\/td><td><span class=\"rnthl rntcred\">3.3V<\/span> (OUT)<\/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<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\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-BME280-circuit.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Wiring the BME280 to the Raspberry Pi Pico parts required\" class=\"wp-image-136809\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-BME280-circuit.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-BME280-circuit.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 BME280 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 href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"BME280 sensor module\u2028 (opens in a new tab)\">BME280 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 board<\/a><\/li>\n\n\n\n<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>\n\n\n\n<li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" 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=\"bme280-raspberry-pi-pico-circuit\">Wiring the BME280 to the Raspberry Pi Pico<\/h2>\n\n\n\n<p>Wire the BME280 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=\"759\" height=\"684\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-BME280-circuit-diagram_bb.png?resize=759%2C684&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Wiring the BME280 to the Raspberry Pi Pico\" class=\"wp-image-136832\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-BME280-circuit-diagram_bb.png?w=759&amp;quality=100&amp;strip=all&amp;ssl=1 759w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-BME280-circuit-diagram_bb.png?resize=300%2C270&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\"><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=\"bme280-micropython-library\">BME280 MicroPython Library<\/h2>\n\n\n\n<p>The library to read from the BME280 sensor isn\u2019t part of the standard MicroPython library by default. There are different modules\/libraries you can use to read from the BME280 sensor. We&#8217;ll use the following (adapted from the Adafruit BME280 Library). <\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\">from machine import I2C\nimport time\n\n# BME280 default address.\nBME280_I2CADDR = 0x76\n\n# Operating Modes\nBME280_OSAMPLE_1 = 1\nBME280_OSAMPLE_2 = 2\nBME280_OSAMPLE_4 = 3\nBME280_OSAMPLE_8 = 4\nBME280_OSAMPLE_16 = 5\n\n# BME280 Registers\n\nBME280_REGISTER_DIG_T1 = 0x88  # Trimming parameter registers\nBME280_REGISTER_DIG_T2 = 0x8A\nBME280_REGISTER_DIG_T3 = 0x8C\n\nBME280_REGISTER_DIG_P1 = 0x8E\nBME280_REGISTER_DIG_P2 = 0x90\nBME280_REGISTER_DIG_P3 = 0x92\nBME280_REGISTER_DIG_P4 = 0x94\nBME280_REGISTER_DIG_P5 = 0x96\nBME280_REGISTER_DIG_P6 = 0x98\nBME280_REGISTER_DIG_P7 = 0x9A\nBME280_REGISTER_DIG_P8 = 0x9C\nBME280_REGISTER_DIG_P9 = 0x9E\n\nBME280_REGISTER_DIG_H1 = 0xA1\nBME280_REGISTER_DIG_H2 = 0xE1\nBME280_REGISTER_DIG_H3 = 0xE3\nBME280_REGISTER_DIG_H4 = 0xE4\nBME280_REGISTER_DIG_H5 = 0xE5\nBME280_REGISTER_DIG_H6 = 0xE6\nBME280_REGISTER_DIG_H7 = 0xE7\n\nBME280_REGISTER_CHIPID = 0xD0\nBME280_REGISTER_VERSION = 0xD1\nBME280_REGISTER_SOFTRESET = 0xE0\n\nBME280_REGISTER_CONTROL_HUM = 0xF2\nBME280_REGISTER_CONTROL = 0xF4\nBME280_REGISTER_CONFIG = 0xF5\nBME280_REGISTER_PRESSURE_DATA = 0xF7\nBME280_REGISTER_TEMP_DATA = 0xFA\nBME280_REGISTER_HUMIDITY_DATA = 0xFD\n\n\nclass Device:\n  &quot;&quot;&quot;Class for communicating with an I2C device.\n\n  Allows reading and writing 8-bit, 16-bit, and byte array values to\n  registers on the device.&quot;&quot;&quot;\n\n  def __init__(self, address, i2c):\n    &quot;&quot;&quot;Create an instance of the I2C device at the specified address using\n    the specified I2C interface object.&quot;&quot;&quot;\n    self._address = address\n    self._i2c = i2c\n\n  def writeRaw8(self, value):\n    &quot;&quot;&quot;Write an 8-bit value on the bus (without register).&quot;&quot;&quot;\n    value = value &amp; 0xFF\n    self._i2c.writeto(self._address, value)\n\n  def write8(self, register, value):\n    &quot;&quot;&quot;Write an 8-bit value to the specified register.&quot;&quot;&quot;\n    b=bytearray(1)\n    b[0]=value &amp; 0xFF\n    self._i2c.writeto_mem(self._address, register, b)\n\n  def write16(self, register, value):\n    &quot;&quot;&quot;Write a 16-bit value to the specified register.&quot;&quot;&quot;\n    value = value &amp; 0xFFFF\n    b=bytearray(2)\n    b[0]= value &amp; 0xFF\n    b[1]= (value&gt;&gt;8) &amp; 0xFF\n    self.i2c.writeto_mem(self._address, register, value)\n\n  def readRaw8(self):\n    &quot;&quot;&quot;Read an 8-bit value on the bus (without register).&quot;&quot;&quot;\n    return int.from_bytes(self._i2c.readfrom(self._address, 1),'little') &amp; 0xFF\n\n  def readU8(self, register):\n    &quot;&quot;&quot;Read an unsigned byte from the specified register.&quot;&quot;&quot;\n    return int.from_bytes(\n        self._i2c.readfrom_mem(self._address, register, 1),'little') &amp; 0xFF\n\n  def readS8(self, register):\n    &quot;&quot;&quot;Read a signed byte from the specified register.&quot;&quot;&quot;\n    result = self.readU8(register)\n    if result &gt; 127:\n      result -= 256\n    return result\n\n  def readU16(self, register, little_endian=True):\n    &quot;&quot;&quot;Read an unsigned 16-bit value from the specified register, with the\n    specified endianness (default little endian, or least significant byte\n    first).&quot;&quot;&quot;\n    result = int.from_bytes(\n        self._i2c.readfrom_mem(self._address, register, 2),'little') &amp; 0xFFFF\n    if not little_endian:\n      result = ((result &lt;&lt; 8) &amp; 0xFF00) + (result &gt;&gt; 8)\n    return result\n\n  def readS16(self, register, little_endian=True):\n    &quot;&quot;&quot;Read a signed 16-bit value from the specified register, with the\n    specified endianness (default little endian, or least significant byte\n    first).&quot;&quot;&quot;\n    result = self.readU16(register, little_endian)\n    if result &gt; 32767:\n      result -= 65536\n    return result\n\n  def readU16LE(self, register):\n    &quot;&quot;&quot;Read an unsigned 16-bit value from the specified register, in little\n    endian byte order.&quot;&quot;&quot;\n    return self.readU16(register, little_endian=True)\n\n  def readU16BE(self, register):\n    &quot;&quot;&quot;Read an unsigned 16-bit value from the specified register, in big\n    endian byte order.&quot;&quot;&quot;\n    return self.readU16(register, little_endian=False)\n\n  def readS16LE(self, register):\n    &quot;&quot;&quot;Read a signed 16-bit value from the specified register, in little\n    endian byte order.&quot;&quot;&quot;\n    return self.readS16(register, little_endian=True)\n\n  def readS16BE(self, register):\n    &quot;&quot;&quot;Read a signed 16-bit value from the specified register, in big\n    endian byte order.&quot;&quot;&quot;\n    return self.readS16(register, little_endian=False)\n\n\nclass BME280:\n  def __init__(self, mode=BME280_OSAMPLE_1, address=BME280_I2CADDR, i2c=None,\n               **kwargs):\n    # Check that mode is valid.\n    if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,\n                    BME280_OSAMPLE_8, BME280_OSAMPLE_16]:\n        raise ValueError(\n            'Unexpected mode value {0}. Set mode to one of '\n            'BME280_ULTRALOWPOWER, BME280_STANDARD, BME280_HIGHRES, or '\n            'BME280_ULTRAHIGHRES'.format(mode))\n    self._mode = mode\n    # Create I2C device.\n    if i2c is None:\n      raise ValueError('An I2C object is required.')\n    self._device = Device(address, i2c)\n    # Load calibration values.\n    self._load_calibration()\n    self._device.write8(BME280_REGISTER_CONTROL, 0x3F)\n    self.t_fine = 0\n\n  def _load_calibration(self):\n\n    self.dig_T1 = self._device.readU16LE(BME280_REGISTER_DIG_T1)\n    self.dig_T2 = self._device.readS16LE(BME280_REGISTER_DIG_T2)\n    self.dig_T3 = self._device.readS16LE(BME280_REGISTER_DIG_T3)\n\n    self.dig_P1 = self._device.readU16LE(BME280_REGISTER_DIG_P1)\n    self.dig_P2 = self._device.readS16LE(BME280_REGISTER_DIG_P2)\n    self.dig_P3 = self._device.readS16LE(BME280_REGISTER_DIG_P3)\n    self.dig_P4 = self._device.readS16LE(BME280_REGISTER_DIG_P4)\n    self.dig_P5 = self._device.readS16LE(BME280_REGISTER_DIG_P5)\n    self.dig_P6 = self._device.readS16LE(BME280_REGISTER_DIG_P6)\n    self.dig_P7 = self._device.readS16LE(BME280_REGISTER_DIG_P7)\n    self.dig_P8 = self._device.readS16LE(BME280_REGISTER_DIG_P8)\n    self.dig_P9 = self._device.readS16LE(BME280_REGISTER_DIG_P9)\n\n    self.dig_H1 = self._device.readU8(BME280_REGISTER_DIG_H1)\n    self.dig_H2 = self._device.readS16LE(BME280_REGISTER_DIG_H2)\n    self.dig_H3 = self._device.readU8(BME280_REGISTER_DIG_H3)\n    self.dig_H6 = self._device.readS8(BME280_REGISTER_DIG_H7)\n\n    h4 = self._device.readS8(BME280_REGISTER_DIG_H4)\n    h4 = (h4 &lt;&lt; 24) &gt;&gt; 20\n    self.dig_H4 = h4 | (self._device.readU8(BME280_REGISTER_DIG_H5) &amp; 0x0F)\n\n    h5 = self._device.readS8(BME280_REGISTER_DIG_H6)\n    h5 = (h5 &lt;&lt; 24) &gt;&gt; 20\n    self.dig_H5 = h5 | (\n        self._device.readU8(BME280_REGISTER_DIG_H5) &gt;&gt; 4 &amp; 0x0F)\n\n  def read_raw_temp(self):\n    &quot;&quot;&quot;Reads the raw (uncompensated) temperature from the sensor.&quot;&quot;&quot;\n    meas = self._mode\n    self._device.write8(BME280_REGISTER_CONTROL_HUM, meas)\n    meas = self._mode &lt;&lt; 5 | self._mode &lt;&lt; 2 | 1\n    self._device.write8(BME280_REGISTER_CONTROL, meas)\n    sleep_time = 1250 + 2300 * (1 &lt;&lt; self._mode)\n\n    sleep_time = sleep_time + 2300 * (1 &lt;&lt; self._mode) + 575\n    sleep_time = sleep_time + 2300 * (1 &lt;&lt; self._mode) + 575\n    time.sleep_us(sleep_time)  # Wait the required time\n    msb = self._device.readU8(BME280_REGISTER_TEMP_DATA)\n    lsb = self._device.readU8(BME280_REGISTER_TEMP_DATA + 1)\n    xlsb = self._device.readU8(BME280_REGISTER_TEMP_DATA + 2)\n    raw = ((msb &lt;&lt; 16) | (lsb &lt;&lt; 8) | xlsb) &gt;&gt; 4\n    return raw\n\n  def read_raw_pressure(self):\n    &quot;&quot;&quot;Reads the raw (uncompensated) pressure level from the sensor.&quot;&quot;&quot;\n    &quot;&quot;&quot;Assumes that the temperature has already been read &quot;&quot;&quot;\n    &quot;&quot;&quot;i.e. that enough delay has been provided&quot;&quot;&quot;\n    msb = self._device.readU8(BME280_REGISTER_PRESSURE_DATA)\n    lsb = self._device.readU8(BME280_REGISTER_PRESSURE_DATA + 1)\n    xlsb = self._device.readU8(BME280_REGISTER_PRESSURE_DATA + 2)\n    raw = ((msb &lt;&lt; 16) | (lsb &lt;&lt; 8) | xlsb) &gt;&gt; 4\n    return raw\n\n  def read_raw_humidity(self):\n    &quot;&quot;&quot;Assumes that the temperature has already been read &quot;&quot;&quot;\n    &quot;&quot;&quot;i.e. that enough delay has been provided&quot;&quot;&quot;\n    msb = self._device.readU8(BME280_REGISTER_HUMIDITY_DATA)\n    lsb = self._device.readU8(BME280_REGISTER_HUMIDITY_DATA + 1)\n    raw = (msb &lt;&lt; 8) | lsb\n    return raw\n\n  def read_temperature(self):\n    &quot;&quot;&quot;Get the compensated temperature in 0.01 of a degree celsius.&quot;&quot;&quot;\n    adc = self.read_raw_temp()\n    var1 = ((adc &gt;&gt; 3) - (self.dig_T1 &lt;&lt; 1)) * (self.dig_T2 &gt;&gt; 11)\n    var2 = ((\n        (((adc &gt;&gt; 4) - self.dig_T1) * ((adc &gt;&gt; 4) - self.dig_T1)) &gt;&gt; 12) *\n        self.dig_T3) &gt;&gt; 14\n    self.t_fine = var1 + var2\n    return (self.t_fine * 5 + 128) &gt;&gt; 8\n\n  def read_pressure(self):\n    &quot;&quot;&quot;Gets the compensated pressure in Pascals.&quot;&quot;&quot;\n    adc = self.read_raw_pressure()\n    var1 = self.t_fine - 128000\n    var2 = var1 * var1 * self.dig_P6\n    var2 = var2 + ((var1 * self.dig_P5) &lt;&lt; 17)\n    var2 = var2 + (self.dig_P4 &lt;&lt; 35)\n    var1 = (((var1 * var1 * self.dig_P3) &gt;&gt; 8) +\n            ((var1 * self.dig_P2) &gt;&gt; 12))\n    var1 = (((1 &lt;&lt; 47) + var1) * self.dig_P1) &gt;&gt; 33\n    if var1 == 0:\n      return 0\n    p = 1048576 - adc\n    p = (((p &lt;&lt; 31) - var2) * 3125) \/\/ var1\n    var1 = (self.dig_P9 * (p &gt;&gt; 13) * (p &gt;&gt; 13)) &gt;&gt; 25\n    var2 = (self.dig_P8 * p) &gt;&gt; 19\n    return ((p + var1 + var2) &gt;&gt; 8) + (self.dig_P7 &lt;&lt; 4)\n\n  def read_humidity(self):\n    adc = self.read_raw_humidity()\n    # print 'Raw humidity = {0:d}'.format (adc)\n    h = self.t_fine - 76800\n    h = (((((adc &lt;&lt; 14) - (self.dig_H4 &lt;&lt; 20) - (self.dig_H5 * h)) +\n         16384) &gt;&gt; 15) * (((((((h * self.dig_H6) &gt;&gt; 10) * (((h *\n                          self.dig_H3) &gt;&gt; 11) + 32768)) &gt;&gt; 10) + 2097152) *\n                          self.dig_H2 + 8192) &gt;&gt; 14))\n    h = h - (((((h &gt;&gt; 15) * (h &gt;&gt; 15)) &gt;&gt; 7) * self.dig_H1) &gt;&gt; 4)\n    h = 0 if h &lt; 0 else h\n    h = 419430400 if h &gt; 419430400 else h\n    return h &gt;&gt; 12\n\n  @property\n  def temperature(self):\n    &quot;Return the temperature in degrees.&quot;\n    t = self.read_temperature()\n    ti = t \/\/ 100\n    td = t - ti * 100\n    return &quot;{}.{:02d}C&quot;.format(ti, td)\n\n  @property\n  def pressure(self):\n    &quot;Return the temperature in hPa.&quot;\n    p = self.read_pressure() \/\/ 256\n    pi = p \/\/ 100\n    pd = p - pi * 100\n    return &quot;{}.{:02d}hPa&quot;.format(pi, pd)\n\n  @property\n  def humidity(self):\n    &quot;Return the humidity in percent.&quot;\n    h = self.read_humidity()\n    hi = h \/\/ 1024\n    hd = h * 100 \/\/ 1024 - hi * 100\n    return &quot;{}.{:02d}%&quot;.format(hi, hd)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/WiFi\/HTTP_Client_IFTTT_BME280\/BME280.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Upload the previous library to your Raspberry Pi Pico board (save it with the name <em>BME280.py<\/em>). Follow the instructions below to learn how to upload the library using Thonny IDE.<\/p>\n\n\n\n<p>If you\u2019re using Thonny IDE, follow the next steps:<\/p>\n\n\n\n<p><strong>1.<\/strong> Copy the library code to a new file. The <a aria-label=\" (opens in a new tab)\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/blob\/master\/code\/WiFi\/HTTP_Client_IFTTT_BME280\/BME280.py\" target=\"_blank\" rel=\"noreferrer noopener\">BME280 library code can be found here<\/a>.<\/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=\"685\" height=\"740\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/BME280-library-micropython.png?resize=685%2C740&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 library MicroPython Thonny IDE\" class=\"wp-image-136837\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/BME280-library-micropython.png?w=685&amp;quality=100&amp;strip=all&amp;ssl=1 685w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/BME280-library-micropython.png?resize=278%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 278w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/figure><\/div>\n\n\n<p><strong>2.<\/strong> Go to <strong>File<\/strong> &gt; <strong>Save as&#8230;<\/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=\"206\" height=\"294\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/Thonny-IDE-ESP32-ESP8266-MicroPython-Save-file-library-to-device-save-as.png?resize=206%2C294&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE ESP32 ESP8266 MicroPython Save file library to device save as\" class=\"wp-image-99603\"\/><\/figure><\/div>\n\n\n<p><strong>3.<\/strong> Select save to &#8220;<strong>Raspberry Pi Pico<\/strong>&#8220;:<\/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=\"214\" height=\"203\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Save-file-to-Raspberry-Pi-Pico.png?resize=214%2C203&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Save Files to Raspberry Pi Pico Thonny IDE\" class=\"wp-image-130899\"\/><\/figure><\/div>\n\n\n<p><strong>4.<\/strong> Name your file as <em>BME280.py<\/em> and press the OK button:<\/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=\"545\" height=\"319\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-save-bme280-library.png?resize=545%2C319&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 library save to Raspberry Pi Pico Thonne IDE\" class=\"wp-image-136833\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-save-bme280-library.png?w=545&amp;quality=100&amp;strip=all&amp;ssl=1 545w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-save-bme280-library.png?resize=300%2C176&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<p>And that&#8217;s it. The library was uploaded to your board. To make sure that it was uploaded successfully, go to <strong>File<\/strong> &gt; <strong>Save as&#8230;<\/strong> and select the MicroPython device. Your file should be listed there:<\/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=\"545\" height=\"327\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-bme280-library-saved.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 Raspberry Pi pico library saved Thonny IDE\" class=\"wp-image-136834\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-bme280-library-saved.png?w=545&amp;quality=100&amp;strip=all&amp;ssl=1 545w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-bme280-library-saved.png?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<p>After uploading the library to your board, you can use the library functionalities in your code by importing the library.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bme280-raspberry-pi-pico-code\">BME280 Pressure, Temperature, and Humidity &#8211; MicroPython Code<\/h2>\n\n\n\n<p>After uploading the library to the Raspberry Pi Pico, create a new file called <em>main.py<\/em> and paste the following code. It simply prints the temperature, humidity, and pressure into the console every 5 seconds.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-bme280-micropython\/\n\nfrom machine import Pin, I2C\nfrom time import sleep\nimport BME280\n\n# Initialize I2C communication\ni2c = I2C(id=0, scl=Pin(5), sda=Pin(4), freq=10000)\n\nwhile True:\n    try:\n        # Initialize BME280 sensor\n        bme = BME280.BME280(i2c=i2c)\n        \n        # Read sensor data\n        tempC = bme.temperature\n        hum = bme.humidity\n        pres = bme.pressure\n        \n        # Convert temperature to fahrenheit\n        tempF = (bme.read_temperature()\/100) * (9\/5) + 32\n        tempF = str(round(tempF, 2)) + 'F'\n        \n        # Print sensor readings\n        print('Temperature: ', tempC)\n        print('Temperature: ', tempF)\n        print('Humidity: ', hum)\n        print('Pressure: ', pres)\n        \n    except Exception as e:\n        # Handle any exceptions during sensor reading\n        print('An error occurred:', e)\n\n    sleep(5)\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\/MicroPython\/main_BME280_Basic.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>First, you need to import the necessary libraries, including the <span class=\"rnthl rntliteral\">BME280<\/span> module you&#8217;ve imported previously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import Pin, I2C\nfrom time import sleep\nimport BME280<\/code><\/pre>\n\n\n\n<p>Set the I2C id, pins, and frequency. We&#8217;re using GPIOs 5 and 4, but any other <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/#i2c\" title=\"\">I2C pins<\/a> should work.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>i2c = I2C(id=0, scl=Pin(5), sda=Pin(4), freq=10000)<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> GPIOs 5 and 4 belong to I2C id=0\u2014<a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/#i2c\" title=\"\">check here other combinations and their ids<\/a>.<\/p>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">while<\/span> loop, create a <span class=\"rnthl rntliteral\">BME280<\/span> object called <span class=\"rnthl rntliteral\">bme<\/span> with the I2C pins defined earlier:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>bme = BME280.BME280(i2c=i2c)<\/code><\/pre>\n\n\n\n<p>By default, the library assumes your BME280 sensor has the default <strong>0x76<\/strong> I2C address (if you open the library code, you\u2019ll see the address defined right on line 5 <span class=\"rnthl rntliteral\">BME280_I2CADDR = 0x76<\/span>).<\/p>\n\n\n\n<p>If your sensor is not working, it may be the case that it has a different I2C address. To check the I2C address, you can take a look at the following tutorial.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-i2c-scanner-micropython\/\">Raspberry Pi Pico: I2C Scanner (MicroPython) \u2013 Finding the Address of I2C Devices<\/a><\/li>\n<\/ul>\n\n\n\n<p>If after running the I2C scanner we provided previously, your sensor has a different I2C address, change this line:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>bme = BME280.BME280(i2c=i2c)<\/code><\/pre>\n\n\n\n<p>To:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>bme = BME280.BME280(i2c=i2c, addr=<strong>YOUR_I2C_ADDRESS<\/strong>)<\/code><\/pre>\n\n\n\n<p>&nbsp;For example, if your I2C address is <strong>0x78<\/strong>, it will look as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>bme = BME280.BME280(i2c=i2c, addr=0x78)<\/code><\/pre>\n\n\n\n<p>Reading temperature, humidity, and pressure is as simple as using the <span class=\"rnthl rntliteral\">temperature<\/span>, <span class=\"rnthl rntliteral\">humidity,<\/span> and <span class=\"rnthl rntliteral\">pressure<\/span> methods on the <span class=\"rnthl rntliteral\">bme<\/span> object. <\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>temp = bme.temperature\nhum = bme.humidity\npres = bme.pressure<\/code><\/pre>\n\n\n\n<p>If you want to display the temperature in Fahrenheit degrees, you just need to uncomment (remove the <span class=\"rnthl rntliteral\">#<\/span>) the next two lines:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># temp = (bme.read_temperature()\/100) * (9\/5) + 32\n# temp = str(round(temp, 2)) + 'F'<\/code><\/pre>\n\n\n\n<p>Finally, print the readings on the shell:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print('Temperature: ', temp)\nprint('Humidity: ', hum)\nprint('Pressure: ', pres)<\/code><\/pre>\n\n\n\n<p>In the end, we add a delay of 5 seconds:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>sleep(5)<\/code><\/pre>\n\n\n\n<p>We use <span class=\"rnthl rntliteral\">try<\/span> and <span class=\"rnthl rntliteral\">except<\/span> to catch any unexpected errors that might occur while trying to read the sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>except Exception as e:\n    print('An error occurred:', e)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"demonstration\">Demonstration<\/h2>\n\n\n\n<p>Save the code to your Raspberry Pi Pico board using <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\">Thonny IDE<\/a> or any other MicroPython IDE of your choice.<\/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\/09\/raspberry-pi-pico-bme280.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-136847\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-bme280.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-bme280.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>Copy the MicroPython code provided to a new file on Thonny IDE.<\/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=\"685\" height=\"740\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/thonny-ide-micropython-bme280-raspberry-pi-pico.png?resize=685%2C740&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Thonny IDE Read Temperature, Humidity, and Pressure from BME280\" class=\"wp-image-136838\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/thonny-ide-micropython-bme280-raspberry-pi-pico.png?w=685&amp;quality=100&amp;strip=all&amp;ssl=1 685w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/thonny-ide-micropython-bme280-raspberry-pi-pico.png?resize=278%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 278w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/figure><\/div>\n\n\n<p>With the code copied to the file, click on the&nbsp;<strong>Save&nbsp;<\/strong>icon. Then, select&nbsp;<strong>Raspberry Pi Pico<\/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=\"214\" height=\"203\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Save-file-to-Raspberry-Pi-Pico.png?resize=214%2C203&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Saving file to Raspberry Pi Pico MicroPython IDE\" class=\"wp-image-130899\"\/><\/figure><\/div>\n\n\n<p>Save the file with the following name:&nbsp;<strong>main.py<\/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=\"545\" height=\"327\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-save-main-file-f.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Saving file main.py to Raspberry Pi Pico MicroPython IDE\" class=\"wp-image-136836\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-save-main-file-f.png?w=545&amp;quality=100&amp;strip=all&amp;ssl=1 545w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-save-main-file-f.png?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<p><strong>Note<\/strong>: When you name a file <span class=\"rnthl rntliteral\">main.py<\/span>, the Raspberry Pi Pico will run that file automatically on boot. If you call it a different name, it will still be saved on the board filesystem, but it will not run automatically on boot.<\/p>\n\n\n\n<p>Click the little green button &#8220;Run Current Script&#8221; or press F5.<\/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=\"450\" height=\"78\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/thonny-ide-run-code.png?resize=450%2C78&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"run script on thonny ide\" class=\"wp-image-132505\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/thonny-ide-run-code.png?w=450&amp;quality=100&amp;strip=all&amp;ssl=1 450w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/thonny-ide-run-code.png?resize=300%2C52&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/figure><\/div>\n\n\n<p>New temperature and humidity readings will be published on the shell approximately every five 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=\"685\" height=\"732\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-bme280-thonny-ide-get-sensor-readings.png?resize=685%2C732&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico print temperature, humidity and pressure from BME280\" class=\"wp-image-136840\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-bme280-thonny-ide-get-sensor-readings.png?w=685&amp;quality=100&amp;strip=all&amp;ssl=1 685w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/raspberry-pi-pico-bme280-thonny-ide-get-sensor-readings.png?resize=281%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 281w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Troubleshooting<\/h3>\n\n\n\n<p><strong>Your BME280 is not working?<\/strong> It might be: incorrect wiring, <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-i2c-scanner-micropython\/\" title=\"\">wrong I2C address<\/a>, broken sensor or you got a fake BME280 sensor. Take a look at this&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/solved-could-not-find-a-valid-bme280-sensor\/\">BME280 troubleshooting guide<\/a>&nbsp;to find the cause and solution for your problem.<\/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 Pico and how to write a MicroPython program to get and display readings on the MicroPython console. This is one of the simplest examples to get you started using the BME280 sensor.<\/p>\n\n\n\n<p>We hope you&#8217;ve 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-micropython\/\">Raspberry Pi Pico: <strong>DS18B20 Temperature Sensor<\/strong> (MicroPython) \u2013 Single and Multiple<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dht11-dht22-micropython\/\">Raspberry Pi Pico: <strong>DHT11\/DHT22 Temperature and Humidity Sensor<\/strong> (MicroPython)<\/a><\/li>\n<\/ul>\n\n\n\n<p>You can check all our Raspberry Pi Pico projects on the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\" title=\"\">Learn Raspberry Pi Pico with MicroPython (eBook)<\/a><\/strong><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\" title=\"\"><strong>Free Raspberry Pi Pico Projects and Tutorials<\/strong><\/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, Arduino, and Raspberry Pi:<\/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\/raspberry-pi-bme280-python\/\"><strong>Raspberry Pi<\/strong>: BME280 Temperature, Humidity and Pressure Sensor (Python)<\/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 use the BME280 sensor module with the Raspberry Pi Pico board with MicroPython to get temperature, humidity, and pressure readings. This is a short tutorial to get &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: BME280 Get Temperature, Humidity, and Pressure (MicroPython)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-bme280-micropython\/#more-136805\" aria-label=\"Read more about Raspberry Pi Pico: BME280 Get Temperature, Humidity, and Pressure (MicroPython)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":136808,"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":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324,326],"tags":[],"class_list":["post-136805","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-micropython"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/09\/Raspberry-Pi-Pico-BME280-MicroPython.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\/136805","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=136805"}],"version-history":[{"count":14,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/136805\/revisions"}],"predecessor-version":[{"id":154863,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/136805\/revisions\/154863"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/136808"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=136805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=136805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=136805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}