Jump to content

SPI on a Raspberry Pi


SteamyTea

Recommended Posts

I have bought a few of these nifty temperature, humidity and air pressure sensors:

http://www.ebay.co.uk/itm/302009695856?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

 

How do I connect they to a Raspberry Pi Model A so that I can read from all of them.

I have an real time clock (RTC) already connected to the Inter-Integrated Circuit ( I2C) pin, and as I want to use several of these to check temprature and humidy, I need to run then via the Serial Peripheral Interface (SPI).

I am struggling to find much about it from the hardware side, so uncertain as to how to wire them up.  They have 6 pins (once soldered on):

VCC = power

GND = ground

SCL = clock

SDA = data

CSB = ?

SDO = ?

 

I think that CSB may be the 'polling' pin to read from that device and SDO may be the data pin.  These seem easy to set up on I2C as it only uses SCL and SDA as far as I can work out, but, as mentioned earlier, really struggling to find anything about setting them up on SPI.

Link to comment
Share on other sites

Nick, the BMP supports both I2C and SPI 3 and 4 wire.  SPI is fast and point to point I2C is a proper bus so you can run your RTC and your BMP280s on it (two per bus).  I am not sure what the issue is.  The RPI GPIOs and the BMP280s will all coexist on an I2C bus with a 3.3V pullup.

 

Section 5 of the DS describes the electrical configs.  You can have two devices per I2C bus at addresses 0x76 or 0x77 depending on whether pin SDO (not used on I2C bus) is pulled high or low.  The other pins used are  SCK used as SCL and SDI used as SDA. SDI needs a 4.7K pullup which is what tells the chip it is talking I2C.

 

PS.  Just looked at the module in zoom.  I can't understand the layout since it also has a reverse side which isn't shown.   This seems to the the same part except both sides are shown and a description of the pinouts are given and this module diagram:

HTB1xFeBNVXXXXaUXpXXq6xXFXXXL.jpg?size=4

 

Hope this helps :)

 

It looks as if it is wired for I2C but you will need to remove R4 and pull up SDO if you want to use 0x76 address.

Link to comment
Share on other sites

Terry

I may not have made myself clear.

I want to connect 10 of these to the RPI, and have a reliable RTC as well.  Ideally, they will be on the same bus as that leaves RPi pins free to do other 'stuff'.

Maybe they are not what I need for this project, just that they are ridiculously cheap and I keep reading that up to 128 of them can be connected in parallel.

There seems loads of examples about using just one of them via the I2C, very little about using them via SPI, and then there is confusion about which bus protocol to use (some say you can connect two, others say up to 128, some say only on SPI, others say on I2C, all very confusing).

There does seem to be a way to read them using the CSB, set it high and it is locked out, set it low and you can read it, I think.  But this uses up GPIOs on the board, which is what I am trying to avoid.

I can see why 1-Wire is so sensible, a nice easy address (shame you can't put your own name on it i.e. living room instead of 28-0516b2b58bff).

Edited by SteamyTea
Link to comment
Share on other sites

Nick, an RPi model A costs what? £25? or thereabouts.  IMO, it is a big mistake trying to overload a single board and be miserly about keeping GPIOs free. 

  • SPI is point-to point and buffered so you can drive it at high clock rates so is good for high bandwidth connections like to Flash 3pins for one SPI connection, 4pins for two.
  • I2C is a bus with a bus protocol.  The complexity of all this his handled by the I2C drivers both on the RPi (and on the ESP chips that I use), so don't let this put you off.  Even if you want 10 of the buggers at 2 per I2C bus, this is still only 20 IO pins.

I guess I am a bit more relaxed because I only use an PRI3 ModB for my main server and the Wemos ESP8266 boards for everything else at under £3 per board ( I don't connect any physical devices directly to my RPi).  OK the project box, grommets, cabling, vero board,  etc., bumps the price per unit to maybe £15 but this is still pin money.

 

As for as addressing OW by meaningful names is concerned, this is what high level languages like Python and Lua are for: you can have keyed lookup into arrays.

Link to comment
Share on other sites

Just a gentle warning about I2C; the bus length has to be pretty short.  Anything over a metre or two may well get flaky - I speak from experience! 

 

SPI has the advantage that you can run it nice and slowly, if you bit-bang it, so it will often work over longer cables OK without drivers, but it does mean writing your own code to bit-bang the interface (not really onerous).  Neither I2C nor SPI are as robust over long cables as the one-wire protocol, in my experience. 

 

Where I've used either SPI or I2C sensors I've used a remote interface, usually a small 8 pin PIC, to convert either I2C or SPI to low speed serial.  Low speed serial at 1200 or even 2400 baud, with just a 5V swing, is surprisingly good.  I'm running a bidirectional 2400 baud serial link at 5V from the display, temperature and CO2 sensor in the hall, up to the main data logger in the services room, around 15m of cable, and it works fine, even with no CRC, just a simple handshake.  I2C, even at the slow bus speed, just won't work over that distance, which isn't surprising as it was designed for board to board data transfer within one item of equipment.

Link to comment
Share on other sites

The more I read about the two systems, the less I like them.  I had read about the cable limitation on I2C, seems a good reason to avoid it for household data collection.

It may be easier and cheaper to use a Zero with an ethernet adapter, would still be under 17 quid for 4 sensors.

I shall try something out soon, just got a very busy week this week as I have a show at Land's End starting Friday for a week (kitchen table is full).  All bad timing.

 

 

 

 

 

 

Link to comment
Share on other sites

Or a ZeroW and then you only need a power supply.

 

My weapons of choice are PICs, but that's really just because I'm a hardware person, and I've been using microcontrollers for decades, so am comfortable working this way.  This does mean I'm not a great help to anyone using a RPi!  I'm afraid my whole house data logger uses around 6 PICs, doing various jobs, all talking to a master logger that stores data every 6 minutes to a USB stick, as a series of monthly, dated, .csv files.  It does mean I have to physically swap out the USB stick to analyse the data, but I'm happy with doing that.  I do display data on a couple of 20 character by 4 line LCDs, one in the hall and one in the services room, plus I have a GPS master clock and display up there that keeps everything in the house working accurately to a UTC reference time.

Edited by JSHarris
typo, "was" when I meant ""way"
Link to comment
Share on other sites

5 hours ago, TerryE said:

how about talking about what you are trying to do?  How many and how far separated do you want your sensors? 

Just logging around the house, so cables would be no more than 10m long, probably a lot less.

I was doing it with DHT22/11s, but the 11s proved unreliable and inaccurate (I do have some more 22s which seem much better, must rig some up and calibrate them).

 

It would be nice to use very cheap equipment if it proves accurate and reliable, I seem to have reliable code (and simple) for my logging now, just a matter of time to see how reliable it all is.

Link to comment
Share on other sites

Nick, I am planning to just use something like N ×

Plugged into a USB plug charger, say another £5,  or £10 per room.  I could mount these in a small project box but it isn't worth it  The other option is to use an ESP-01 chip 3.3 drop down and male USB packed in a length of 25m overflow pipe for under £5 which plugs directly the the USB plug charger female USB.  Neater.

 

They all talk back to my RPi using Wifi, which is the simplest inter-room-cabling system for me.   I just don't see the point of running data cables into every room.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...