# BME680 Examples

**URL:** <https://forums.pimoroni.com/t/bme680-examples/22050>\
**Category:** Support\
**Created:** [April 26, 2023, 9:39am UTC](https://forums.pimoroni.com/t/bme680-examples/22050 "2023-04-26T09:39:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbar](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@gbar](https://forums.pimoroni.com/u/gbar)\
**Post date:** [April 26, 2023, 9:39am UTC](https://forums.pimoroni.com/t/bme680-examples/22050/1 "2023-04-26T09:39:52Z")

</div>

Hi,  
struggling to run thew examples.  
Followed the instructions here -

> **[Getting Started with BME680 Breakout](https://learn.pimoroni.com/article/getting-started-with-bme680-breakout)**
>
> Learn how to solder and use your BME680 breakout – Pimoroni Learning Portal

but i can’t figure out the correct way to insert the I2C address in the code. It doesn’t like my attempts. Always fails on line 13

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [April 26, 2023, 10:57am UTC](https://forums.pimoroni.com/t/bme680-examples/22050/2 "2023-04-26T10:57:29Z")

</div>

Raspberry Pi? Python?  
Try these examples.  
[bme680-python/examples at master · pimoroni/bme680-python (github.com)](https://github.com/pimoroni/bme680-python/tree/master/examples)

---

<div class="post-metadata">

**Author:** ![gbar](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@gbar](https://forums.pimoroni.com/u/gbar)\
**Post date:** [April 26, 2023, 8:34pm UTC](https://forums.pimoroni.com/t/bme680-examples/22050/4 "2023-04-26T20:34:28Z")

</div>

```auto
#!/usr/bin/env python

import bme680
import time

print("""read-all.py - Displays temperature, pressure, humidity, and gas.

Press Ctrl+C to exit!

""")

try:
    sensor = bme680.BME680(bme680.I2C 0x76)
except (RuntimeError, IOError):
    sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)

# These calibration data can safely be commented
# out, if desired.

print('Calibration data:')
for name in dir(sensor.calibration_data):

    if not name.startswith('_'):
        value = getattr(sensor.calibration_data, name)

        if isinstance(value, int):
            print('{}: {}'.format(name, value))

# These oversampling settings can be tweaked to
# change the balance between accuracy and noise in
# the data.

sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)

print('\n\nInitial reading:')
for name in dir(sensor.data):
    value = getattr(sensor.data, name)

    if not name.startswith('_'):
        print('{}: {}'.format(name, value))

sensor.set_gas_heater_temperature(320)
sensor.set_gas_heater_duration(150)
sensor.select_gas_heater_profile(0)

# Up to 10 heater profiles can be configured, each
# with their own temperature and duration.
# sensor.set_gas_heater_profile(200, 150, nb_profile=1)
# sensor.select_gas_heater_profile(1)

print('\n\nPolling:')
try:
    while True:
        if sensor.get_sensor_data():
            output = '{0:.2f} C,{1:.2f} hPa,{2:.2f} %RH'.format(
                sensor.data.temperature,
                sensor.data.pressure,
                sensor.data.humidity)

            if sensor.data.heat_stable:
                print('{0},{1} Ohms'.format(
                    output,
                    sensor.data.gas_resistance))

            else:
                print(output)

        time.sleep(1)

except KeyboardInterrupt:
    pass

```

---

<div class="post-metadata">

**Author:** ![gbar](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@gbar](https://forums.pimoroni.com/u/gbar)\
**Post date:** [April 26, 2023, 8:39pm UTC](https://forums.pimoroni.com/t/bme680-examples/22050/7 "2023-04-26T20:39:47Z")

</div>

~/bme680/examples $ python read-all.py  
File “/home/bme680/examples/read-all.py”, line 13  
sensor = bme680.BME680(bme680.I2C 0x76)  
^  
SyntaxError: invalid syntax

---

<div class="post-metadata">

**Author:** ![gbar](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@gbar](https://forums.pimoroni.com/u/gbar)\
**Post date:** [April 26, 2023, 8:40pm UTC](https://forums.pimoroni.com/t/bme680-examples/22050/8 "2023-04-26T20:40:35Z")

</div>

the ‘^’ is under my entering of the I2C address

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/halfer/32/9738_2.png) [@halfer](https://forums.pimoroni.com/u/halfer)\
**Post date:** [April 26, 2023, 9:32pm UTC](https://forums.pimoroni.com/t/bme680-examples/22050/12 "2023-04-26T21:32:30Z")

</div>

> [@gbar](#):
>
> ```
> sensor = bme680.BME680(bme680.I2C 0x76)
> 
> ```

Hmm, I am no Pythonista, but this looks problematic. The `BME680()` method is given two parameters, but it is not clear if they should be one parameter, or separated by a comma (which is how parameters are separated). What documentation are you looking at? Could you link to it?

## Update

In [this file](https://github.com/pimoroni/bme680-python/blob/master/examples/indoor-air-quality.py) it looks like it should take one parameter, thus:

```
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)

```

---

<div class="post-metadata">

**Author:** ![gbar](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@gbar](https://forums.pimoroni.com/u/gbar)\
**Post date:** [April 27, 2023, 7:07am UTC](https://forums.pimoroni.com/t/bme680-examples/22050/13 "2023-04-27T07:07:21Z")

</div>

Thanks for the feedback.  
Original as you write didn’t work either.

I thought i was to substitute the \_ADDR\_PRIMARY with my I2C address, the breakout board is default 76 or 0x76 according to the guide

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [April 27, 2023, 8:36am UTC](https://forums.pimoroni.com/t/bme680-examples/22050/14 "2023-04-27T08:36:32Z")

</div>

> [@gbar](#):
>
> `sensor = bme680.BME680(bme680.I2C 0x76)`

If its using the default address (AFAIK) you don’t need the 0x76.  
Try `sensor = bme680.BME680(bme680.I2C)`

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/halfer/32/9738_2.png) [@halfer](https://forums.pimoroni.com/u/halfer)\
**Post date:** [April 27, 2023, 10:29am UTC](https://forums.pimoroni.com/t/bme680-examples/22050/15 "2023-04-27T10:29:26Z")

</div>

Assuming the all-caps variables are just integer constants, I should think that hardwiring would work too:

```
sensor = bme680.BME680(0x76)

```

If it doubt about a class variable, one can try printing it to the console:

```
print(bme680.I2C_ADDR_PRIMARY) # Substitute the one you want to try here

```

This will print it in decimal though, not in hex - so have your programmers’ calculator to hand!

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [April 27, 2023, 10:45am UTC](https://forums.pimoroni.com/t/bme680-examples/22050/16 "2023-04-27T10:45:31Z")

</div>

> [@halfer](#):
>
> `sensor = bme680.BME680(0x76)`

Should work, you’d do that if you were using two bme sensors.  
or ‘sensor = bme680.BME680(i2c,0x76)’ if the above doesn’t work.  
It’s easy in Micropython, haven’t done it in Python in while.

---

<div class="post-metadata">

**Author:** ![gbar](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@gbar](https://forums.pimoroni.com/u/gbar)\
**Post date:** [April 27, 2023, 10:55am UTC](https://forums.pimoroni.com/t/bme680-examples/22050/17 "2023-04-27T10:55:39Z")

</div>

```auto
>>> %Run read-all.py
read-all.py - Displays temperature, pressure, humidity, and gas.

Press Ctrl+C to exit!

Traceback (most recent call last):
  File "/home/bme680/examples/read-all.py", line 14, in <module>
    sensor = bme680.BME680(bme680.I2C)
AttributeError: module 'bme680' has no attribute 'I2C'
>>> 

```

Alas, no joy. I feel i must be doing something really obvious and stupid. Pimoroni must have sold hundreds of these things

---

<div class="post-metadata">

**Author:** ![gbar](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@gbar](https://forums.pimoroni.com/u/gbar)\
**Post date:** [April 27, 2023, 10:57am UTC](https://forums.pimoroni.com/t/bme680-examples/22050/18 "2023-04-27T10:57:15Z")

</div>

i see I2c and also I2C mentioned above, i’ve tried both

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [April 27, 2023, 11:01am UTC](https://forums.pimoroni.com/t/bme680-examples/22050/19 "2023-04-27T11:01:14Z")

</div>

Try this, if you haven’t already  
`sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)`

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/halfer/32/9738_2.png) [@halfer](https://forums.pimoroni.com/u/halfer)\
**Post date:** [April 27, 2023, 11:24am UTC](https://forums.pimoroni.com/t/bme680-examples/22050/20 "2023-04-27T11:24:36Z")

</div>

There’s a bit too much guessing going on in this thread - `I2C` isn’t in the library code, so I imagine it won’t do anything.

These are the constants that should work, assuming you are using the Pimoroni library: [bme680-python/constants.py at master · pimoroni/bme680-python · GitHub](https://github.com/pimoroni/bme680-python/blob/master/library/bme680/constants.py)

Here is the class definition: [bme680-python/\_\_init\_\_.py at master · pimoroni/bme680-python · GitHub](https://github.com/pimoroni/bme680-python/blob/master/library/bme680/ __init__.py)

I don’t really “do” Python - @alphanumeric could you explain how this line works?

```
def __init__ (self, i2c_addr=constants.I2C_ADDR_PRIMARY, i2c_device=None):

```

If this is an object constructor, what should be passed in `self`? It looks like the address needs to be the second instantiation param, not the first.

However I think @gbar is following the example correctly - it looks like the address should be passed as the first param. See this example: [bme680-python/read-all.py at master · pimoroni/bme680-python · GitHub](https://github.com/pimoroni/bme680-python/blob/master/examples/read-all.py)

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [April 27, 2023, 11:38am UTC](https://forums.pimoroni.com/t/bme680-examples/22050/21 "2023-04-27T11:38:15Z")

</div>

If it’s wired up correctly, and the install script has been run?  
The examples should **just work**?  
I don’t have a BME680 handy that I can plug into my Pi based Breakout Garden. I think they are all wired up to Pi PICO’s? I will have a good look for one though.

---

<div class="post-metadata">

**Author:** ![ahnlak](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/ahnlak/32/5610_2.png) [@ahnlak](https://forums.pimoroni.com/u/ahnlak)\
**Post date:** [April 27, 2023, 12:04pm UTC](https://forums.pimoroni.com/t/bme680-examples/22050/22 "2023-04-27T12:04:53Z")

</div>

That `self` is just the weird way (Micro)Python passes in self-references; you can ignore that - it’s showing two arguments (`i2c_addr` and `i2c_device`) along with their defaults.

As such, `sensor = bms.BME680()` should work, and do the same as `BME680(bme680.I2C_ADDR_PRIMARY)`.

If you’re using a different I2C address, you should be able to add it as @alphanumeric says.

---

<div class="post-metadata">

**Author:** ![hel](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/hel/32/6075_2.png) [@hel](https://forums.pimoroni.com/u/hel)\
**Post date:** [April 27, 2023, 2:40pm UTC](https://forums.pimoroni.com/t/bme680-examples/22050/23 "2023-04-27T14:40:46Z")

</div>

Spotted a few bits of out of date information in the BME680 Learn article, so I’ve done a few ninja updates 🐱‍👤

> **[Getting Started with BME680 Breakout](https://learn.pimoroni.com/article/getting-started-with-bme680-breakout)**
>
> Learn how to solder and use your BME680 breakout – Pimoroni Learning Portal

---

<div class="post-metadata">

**Author:** ![gbar](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@gbar](https://forums.pimoroni.com/u/gbar)\
**Post date:** [April 27, 2023, 8:36pm UTC](https://forums.pimoroni.com/t/bme680-examples/22050/24 "2023-04-27T20:36:35Z")

</div>

Hi,

Thanks to everyone who has chimed in but…i see a DS18B20 in my future.  
Before starting down this path i had a fresh install. Pi4 in use.  
Followed the revised ‘Getting Started’

```auto
~/Pimoroni/bme680/examples $ python read-all.py
read-all.py - Displays temperature, pressure, humidity, and gas.

Press Ctrl+C to exit!

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/ __init__.py", line 46, in __init__
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/ __init__.py", line 352, in _get_regs
OSError: [Errno 121] Remote I/O error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/graeme/Pimoroni/bme680/examples/read-all.py", line 13, in <module>
    sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/ __init__.py", line 50, in __init__
RuntimeError: Unable to identify BME680 at 0x76 (IOError)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/ __init__.py", line 46, in __init__
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/ __init__.py", line 352, in _get_regs
OSError: [Errno 121] Remote I/O error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/graeme/Pimoroni/bme680/examples/read-all.py", line 15, in <module>
    sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
  File "/usr/local/lib/python3.9/dist-packages/bme680-1.1.1-py3.9.egg/bme680/ __init__.py", line 50, in __init__
RuntimeError: Unable to identify BME680 at 0x77 (IOError)

```

That last line "…at 0x77 (IOError)  
according to docs should be 0x76?

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/halfer/32/9738_2.png) [@halfer](https://forums.pimoroni.com/u/halfer)\
**Post date:** [April 27, 2023, 8:46pm UTC](https://forums.pimoroni.com/t/bme680-examples/22050/25 "2023-04-27T20:46:38Z")

</div>

Have you used the `i2cdetect` tools to see what your Pi can detect on your system?

I used `i2cdetect -l` when playing around with a USB-to-I2C board recently. It should give you what I2C system it is running on (i2cX, where X is an integer).

---

<div class="post-metadata">

**Author:** ![gbar](https://avatars.discourse-cdn.com/v4/letter/g/e8c25b/32.png) [@gbar](https://forums.pimoroni.com/u/gbar)\
**Post date:** [April 27, 2023, 9:02pm UTC](https://forums.pimoroni.com/t/bme680-examples/22050/26 "2023-04-27T21:02:17Z")

</div>

embarassing…  
hardware issue.  
sorry for wasting everyones time.  
working now but i think maybe only since the guide was updated

[Next page](https://forums.pimoroni.com/t/bme680-examples/22050.md?page=2)
