SPI CS pullup resistors or code?

Frukost

Well-known member
I've been reading the article at https://dorkbotpdx.org/blog/paul/better_spi_bus_design_in_3_steps/, and need help in clarifying something. In the last paragraph of the Step 1: Pullup Resistors for Chip Select & Reset Signals section I read:

A simpe workaround for devices without pullup resistor involves adding code at the beginning of setup.

void setup() {
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
delay(1);
// now it's safe to use SD.begin(4) and Ethernet.begin()
}

Does this mean that the code above is completely interchangeable with physical pullup resistors on the SPI slave devices? If yes, why?

Thanks in advance
 
Yes, because in the code, you're pulling each CS pin high before you start using any of the SPI devices. In hardware, you are using resistors to pullup the CS pin high before using any of the SPI devices. In both cases this happens before communication is initialized with any of the SPI devices.

I typically use the software approach when I design the board that has all of the SPI devices on it and I am writing the code. It reduces the part count and manufacturing costs slightly. The issue is if you're using breakout boards to test sensors with a solderless breadboard. If hardware pullups aren't on each sensor, people might not know that they need to add some software to pull up each CS pin immediately as the program begins.
 
Thanks for clarifying. It does seem slightly more convenient to trade components for an extra few lines of code.

So to extrapolate, I assume this extends to all code that deals with SPI communication, and not just the setup() function. Without the pullup resistors I'll need to set the CS pin HIGH anytime and before I address another device, no?
 
No. Of course this is library specific, but most SPI libraries should be setting the CS pin to digital output, setting the pin to low when selecting the device and returning the CS pin high when de-selecting the device. So you really only need to set all the CS pins high at the start of the program.
 
Back
Top