Reading a SPI sensor over a long distance

Discussion in 'General Electronics Chat' started by nickagian, Dec 24, 2010.

  1. nickagian

    Thread Starter Active Member

    Mar 12, 2010
    34
    0
    Hi folks and Merry Christmas!

    Attached is my schematic. As you can see I use the uC SN250 as SPI master for reading data on the same bus, both from an SPI memory (M25P128) and a SPI pressure sensor (MS5540C). The 74AHC1G126 3-state buffer is because the sensor does not have any CS# line. When GPIO3 is LOW, then the flash memory is enabled for communication and the buffer is in high-Z, whereas when GPIO3 is HIGH, then the buffer is enabled and communication with the sensor an be performed. The SG303LC oscillator provides a clock necessary for the operation of the integral ADC of the sensor. All components are included on the same PCB except for the sensor, which is 3m away. The problem I experience is that the SPI bus seems not to be working correctly, when this long twisted and shielded 3m cable is connected. On the contrary, if the sensor is put very close to the PCB (20cm cables) everything works fine.. When the sensor is connected so far away, I can neither read from the sensor neither read from the memory correctly. Is it possible that the long cables by their own produce such a serious problem? Are you aware of any maximum distance for the SPI bus? Do you have anything else to propose as the reason for this problem?

    Thanks!
     
  2. tom66

    Senior Member

    May 9, 2009
    2,613
    213
    As far as I know the SPI standard does not specify any particular distance limitation, unlike, for example, USB, which defines fixed clock rates and response times. SPI timeouts, if any, are usually specific to your device.

    One problem you could be getting is over long wires, the slew rate of the signal may be affected, noise may be an issue and crosstalk may play a part. Do you have a scope to trace out the performance of the clock and MISO/MOSI lines?
     
  3. nickagian

    Thread Starter Active Member

    Mar 12, 2010
    34
    0
    Hi! Thanks for answering my post.

    Personally, I also do not know any limit for the distance of the SPI protocol. That's why I'm so curious with my problem.

    Yes, I have a scope and have already traced the signals. However, I'm not so sure about what should I search to find. Digitally speeking, the MOSI and SCLK lines traced on the PCB seem to be as they should be. The MISO line, however, doesn't bring the correct data from the memory. I haven't though try to measure the fall and rise times of the signals. Would that be helpful?

    The problem of the communication with the memory is shown-up, even if I just connect the sensor to the PCB, without trying to read from it (from the embedded software of the uC I mean).
     
  4. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Can you post a schematic?
     
  5. nickagian

    Thread Starter Active Member

    Mar 12, 2010
    34
    0
    Here you are... this is the SPI related parts of my schematic.

    Page 4 contains the remote PCB where the sensor (along with some other analogue sensors) is soldered on. The other pages are the main PCB.
    Page 1 is the uC, page 2 is the buffer and the oscillator and page 3 is the flash memory.
     
  6. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    The number of bits per second will have to be decreased to achieve the greater transmission distances. If you have control over the software for the SP250 then you should be able slow down the speed of the data being transmitted.

    hgmjr
     
  7. nickagian

    Thread Starter Active Member

    Mar 12, 2010
    34
    0
    Actually, I have already decreased it to about 300Hz from 1.5MHz which is the previous speed I used. Do you think that this is enough or should I slow it down further?
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    Long cables have distributed capacitance and inductance. The effect on a signal is to round off the corners, reduce the overall signal level, and create reflections. SPI is a board level protocol. This means it is not designed to be a fieldbus. I believe that 12-18 inches is a practical maximum distance without the use of transceivers and other specialized techniques.

    The notion that because you can't find a distance limit there isn't one is just plain nonsense. It may be that the original designers did not think it was worthwhile to find the distance limits.

    For each device you need to find the typical and worst case transmit signal level.
    For each device you need to find the typical and worst case receiver threshold.
    For the 3m of cable you need to see what your clock signal, 300 kHz looks like as it travels back and forth. Yes back and forth because an unterminated transmission line will have reflections and those reflections will change what a receiver "sees" on the data line and when it "sees" it.

    Empirically you can keep reducing the data rate, but you can never be sure that you've gone far enough unles you can characterize the components.
     
    Last edited: Dec 24, 2010
    nickagian likes this.
  9. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Obviously 1.5MHz is going to be a problem but 300Hz is way slower than I imagined you would need to go.

    Are you saying that the SPI communication link is not working at 300Hz?

    hgmjr
     
  10. nickagian

    Thread Starter Active Member

    Mar 12, 2010
    34
    0
    Exactly.. in the first place, I have tested it at 1.5MHz and then at the kHz range, without success. But even at 300Hz that I use now the SPI link is not working neither.
     
  11. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I strongly suspect that the problem you are experiencing has more to do with the fact that the two remote circuits are on different power supplies. One way to overcome this would be to opto-isolate the signals between the controller board and the remote sensor.

    Have you considered opto-isolation?

    hgmjr
     
  12. nickagian

    Thread Starter Active Member

    Mar 12, 2010
    34
    0
    hmmm... interesting idea. No, I haven't ever consider using opto-isolation. However, I have two remarks.
    [1] the two different power supplies that you see are actually coming from the same regulator and have the same value (3.3V), with the difference that they are driven through two different analogue switches. This have been done so that some parts of the system are powered down when they are not needed. But in reality the power supplies are of the same value.
    [2] How do you explain that although the power supplies are different, the SPI bus is ok for small distances?

    Well, I haven't thought of this before..This could indeed solve the situation. However, originally I wanted to have a simple solution, without any uCs on the remote end. I think that I could use this solution, if nothing can be found for the current architecture. Thank you for your proposal.
     
    Last edited: Dec 24, 2010
  13. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214

    The fact that the system behavior is stable for short runs and unstable for long runs would seem to point to voltage drop over the long cable distance. The use of 3.3 volts as your power source means that the tolerence of the system to voltage drop in the ground connection between the two boards is low. Also there could be ringing in the signal cable that can really cause havoc.

    hgmjr
     
    nickagian likes this.
  14. nickagian

    Thread Starter Active Member

    Mar 12, 2010
    34
    0
    Hi!

    I have given second thoughts on what we discussed yesterday.

    • First of all, could you please explain to me why do you say that the 3.3V allows only a small tolerance of the system to voltage drop? I cannot understand this.

    • Secondly assuming that there is ringing and distortion at the signals, what would you recommend me to do to solve this? Does putting some low-pass filters at the spi lines would make things better.

    • Lastly, I thought again the thing regarding the voltages and realized sth. I had forgotten that the processor and the other peripherals (flash memory, sensor) work at a slightly different voltage.

      (This was a huge error when designing the PCB.) They are powered from the same supply rail (+3.3V from the regulator) but between the processor and the regulator a diode has been placed. In fact, an OR-gate was used that originally I thought it was necessary when programming the device, so that during the programming procedure an external voltage could be provided to the processor without destroying the regulator.

      Anyway, in the end this is a mistake and the result is that the processor is working at a voltage of around 3V and thus the digital signals are between 0 and 3V, whereas the devices are powered from 3.3V I feel that this might have been the reason of my problem, in the sense that the 3V of the digital signals may drop until they reach the sensor. However, I have two objections. I have measured the supply voltage at the sensor's end and found it at 3.3V. Doesn't this mean that the voltage of the digital signals does not drop, too? And secondly the signal levels locally, on the main PCB were ok. I have seen them with the scope to be around 3V.

      Anyway, I'll try to remove the diode, bringing the processor and the peripherals at the same supply voltage and then make the tests again. I just hope that this caused these problems!
     
  15. GetDeviceInfo

    Senior Member

    Jun 7, 2009
    1,571
    230
    Do you not have a scope to view the line?

    I would definately be looking into the differential line drivers.
     
  16. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    If speed is not an issue then I would be looking at transmit levels and receive thresholds. Check to see if there might be a problem with different VCC levels. A 3.3 Volt sensor may have a hard time talking to a device powered by Vcc = 5Volts.

    Edit: Wrote first part of response before seeing later posts
    IR drop in the cables will case the level of a transmitted signal to be lower at the receiver than at the transmitter. The ground difference between the two devices will lower the receiver threshold. This is one reason why differential signaling is used with long cables because both wires are subjected to the same effects.

    When considering IR drop with single ended signals there is also the return path to consider, so you actually have an effective distance of 6 meters.

    Trying to imagine what each device actually "sees" and "produces" in electrical terms is incredibly difficult without being able to see the waveforms. Anybody on this board will tell you that one scope picture can be well worth the 1000 hours you will spend trying to solve the problem without one. How much is your time worth?
     
    Last edited: Dec 25, 2010
    nickagian likes this.
  17. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    I don't see this mentioned above, but what type of wire are you using to reach 10 feet? Ribbon cable? Cat 5 (if so, which pairs), etc? This will be an issue of some level no matter which protocol you end up using. I'd also suggest a far end uC to create a 485 bus.
     
  18. nickagian

    Thread Starter Active Member

    Mar 12, 2010
    34
    0
    I will certainly do so and thank you for your advice. However, I have a 3.0V device that tries to talk to a 3.3V device. It's not that big difference.

    Anyway, from the data-sheets of the components I didn't find anything wrong. The input low voltage has a max of 0.3Vcc which gives 0.3x3.3 = 0.99V And the input high voltage has a min of 0.7Vcc, which gives 0.7x3.3 = 2.31V The limits for the sensor are 0.2Vcc = 0.66V and 0.8Vcc=2.64V respectively. The input rise/fall times for the two devices are 5ns for the flash memory and 200ns for the sensor.

    Is there any chance that the signal from the uC can fall from 3.0V down to below 2.64V at the distance of the sensor? Of course in this example I assume that the signal level at the pins of the uC is at its highest value (at Vcc), whereas it can even go down to 0.82xVcc = 0.82x3 = 2.46V.

    But anyway I will bring the devices to the same supply voltage and then test again.

    EDIT : I fully understand what you are saying and I agree with you that some scope's picture are really necessary. I have already made some captures with the scope but I don't have them right now with me. And I don't have access to a scope nor the PCBs until Monday. If I had them now with me, I wouldn't waste my time trying to think theoretically the source of the problem, be sure of that! I intend to do some more tests beginning of next week, take some more detailed captures with the scope and try to figure out the problem. As soon as I have some pictures I will post them here.
     
    Last edited: Dec 25, 2010
  19. nickagian

    Thread Starter Active Member

    Mar 12, 2010
    34
    0
    In total, I use 3 cables of two twisted cores each (to pass the total of 6 signals required for the sensor), AWG26. The cables are shielded with a helicoidal screen and sheathed. Their datasheet states that their operating frequency is "up to 2000Hz". However, I have also tried to use cat5 cables (because I suspected that the cables may have caused the problems), slightly shorter than 3m (about 2m I guess, haven't measured them exactly), and the result was slightly the same: the SPI communication with the flash memory could not take place, but I could read from the sensor. I do not know the reason for this change of behavior (perhaps the shorter length played some role) Nevertheless, this experiment was done late yesterday, I was in a hurry and could not examine the situation further. I have to make the experiment again with the CAT5 cable and take some pictures with the scope in this case. Just as Papabravo said in his previous post, we can't keep doing assumptions unless I have some pictures from the scope to show you.
     
  20. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    I'll wait for scope shots of the circuit (both ends of wire), use the two different types of wire as well.

    Things like this are quite a bit easier when one can provide the pictures.
     
Loading...