adellis
Posts: 4
Joined: Sun Jun 16, 2019 9:55 pm

WiringPi and I2C BME280

Sun Jun 16, 2019 10:02 pm

Hi,

I've got a BME280 wired up to the I2C and am using WiringPi to successfully communicate and read the temp/humidity/pressure.

What doesn't work is when the sensor isn't connected.

The program still opens the file descriptor as if everything is fine, when I want to fall back and use a DHT22.

The ic2detect -y 1 shows

Code: Select all

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
when the device is not connected and shows 76 when it is. 76 is the device id.

I'm using wiringPi 2.46 which is a couple of years old but the I2C parts don't seem to have changed since then.

Any ideas.

Thanks in advance

Ade

RattusRattus
Posts: 44
Joined: Sat Sep 29, 2018 11:27 am

Re: WiringPi and I2C BME280

Mon Jun 17, 2019 7:44 pm

So I think you are saying
A) when you have the BME280 connected everything works as you would expect
B) When the BME280 is connected it shows up on as device 76 when you scan the I2C bus with ic2detect -y 1
C) When you disconnect the BME280 it correctly disapears from ic2detect -y 1


So far everything appears correct and is doing what it should
What doesn't work is when the sensor isn't connected.
The program still opens the file descriptor as if everything is fine,
OK so

Does your program work correctly if the BME280 is NOT conneced when you start your program?

If this is the case then you need to look at how you ar initialising your file handler

If it doesn't fail (because the BME280 isn't found) then it would appear to be blind luck that you are ever able to read from it :-)

/Rattus

adellis
Posts: 4
Joined: Sun Jun 16, 2019 9:55 pm

Re: WiringPi and I2C BME280

Mon Jun 17, 2019 10:05 pm

Your initial analysis is correct.

My program works fine with no BME280 connected, it just reads zero values. I was hoping something would report an error so I could fall back to using the DHT22 which is connected.

I've tried a raw open("/dev/i2c-1") and then an ioctl on the I2C_SLAVE with the BME280 device id and that doesn't fail either. This is how wiringPi and the BME280 code from the Bosch repo work. I'm expecting the file descriptor will always open as the I2C device is enabled, so it's the ioctl that needs to fail.

In fact the only think I've found that fails when the BME280 is not connected is a python script, so I might have to see what that does and try and reproduce it in C/C++.

Thanks

Ade

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6882
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: WiringPi and I2C BME280

Tue Jun 18, 2019 7:40 am

ioctl(I2C_SLAVE) only updates the address to be used in subsequent transactions, it does not perform any communication with the device at that point, and therefore doesn't know whether the device is present or not.
When you try to do a read or write to the device, that will fail as the device will not be ACKing the transfer.

If you look at i2cdetect, it opens the device, and the loops over calling ioctl(I2C_SLAVE), and then doing a read or write on the device. It is the result of the read/write that determines the output.
The only time the ioctl will fail is if a kernel driver has already claimed the device on that address.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

adellis
Posts: 4
Joined: Sun Jun 16, 2019 9:55 pm

Re: WiringPi and I2C BME280

Tue Jun 18, 2019 9:41 am

That info about ioctl is very useful to know, thanks a lot.

I had a quick look at the python and think that was checking the device id which was the cause of it reporting the device wasn't present.
I'll have to incorporate that method into my code.

Thanks again.

adellis
Posts: 4
Joined: Sun Jun 16, 2019 9:55 pm

Re: WiringPi and I2C BME280

Tue Jun 18, 2019 7:14 pm

For completeness, the Python was reading two bytes for the Chip Id and Version from 0xD0.

I added a call to wiringPiI2CReadReg16 from the same address and then just checked if it matched the Chip Id reported by the Python (96 in this case).

I call this once at the start and then use the check to decide whether to read from the BME280 or the DHT22.

Thanks for all you help.

Return to “Interfacing (DSI, CSI, I2C, etc.)”