Main Site Documentation

TinyCLR library for the BME280 Humidity, Barometric Pressure + Temp sensor


#1

NuGet:

Source:

Usage:

using System.Threading;
using System.Diagnostics;

using GHIElectronics.TinyCLR.Pins;
using GHIElectronics.TinyCLR.Devices.I2c;

using Monoculture.TinyCLR.Drivers.BME280;;

namespace TinyCLRApplication1
{
    class Program
    {
        public static void Main()
        {
            var settings = BME280Driver.GetI2CConnectionSettings(BME280Address.Primary);

            var controller = I2cController.FromName(G120E.I2cBus.I2c0);

            var device = controller.GetDevice(settings);

            var driver = new BME280Driver(device);

            driver.Initialize();

            driver.ChangeSettings(
                BME280SensorMode.Forced,
                BME280OverSample.X1,
                BME280OverSample.X1,
                BME280OverSample.X1,
                BME280Filter.Off);

            while (true)
            {
                driver.Read();

                Debug.WriteLine("Pressure: " + driver.Pressure);
                Debug.WriteLine("Humidity: " + driver.Humidity);
                Debug.WriteLine("Temperature:" + driver.Temperature);

                Thread.Sleep(1000);
            }
        }
    }
}

#2

Humm… this is the device that has the plethora of calibration values that you have to take into account when calculating the value of the temp? In your tests was the value accurate?

EDIT: Just read the code, yep it’s that device. Don’t mind if I help myself to your calculator. I tried writing on for dotnet core a few weeks back and found that part was rather confusing.


#3

Hi john,

I wrote the code a couple of months ago and its just been sitting in a private repository on GitHub gathering dust. At the time I had ordered a high-pressure waterproof sensor from China and I was just using the BME280 as a temporary stand-in for some code I was working on. I didn’t do much checking other than eyeballing the results and then comparing them to my thermostat.

I’ve just run it again and this is what i’m getting back:

  1. 99953 Pascals is roughly 1 standard atmosphere.
  2. 63% humidity agrees with my thermostat (62%)
  3. 24 Deg c seems a little high - My thermostat says its only 20 deg c

Did you mean the CRC calculation?

It’s an Apache 2.0 license so you can pretty much do whatever you want with it but if you find any bugs can you let me know so I can fix them?

Thanks

David


#4

I think the device would be more accurate than the thermostat in terms of the temp. Also, it was perhaps sitting on your desk at the time (as opposed to the wall). I did not mean the CRC calculation, I am referring to the entire calculation used to compute temp, press and humid.

I’ll let you know if there are any bugs. Further, how did you develop the code for the calculations? Did your port from another project or did you read the datasheet :slight_smile:


#5

I looked at a few Arduino implementations but I think the logic for the main calculation was lifted straight from the official Bosch C drivers:


#6

MonoCulture%20BEM280%20working

Well I managed to “port” it over to dotnet core. I don’t think it has any errors! Much thanks Monoculture. Now I don’t know what to do with the modified code.


#7

Fantastic!

One small thing I noticed in your screenshot the units are Pa not kPa

I don’t know how closely you followed by code but if I was reimplementing it I would probably cache the output of the three properties in a backing variable and then clear the cache when the update function gets called. That way the calculation isn’t rerun every time the property is accessed. I’d also probably rename the “Update” function to “Read” or something similiar.

So, is this is an open source thing? will we get to see it?


#8

Oh it’s Pa; no scene its just the output formatting.

The project requires System.Device.Gpio for the SPI bus

I guess now is as good a time to learn GitHub. It’s a totally different project type and all though. How should I publish it?


#9

BTW my device uses the SPI bus not I2C.


#10

Ok then. Here is open source.

EDIT:


And here is the power bi view of that data. Yea!


#11

Hi John,

Sorry I didn’t reply earlier but I’ve been really busy with stuff at work…

What you’ve done with it looks really good. I especially like those cool graphs :sunglasses:

I’ve started a new dev branch in order to add SPI support to the original TinyCLR driver.

https://github.com/monoculture/Monoculture.TinyCLR.Drivers.BME280/tree/development

Take a look at the BME280BusWrapper class - Unfortunately I don’t have the board with me at the moment so I haven’t been able to test it but I’ll do that when i get in tonight and if everything looks good I’ll merge the changes into main and do another release.

I also found a bug in the original code - Filter isn’t getting updated in the ChangeSettings method.


#12

Done…

I’ve merged the code back into main and uploaded the new version to NuGet…

For some reason I couldn’t get SPI TransferSequential to work properly (I suspect it has something to do with the byte order of the data being returned) so I used your bit of code for that. Hope that’s OK with you?


#13

We have a bug with chip select being deasserted between write and read. Will be fixed in next release.


#14

Sigh… now the device does not appear to work anymore. I think it’s been blown. It’s not reporting the correct values. Is that even possible?


#15

Is this with the new code i just uploaded or with your own driver?

Can you post the values you are getting?


#16

This is with my own driver. Suddenly yesterday, it stopped working. No code changes, just started it up and… poof.
This is my formatted output:

Pressure : 67724.2 Pa, Humidity : 46.85%, Temprature : 23.78°C

I know for a fact that temp is wrong.


#17

Hi John…

I think the sensor config is getting corrupted in SPI mode.

I had a look through the code yesterday and found the following problems:

  1. The reset register & command is wrong in my original code - It should be 0xE0 & 0xB6.
  2. The line that sets up the configuration register in WriteSettings is also wrong. The first bit is enabling 3 wire SPI. Just change the last 1 to a 0.
  3. In SPI mode the MSB of the register address is not used. You have to set it to 0 for writes and 1 for reads.
  4. The sensor can lock up so I’ve moved the Reset function to the top of the initialise method.

I’ve checked the changes in and created another release.