0

I am using a MCP3002 to convert an analog signal to digital data. It works fine actually, sends good data. However; After a while, when the power is turned off and on again a few times, it only returns zeros. If I Power it on the next day, it works for a while again.

I am just beginning with SPI, so I do not know 'all' the basics. Is there something I should reset when powering on or something? (Clock, Timing, etc.)

This is the code I am using (pretty straightforward):

#include <SPI.h>
SPISettings spiSettings(7280000, MSBFIRST, SPI_MODE0);
const int SPI_CS = 2;

void setup() {
    pinMode (SPI_CS, OUTPUT);
    SPI.begin();
    SPI.setBitOrder(MSBFIRST);
    SPI.setFrequency(7280000);
}


void loop() {
    SPI.beginTransaction(spiSettings);
    digitalWrite (SPI_CS, LOW);

    uint8_t cmd = 0b11000000;
    byte msb, lsb;
    word assembled;

    msb = SPI.transfer(cmd);
    lsb = SPI.transfer(0);

    assembled = word(msb, lsb);
    Serial.println(assembled);

    digitalWrite (SPI_CS, HIGH);
    SPI.endTransaction();
}

Is there something wrong, or should something be added to this?

  • 2
    Try using a small delay between the transactions. An ADC need a bit of time to convert another value. The datasheet states, that is will clock out zeros indefinitely, when you continue to read (read chapter 5.1 in the datasheet. Last sentence in the left column). So I guess the CS pin is not long enough inactive – chrisl Oct 25 '19 at 7:27
  • Ah, I will try that and read the data sheets! But how does that explain that it works initially? – Revils Oct 25 '19 at 7:44
  • Maybe some weird racing condition, maybe something regarding how the sensor senses the CS pin – chrisl Oct 25 '19 at 8:03
  • 1
    Thank you, I will try it a.s.a.p and come back here to add the results. This way others could use this as well. – Revils Oct 25 '19 at 8:05
  • 1
    Just wondering if you'd missed off the decoupling capacitors, but probably not if it's a BoB. – Majenko Oct 25 '19 at 10:01
1

I thin the following is happening:

The MCP transmits 1 measurement per SPI transaction, which consists of activating chip select, reading data and deactivating chip select. To distinguish between different transactions, the time between deactivation and reactivation of chip select need to have a bit delay between them. Currently your code runs really fast, a few microseconds between the transactions at max.

This may also be connected to the MCP needing time to measure the analog signal.

I think there is some kind of race condition, so that the MCP does not have a new value to transmit, either because the MCP hasn't finished measurement yet, or because the delay between the transactions were just a bit too short, maybe a combination of both.

When the MCP is once in this situation, it will only send zeros at that point (read chapter 5.1 in the datasheet. Last sentence in the left column).

You need to give the MCP more time between the transactions. I suggest using a small delay at the end of the loop() function. You can experiment with the actual value, I would guess, that you can go down to the microseconds range.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.