phantomecho
Posts: 4
Joined: Thu May 26, 2016 4:47 pm

BME 280 SPI Interface in Python

Thu May 26, 2016 5:02 pm

Hello Everyone,

This is my first post on the forum, hopefully it's not a redundant question. I am NOT new to computers or to the raspberry pi, but this is the first time I've attempted to use my RPI3 for serious interface work. I have an RPI 3 interfaced with the 7" official touch screen, and an HDMI monitor attached for doing the main work. The GPIO is broken out to a large breadboard via a spark-fun pi wedge.

I am attempting to interface several sensors with the board via the SPI interface (I cannot use I2C because all of the sensors that I need to use occupy the same hard coded addresses in I2C). One of the sensor types is a spark-fun BME 280 (Combination Temp, Baro, RH), and although I can find a python library for the GPIO and for the Sensor, the library is written assuming that you are using I2C (Again, only 2 addresses available on I2C, and I have several sensors 2 connect). I am not experienced enough as a programmer to write the necessary interface for SPI.

My question is; is there anyone who has written a library for this family of devices for python that can interface with the SPI on the breakout? Any information would be greatly appreciated...

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: BME 280 SPI Interface in Python

Thu May 26, 2016 5:46 pm

As a first stab it would be worth Googling for "BME280 SPI Python". Any examples you find should work on the Pi.

phantomecho
Posts: 4
Joined: Thu May 26, 2016 4:47 pm

Re: BME 280 SPI Interface in Python

Thu May 26, 2016 5:52 pm

joan wrote:As a first stab it would be worth Googling for "BME280 SPI Python". Any examples you find should work on the Pi.
I should have been more verbose; I *HAVE* googled the BME 280 and SPI for Python and examined the libraries and example code that I've found online... I've also poured over a number of forums to (so far) no avail. So yes I've STFW and no luck :(

There is no information that is succinct enough to all me to develop an SPI interface because of my experience level in coding. There are any number of examples of doing this in I2C, but I've not found ANY that work with SPI on this sensor.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: BME 280 SPI Interface in Python

Thu May 26, 2016 7:57 pm

well, since the BMP280 is much more common, you can try starting from spi code for this one (if you can find it) and add the RH part.

Edit: or you can add a multiplexer and use i2c :)

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: BME 280 SPI Interface in Python

Thu May 26, 2016 9:03 pm

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

phantomecho
Posts: 4
Joined: Thu May 26, 2016 4:47 pm

Re: BME 280 SPI Interface in Python

Thu May 26, 2016 9:28 pm

Hello again!

I appreciate the suggestions, but the problem is;

both have suggested that I look at examples of code for interfacing with I2C. Neither the code or libraries from either the BME280 or the (as was suggested) older BMP 280 include information about interfacing with SPI. The Bosch Data sheet shows the necessary registers to communicate with the device, and I can pull the CS0 pin low to start talking to the device but I am not sufficiently skilled to write out a 'bit bang' driver to poll the devices registers directly myself. I was able to get the devices operating with an Arduino Mega2560 on SPI via the affiliated spark-fun library for the Arduino, but I need to directly control the devices from a RPI 3, and eliminate the Mega from the hardware chain.

Please forgive any confusion that I've caused... I do a lot of Wire - Level electronics (Soldering, Analog Development, etc...) but I am not a very experienced programmer, although I am familiar enough with Python, C++, and Basic to write simple programs.

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: BME 280 SPI Interface in Python

Thu May 26, 2016 9:49 pm

https://github.com/sparkfun/SparkFun_BM ... BME280.cpp

Has SPI code which looks straightforward enough.

phantomecho
Posts: 4
Joined: Thu May 26, 2016 4:47 pm

Re: BME 280 SPI Interface in Python

Thu May 26, 2016 10:01 pm

joan wrote:https://github.com/sparkfun/SparkFun_BM ... BME280.cpp

Has SPI code which looks straightforward enough.
Hi Joan,

Again thank you (Genuinely!) for replying to me. I have looked through the .cpp and .h files that you linked me to, and although I can clearly see the SPI segment of the code for the BME280, I also fully admit that it's beyond me to alter this to work in python for my project... I currently have a fairly long (~1,200 lines) program in Python that works exactly right sans the actual sensor data and I'm loathe to consider re-writing it in C++ unless I have NO other choice.

please correct me if I am wrong; I can't simply import the <libraryNameHere>.h files into a python program, and I don't yet have mastery of using any kind of wrapper? if anyone has any suggestions on how else I might skin the feline, please chime in :D

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: BME 280 SPI Interface in Python

Thu May 26, 2016 10:10 pm

Sorry, I didn't spot "SPI".

You should be able to use python spidev readbytes(...) and writebytes(...) functions to communicate with your BMP280 over the SPI interface (no bit banging needed) by cribbing the C++ code Joan has found. Sorry I can't help further, but doing strange things with strange sensors isn't easy when you don't have one to play with.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

paulw25
Posts: 5
Joined: Mon Dec 26, 2016 10:32 am

Re: BME 280 SPI Interface in Python

Mon Dec 26, 2016 10:51 am

I wonder if anyone has had any success interfacing the Pi to the BME280 over SPI? I can successfully read the chip ID, and the calibration data, but the temp, pressure and humidity always read as [0, 128, 0, 0, 128, 0, 0, 128]. I suspect I am not correctly writing to the control registers to initiate the measurements. Here is my sample code:

Code: Select all

import spidev
import time

spi = spidev.SpiDev()
spi.open(0,0)

print('Read the contents of the ID register')
resp = spi.xfer2([0xD0, 0x00])
print resp
print

# Send a command to the control register[0xF2]
spi.xfer2([0xF2, 0x01])

# Send a command to the control register[0xF4]
spi.xfer2([0xF4, 0x27])

# Send a command to the control register[0xF5]
spi.xfer2([0xF5, 0xA0])

print('Read temp, pressure and humidity')
a = []
for x in range(0xF7, 0xFF):
	a.append(x)

resp = spi.xfer2(a)
print resp
print
	
print('Read calibration data')
a = []
for x in range(0x88, 0xA0):
	a.append(x)

resp = spi.xfer2(a)
print resp


paulw25
Posts: 5
Joined: Mon Dec 26, 2016 10:32 am

Re: BME 280 SPI Interface in Python

Fri Dec 30, 2016 11:30 pm

I did manage to get the PigPio code working, but unfortunately I could not slow the clock down far enough for my needs. I need a very slow clock, because I am running the sensor over a long cable (~ 8m). I think I need to use software bit-banging.

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: BME 280 SPI Interface in Python

Sat Dec 31, 2016 9:12 am

The pigpio BME280 class makes four calls to SPI routines being SPI open, SPI close, and calls to SPI xfer to read and write the registers.

If you change those four occurrences (search for pi.spi_) to use bit banging the rest of the code can remain untouched.

See http://abyz.co.uk/rpi/pigpio/python.html#bb_spi_open, http://abyz.co.uk/rpi/pigpio/python.html#bb_spi_close, and http://abyz.co.uk/rpi/pigpio/python.html#bb_spi_xfer

paulw25
Posts: 5
Joined: Mon Dec 26, 2016 10:32 am

Re: BME 280 SPI Interface in Python

Sat Dec 31, 2016 10:00 am

OK I finally read the data sheet and found the problem!

In SPI mode, only 7 bits of the register addresses are used; the MSB of register address is not
used and replaced by a read/write bit (RW = ‘0’ for write and RW = ‘1’ for read).
Example: address 0xF7 is accessed by using SPI register address 0x77. For write access, the
byte 0x77 is transferred, for read access, the byte 0xF7 is transferred.


I was wondering how the chip detected the difference between a read and write! Many thanks.

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: BME 280 SPI Interface in Python

Sat Dec 31, 2016 10:24 am

paulw25 wrote:OK I finally read the data sheet and found the problem!

In SPI mode, only 7 bits of the register addresses are used; the MSB of register address is not
used and replaced by a read/write bit (RW = ‘0’ for write and RW = ‘1’ for read).
Example: address 0xF7 is accessed by using SPI register address 0x77. For write access, the
byte 0x77 is transferred, for read access, the byte 0xF7 is transferred.


I was wondering how the chip detected the difference between a read and write! Many thanks.
Any software you have come across would have taken that into account.

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