Many SPI tutorials use common notation of bus design where each device is directly in parallel connected to SCK, MISO, MOSI and CS lines. This normally works without problem, but in general, there can be problems when more than one SPI device is on the bus. DorkbotPDX rises several issues that can occur on poor design. Here are three suggestions for better SPI improvements:
- Use pullup resistors on all chip select signals.
- Verify tri-state behavior on MISO: use a tri-state buffer chip if necessary.
- Protect bus access with SPI.beginTransaction(settings) and SPI.endTransaction().
Pull up resistor helps to prevent response from multiple devices at once. This might come from poor software design when CS pins aren’t initialized properly. Second problem is with MISO pin. In fact some SPI devices doesn’t enter tri-state even when CS is pulled high. So when talking to other SPI device this will cause failures. Be sure to check if SPI device supports tri-state when inactive, otherwise add external tri-state buffer like 74AHC1G125. And last thing is SPI transactions. In systems where multiple SPI devices are used there is a risk to use different settings that were selected on different device and most importantly transactions can ensure exclusive use of SPI bus when needed.
Indeed, with Arduino declared simplicity most of us are getting lazy to dig deeper in to dangers that might await in obvious places. So always do your homework before building another schematic and don’t assume that things will work out of box every time. And this does not only apply to SPI.