# Pi Pico W and BME280

**URL:** https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883
**Category:** Hardware
**Created:** [31 March 2024 14:04 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883 "2024-03-31T14:04:01Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![badflash](https://avatars.discourse-cdn.com/v4/letter/b/d6d6ee/32.png) [@badflash](https://discourse.nodered.org/u/badflash)
#### Post date: [31 March 2024 14:04 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883/1 "2024-03-31T14:04:01Z")

</div>

I could not get my Pico W to play nicely with my BME280. It just could not read it with the standard micropython drivers. I had some other drivers from Core Electronic and they worked really well. Quite robust and just kicks up an error message instread of crashing your program if a wire comes loose or it gets a few back packages from the sensor. BME280.py , PiicoDev\_BME280.py and PiicoDev\_Unified.py work together.

This is the example code:

```auto
# PiicoDev Atmospheric Sensor BME280 minimal example code
# This program reads Temperature, Pressure and Relative Humididty
# from the PiicoDev Atmospheric Sensor. An altitude reading is also
# available

from PiicoDev_BME280 import PiicoDev_BME280
from PiicoDev_Unified import sleep_ms # cross-platform compatible sleep function

sensor = PiicoDev_BME280() # initialise the sensor
zeroAlt = sensor.altitude() # take an initial altitude reading

while True:
    # Print data
    tempC, presPa, humRH = sensor.values() # read all data from the sensor
    pres_hPa = presPa / 100 # convert air pressurr Pascals -> hPa (or mbar, if you prefer)
    pres_psi=pres_hPa/68.9476 +.22 # add cal factor
    tempF=tempC*9/5+32
    print(str(tempF)+" °F " + str(pres_psi)+" psi " + str(humRH)+" %RH")
    
    # Altitude demo
# print(sensor.altitude() - zeroAlt) # Print the pressure CHANGE since the script began
    sleep_ms(5000)

```

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [31 March 2024 18:08 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883/2 "2024-03-31T18:08:05Z")

</div>

I'm really surprised the bme280 library didn't work for you.  
Here's a simple test script that should work on a Pico-W  
You might want to change the SCL and SDA pin-mapping to suit your situation.

```auto
from machine import Pin, I2C
import utime
import bme280

# Define I2C pins
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=100000)

# Initialize BME280 sensor
bme = bme280.BME280(i2c=i2c)

while True:
    # Read sensor data
    temperature, pressure, humidity = bme.read_compensated_data()

    # Print sensor data
    print("Temperature: {:.2f}°C".format(temperature))
    print("Pressure: {:.2f} hPa".format(pressure / 25600))
    print("Humidity: {:.2f}%".format(humidity / 1024))

    # Wait for 2 seconds before next reading
    utime.sleep(2)

```

---

<div class="post-metadata">

### Author: ![badflash](https://avatars.discourse-cdn.com/v4/letter/b/d6d6ee/32.png) [@badflash](https://discourse.nodered.org/u/badflash)
#### Post date: [31 March 2024 23:18 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883/3 "2024-03-31T23:18:26Z")

</div>

No Go. Same issue I was having before:

```auto
Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
  File "/lib/bme280.py", line 75, in __init__
OSError: [Errno 5] EIO

```

This is line 9:

```auto
bme = bme280.BME280(i2c=i2c)

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e30d8d3f6ec764ccf3452c9ae92b84ee19c7077f.png)

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [1 April 2024 06:05 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883/4 "2024-04-01T06:05:50Z")

</div>

**EDIT** : I've just looked at the library you showed in your screenshot and believe it is the same or very similar to the one I'm using.

I used a bme280 library I found on the InterWeb. There is a copy on my website at this URL...  
[https://teamwork-int.com/share\_my\_projects](https://teamwork-int.com/share_my_projects)  
Go to the folder labelled 'C\_single\_sensor\_node' and then the folder 'lib' where you should find 'bme280.py'

The error you're encountering, `OSError: [Errno 5] EIO`, usually indicates a problem with the I2C communication between your Raspberry Pi Pico and the BME280 sensor.

Ensure your BME280 sensor is correctly wired to the Raspberry Pi Pico. The typical wiring configuration is:

- **VCC** of BME280 to **3.3V** on Pico
- **GND** to **GND**
- **SDA** to **GPIO 8** (or the SDA pin you're using)
- **SCL** to **GPIO 9** (or the SCL pin you're using)

Verify the correct address for your module. You can scan for devices on the I2C bus to find out the address of your BME280 sensor:

```auto
from machine import I2C, Pin

i2c = I2C(0, scl=Pin(9), sda=Pin(8))
devices = i2c.scan()
print("I2C devices found:", devices)

```

**EDIT** : I have some code that handles a loose wire or even a missing BME module. I can share those scripts with you once you have the sensor working reliably.

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [1 April 2024 09:19 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883/5 "2024-04-01T09:19:21Z")

</div>

Just lashed-up a Pico with a BME280 on pins 8 and 9 and can confirm it works fine with the script I posted above and the modified one below. So I suggest you double-check your wiring.

 ![pico_bme_A](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6a7418e8f6edd780b36188b91035dfc5a74f0017.jpeg)

 ![pico_bme_B](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bbf1f6840b493a33863d31be3e28c5f0e6abd58f.jpeg)

![bme_readings](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48326a105cca33b123eddb12c38eb86be9b2e1b3.png)

Here's another Micro-Python script (that gets the values directly from the sensor) you could try.

```auto
from machine import Pin, I2C
import utime
import bme280

# Define I2C pins
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=100000)
# Pin(8) is labelled GP8 on the Pico board and Pin(9) is GP9

# Initialize BME280 sensor
bme = bme280.BME280(i2c=i2c)

while True:
    # Read sensor data
    temp = float(bme.values[0])
    print("\nTemp ", temp)
    
    humidity = float(bme.values[2])
    print("Humidity ", humidity)
    
    pressure = float(bme.values[1])
    print("Pressure ", pressure)

    # Wait for 2 seconds before next reading
    utime.sleep(2)

```

---

<div class="post-metadata">

### Author: ![badflash](https://avatars.discourse-cdn.com/v4/letter/b/d6d6ee/32.png) [@badflash](https://discourse.nodered.org/u/badflash)
#### Post date: [1 April 2024 12:21 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883/6 "2024-04-01T12:21:01Z")

</div>

Believe me, I have quadruple checked my wiring. I have a solution that works reliably. I don't know why yours works and mine doesn't. Sometimes the magic works, sometimes the magic doesn't work.  
I'll stick with the Core Electronics drivers. If it ain't broke, don't fix it.

Thanks though @dynamicdave

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [1 April 2024 12:26 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883/7 "2024-04-01T12:26:07Z")

</div>

Just as a long-shot... have you checked that the wires are good with a continuity tester?.  
The other day I had a breadboard wire that looked fine, but was intermittent and was giving similar error reports as what you are seeing.

---

<div class="post-metadata">

### Author: ![badflash](https://avatars.discourse-cdn.com/v4/letter/b/d6d6ee/32.png) [@badflash](https://discourse.nodered.org/u/badflash)
#### Post date: [1 April 2024 12:33 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883/8 "2024-04-01T12:33:50Z")

</div>

Yes I have. It doesn't explain why the Core Electronics drivers work flawlessly and kick up errors when I wiggle a wire or disconnect it. I have soldered screw terminals on the Pico and secure connectors on the BME280. The Mouser version is pretty secure:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/e/3ed2e5b15bbb6c43aebdc5e2402a4cac0eaccc8e.jpeg)

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [1 April 2024 12:38 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883/9 "2024-04-01T12:38:39Z")

</div>

OK - I'm out of ideas, so I give-up and throw in the towel.

---

<div class="post-metadata">

### Author: ![badflash](https://avatars.discourse-cdn.com/v4/letter/b/d6d6ee/32.png) [@badflash](https://discourse.nodered.org/u/badflash)
#### Post date: [1 April 2024 13:57 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883/10 "2024-04-01T13:57:47Z")

</div>

I appreciate the help.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [31 May 2024 13:58 UTC](https://discourse.nodered.org/t/pi-pico-w-and-bme280/86883/11 "2024-05-31T13:58:38Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
