[SOLVED] BME280 and O2 Sensors

I have a Raspberry Pi 3B and GrovePi+ along with both the Seeed BME280 sensor and the Seeed O2 Gas Sensor. I am using Windows IOT and C#. I see that there is currently no libraries for either of these two sensors. Can you give me a timeframe that there might be libraries for these? Also, the BME280 sensor is I2C and it seems that the I2C interface with the GrovePi is a pass through. I did find the BME280 from Adafruit on a breakout board with code for Win IOT and C#. Since it is I2C, how close is this code. I have tried using it and it seems to somewhat work but it still seems to have a problem seeing the I2C interface. Thanks,

The sensors you mentioned are not on the list of supported sensors.
https://www.dexterindustries.com/GrovePi/supported-sensors/

Dexter Ind as far as I know never said they would try to support all the grove sensors. They don’t make them, or at least all of them, so I don’t think they are responsible for making them work. So are they working on support for it? They could be, but maybe not. I don’t work for them. That make sense? Of course everything I said is what “I” think the companies stance/status on this is. I don’t work for them.

Hi @RickS,

Thank you for taking time bringing this up to us.


I took a look at these 2 sensors from Seeed and judging by what I’m seeing, I think you can use the Seeed O2 Gas Sensor almost straight out-of-the-box. It’s basically an analog sensor.


Regarding the other sensor, the BME280 is quite very similar to what we already have in our repo - the bmp180.
Here’s a link to it.

Looking up on Adafruit, I see a library exactly for your BME280 sensor - this should suit your needs.
Here’s a link to the repo.


Also, here’s a example code I’ve written up in Python for the O2 Gas Sensor (link). The code runs nicely (as in I don’t get errors), but I haven’t tested with an actual sensor since I don’t have one.
I had it translated from the Arduino code I found out on the Seeed page.

If you don’t have Raspbian For Robots on your Raspberry, then you’ll have to add the grovepi.py file to your current directory manually.

import grovepi
import sys
from time import sleep

# function for translating analog value to voltage
def readO2Voltage(pin, vref):
    sum = 0
    max_times = 32

    for tries in range(max_times):
        sum += grovepi.analogRead(pin)

    sum /= max_times
    measured_voltage = sum * vref / 1023.0

    return measured_voltage

# function for translating voltage to O2 concentration in percentages
def readO2Concentration(pin, vref):
    measured_voltage = readO2Voltage(pin, vref)
    O2_concentrantion = measured_voltage * 0.21 / 2.0
    O2_concentrantion_percentage = O2_concentrantion * 100

    return O2_concentrantion_percentage

def Main():
    # YOU NEED TO PREHEAT THE O2 sensor FOR 20-30 MINS
    # OTHERWISE YOU'LL GET HIGHER READS

    analog_pin = 0 # refers to A0 port
    vref = 5.0 # ADC voltage reference

    while True:
        vout = readO2Voltage(analog_pin, vref)
        percentage_concentration = readO2Concentration(analog_pin, vref)
        print('[vout = {:5.2f} V][O2 concentration = {:5.2f}]'.format(vout, percentage_concentration))

        # wait 250 ms before the next read
        sleep(0.25)

if __name__ == "__main__":
    try:
        Main()

    # in case CTRL-C / CTRL-D keys are pressed (or anything else that might interrupt)
    except KeyboardInterrupt:
        print('[Keyboard interrupted]')
        sys.exit(0)

    # in case there's an IO error aka I2C
    except IOError:
        print('[IO Error]')
        sys.exit(0)

    # in case we have a math error (like division by 0 - can happen depending on the read values)
    # or if the values exceed a certain threshold
    # experiment and you'll see
    except ValueError as e:
        print('[{}]'.format(str(e)))
        sys.exit(0)

Again, this ain’t an official example code.
It’s just something I’ve written up for you and it isn’t tested.


Thank you!

Hello Robert,

Thanks for taking the time to look at this. I am fairly new to the Raspberry Pi and the GrovePi and wanted to make sure I wasn’t going down a dead end road. This will help a lot.

Thanks Again,

Rick

Update:

I worked with the code for the O2 sensor. I noticed that you converted the code from the seeed C code on their site.
I got this to work but was getting odd readings. Then I noticed that they are reading the voltage once to display voltage and a reading the voltage again to calculate the O2%. They need to read the voltage once and do the calculation on the same reading. It still will get an out of range reading occasionally so I need to test for that and throw those out. Now all I need to do is convert this over to Win IOT and C#.

Here is the new code:

import grovepi
import sys
from time import sleep

function for translating analog value to voltage

def readVoltage(pin, vref):

voltage = ((grovepi.analogRead(pin) * vref) / 1023)
return voltage

def Main():

    pin = 1    # A0 port on GrovePi
    vref = 5   # Voltage Reference

    while True:
        
      voltage = readVoltage(pin, vref)
      O2percent = (((voltage * .21) / 2)*100)
    
      print('[Voltage = {:5.2f} V][O2 Percent = {:5.2f}]'.format(voltage, O2percent))

      # wait 1 second before the next read
      sleep(1.0)

Thanks Again Robert;

1 Like

This topic was automatically closed after 9 hours. New replies are no longer allowed.