# Serial Peripheral Interface (SPI) vs Two-Wire Interface (TWI, I²C) wire length

I'm using Arduino DUE to read data from an IMU(inertial measurement unit) sensor (MIKROE-1577, MPU-6000)

This IMU sensor supports both Serial Peripheral Interface (SPI) and Two-Wire Interface (TWI, I²C). According to the datasheet,

• 400kHz Fast Mode I2C for communicating with all registers
• 1MHz SPI serial interface for communicating with all registers (MPU-6000 only)
• 20MHz SPI serial interface for reading sensor and interrupt registers (MPU-6000 only)

Note that the Arduino DUE can set the SPI clock using setClockDivider() and I generated 1MHz, 4MHz, 8MHz clock frequency.

My situation; I have to make a long wire to place this IMU sensor on a table.

I prefer to use SPI because it is faster than TWI.

But if I set the SPI clock frequency to 4 MHz or 8 MHz, I noticed that the SPI signals become a sawtooth wave, not like a pulse wave, if the wire gets longer.

I really want to read the IMU data with an SPI clock of 8 MHz. How can I calculate the wires' length, which doesn't distort the waveform strongly, if I want to set the SPI clock frequency to 8 MHz?

Also, in contrast, if I choose the 400kHz Fast Mode I2C, how long can I extend the wires?

• (1) At what rate do you actually need to sample the IMU? How much data do you need to receive from it per sample? (2) What sorts of lengths are we talking about? Meters? Tens of meters? Longer? (3) Related posts: about long SPI, about long I2C. – Nick Alexeev Mar 29 at 17:44

The limiting factor is the capacitance of the interconnecting wires. A pair of wires has a capacitance between them, measured in $$\Fm^{-1}\$$. Therefore, as your wire length goes up, the capacitance goes up. This means that, to a first approximation, you can think of the wire and the driver as an RC filter, so your nice square pulses no longer look like squares. You might be able to calculate the wire length, but there are a lot of variables, so a definite answer is unlikely. Some suggestions:

1. Think about using other protocols rather than SPI or I2C, which are designed for short distance PCB connections.
2. Use as low a frequency as possible for your application.
3. Use as high a drive as possible. There may be a "strength" register setting for the SPI driver, or for I2C use as low as possible value for the pull-up resistors.
4. Do some local processing where the IMU is, so you don't need the full data bandwidth back to the central controller.

Overall, a good engineering problem to be solved - lots of contradictory requirements, so spend a bit of time actually specifying your system, and then identify where you have problems. For example, do you need to read out at 8 MHz, or is 100 kHz enough?

• Hi, @awjlogan Thanks for your answer! Q: do you need to read out at 8 MHz, or is 100 kHz enough? A: 1 MHz via SPI is required, but I wanted to test 4 MHz and 8 MHz. – David Lee Mar 29 at 17:27
• Also, may I ask about what "Use as high a drive as possible." means? Are you saying I have to lower the pull-up resistors values? – David Lee Mar 29 at 17:29
• @DavidLee No problem. If you only need 1 MHz, then use that; faster clocks will need shorter/more careful wiring. That was more of a hypothetical - i.e., if your spec is for X, then design for X. In terms of "high drive", sticking with the RC approximation, it's making the driver's impedance R as low as possible so your edges are as sharp as possible. For the Due, I believe you can program the DRVSTR register to provide higher driving strength (more current) to the pins - probably best search for the actual IC onboard, but a lot of MCUs have this feature. – awjlogan Mar 29 at 17:43
• Thanks, @ awjlogan! "faster clocks will need shorter/more careful wiring." - I see. One last thing; are there any formula/theory to calculate the length of the wires when using fast clock SPIs or 400kHz I2C? – David Lee Mar 29 at 21:36
• This will be a horrible, back of envelope calculation: take a properly defined cable geometry, e.g. Cat-5, which will specify the capacitance per metre (52 pF/m at 800 Hz). Use $\tau = (RC)^{-1}$. Take $\frac{1}{5\tau}$ as the fastest clock you can run. The I2C, R is the pull-up value. For SPI, you would treat it as a constant current (output current specified in the datasheet). Very rough, and not to be relied on at all - doesn't account for noise etc. – awjlogan Mar 29 at 21:47