element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Community Hub
Community Hub
Member's Forum Help, Pi2 and MCP3008
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Leaderboard
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Community Hub to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 14 replies
  • Answers 2 answers
  • Subscribers 571 subscribers
  • Views 1503 views
  • Users 0 members are here
  • mcp3008
  • spi
  • analog
  • pi2
Related

Help, Pi2 and MCP3008

Former Member
Former Member over 9 years ago

My analog board and Pi2 are inside a well ventilated metal enclosure. The MCP3008 is not getting warm to the touch. There is a fan pushing air though the enclosure.

 

All signals look clean on the scope.

 

Vcc, and Vref are 3.3v.  All samples are within 0-3.3v.

 

I have tried all different SPI clocks form 500Khz to 3Mhz.

 

An identical board sitting on my desk has been working fine for 45 min.  Three different PI and MCP3008 inside the metal enclosure will work for about 5 min after a power cycle then the readings go nuts, before all eight channels revert to zero.

 

I will leave the working board running all night.  The AC cuts off here at about now.  So the temp in this office will go up over night to about 30c (Singapore) so I guess I will know if the temperature affects it.

 

Here is my "transfer" code:

 

//==================================================================

//Private main read/write

//==================================================================

int CMCP3008::spiWriteRead( unsigned char *data, int length)

{

  struct spi_ioc_transfer spi[length];

  int i = 0;

  int result=-1;

 

    //One spi transfer for each byte

    for(i=0;i<length;i++)

    {

      memset(&spi[i],0,sizeof(spi[i]));              //Flush

      spi[i].tx_buf        = (unsigned long)(data+i); //Transmit from "data"

      spi[i].rx_buf        = (unsigned long)(data+i); //Receive into "data"

      spi[i].len           = sizeof(*(data+i));

      spi[i].delay_usecs   = 0;

      spi[i].speed_hz      = this->speed;

      spi[i].bits_per_word = this->bitsPerWord;

      spi[i].cs_change = 0;

    }

 

    result=ioctl (this->spifd,SPI_IOC_MESSAGE(length),&spi) ;

    if(result<0) {  printf("spiWR==[%i]\n",result); return -1; }

 

  return 0; //Success

}

 

Please help

  • Sign in to reply
  • Cancel
  • shabaz
    0 shabaz over 9 years ago

    Your code is a bit odd, you only need one of those structs, not an array surely?

    A single struct is sufficient to transfer multiple bytes, since the rx_buf and tx_buf are arrays.

    Anyway, although incorrect it looks benign, it shouldn't cause issues.

    I personally don't use the same locations for rx and tx, whereas you have them both

    pointing to the data array. If it doesn't cause an issue then great - it's just something I've never

    done. and I've never tested to see if it is possible.

    Apart from that I can't see an issue with the code but I've only examined it briefly.

    I think you're saying the MCP and Pi are in the same enclosure. Do you have anything like long cables from the

    MCP to the sensor? Perhaps you're getting noise on them?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to shabaz

    Thank you.  I have been so frustrated that I overlooked the "oddity".  but as you said, it is not likely to be the problem.  The device on my desk has a longer cable run than the device in the enclosure. The readings are dead on.

     

    This is the rest of my object:

     

    //==================================================================

    //Read

    //==================================================================

    float CMCP3008::readADC(int Channel)

    {

        unsigned int  read;

        float value;

        float result=0;

        float m_Step=.003222656;

     

        //Read value

        read=analogRead(Channel);

        value=(float)read;

        result=value*m_Step;

     

        return result;

    }

     

    //==================================================================

    //Read with Multiplier

    //==================================================================

    float CMCP3008::readADC(int Channel,int Mult)

    {

        float value;

        float mult=(float)Mult;

     

        value=readADC(Channel);

     

        if(Mult==1) return value;

        return value*mult;

    }

     

    //==================================================================

    //Constructor

    //==================================================================

    CMCP3008::CMCP3008()

    {

        this->mode=SPI_MODE_0;

        this->bitsPerWord=8;

        this->speed=1000000;     //Default to one MHz

        this->spifd=-1;

    }

     

    //==================================================================

    //Destructor

    //==================================================================

    CMCP3008::~CMCP3008()

    {

        this->spiClose();

    }

     

    //==================================================================

    //Private Raw read (with averaging)

    //==================================================================

    //Do six reads

    //Drop the one highest, and one lowest

    //Average the remaining 4

    unsigned int CMCP3008::readAverage(int Channel)

    {

        unsigned int read;

        unsigned int result=0;

        unsigned int highest=0;

        unsigned int lowest=0x3FF;

        int cnt=0;

     

     

        analogRead(Channel);    //Dummy read

        while(cnt<5)        //Do six reads

        {

            read=analogRead(Channel);

            if(read>highest) highest=read;    //If highest

            if(read<lowest)  lowest=read;    //If lowest

            result+=read;

            cnt++;

        }

        result-=highest;    //Remove the highest

        result-=lowest;        //Remove the lowest

        result>>=2;        //div by 4

        result&=0x3FF;        //Bounds

     

        return result;

    }

     

    //==================================================================

    //Private raw read

    //==================================================================

    unsigned int CMCP3008::analogRead(int Channel)

    {

        unsigned int a2dVal=0;

        unsigned char data[3];

        unsigned char config;

        int result=0;

     

         data[0]=0b00000001;          //First byte transmitted -> start bit

        Channel&=7;            //Mask off any rouge illegal values

        config=(unsigned char)Channel;    //Get the channel

        config|=8;            //Single ended operation

        config<<=4;            //Move the three bits over

        data[1]=config;

        data[2]=0b00000000;         //Third byte transmitted....don't care (zero)

     

        //Do the transactions

        result=this->spiWriteRead(data,3);

        if(result<0) return 0;

     

        //Ignore data[0]

        a2dVal=(unsigned int)(data[1]&0x03);    //Lowest two bits of data[1]

        a2dVal<<=8;                //Position for merge

        a2dVal|=(unsigned int)data[2];        //All eight bits of data[2]

        a2dVal&=0x3FF;                //Mask

     

        return a2dVal;

    }

     

    //==================================================================

    //Private main read/write

    //==================================================================

    int CMCP3008::spiWriteRead( unsigned char *data, int length)

    {

     

      struct spi_ioc_transfer spi[length];

      int i = 0;

      int result=-1;

     

        //One spi transfer for each byte

        for(i=0;i<length;i++)

        {

          memset(&spi[i],0,sizeof(spi[i]));              //Flush

          spi[i].tx_buf        = (unsigned long)(data+i); //Transmit from "data"

          spi[i].rx_buf        = (unsigned long)(data+i); //Receive into "data"

          spi[i].len           = sizeof(*(data+i));

          spi[i].delay_usecs   = 0;

          spi[i].speed_hz      = this->speed;

          spi[i].bits_per_word = this->bitsPerWord;

          spi[i].cs_change = 0;

        }

     

        result=ioctl (this->spifd,SPI_IOC_MESSAGE(length),&spi) ;

        if(result<0) {  printf("spiWR==[%i]\n",result); return -1; }

     

      return 0; //Success

    }

     

     

    //==================================================================

    //SPI open

    /Added SPI clock setting

    //==================================================================

    int CMCP3008::spiOpen(std::string devspi,unsigned int Speed)

    {

        int result=0;

     

        //Set Speed

        this->speed=Speed;

     

        //Open

        this->spifd = open(devspi.c_str(), O_RDWR);

        if(this->spifd<0) result=-1;

     

        //Set SPI write mode

        if(result==0)

        {

            result=ioctl (this->spifd, SPI_IOC_WR_MODE, &(this->mode));

            if(result<0) result=-1;

        }

        //Set SPI read mode

        if(result==0)

        {

            result = ioctl (this->spifd, SPI_IOC_RD_MODE, &(this->mode));

            if(result<0) result=-1;

        }

        //Set bits per word for write

        if(result==0)

        {

        result = ioctl (this->spifd, SPI_IOC_WR_BITS_PER_WORD, &(this->bitsPerWord));

            if(result<0) result=-1;

        }

        //Set bits per word for read

        if(result==0)

        {

            result = ioctl (this->spifd, SPI_IOC_RD_BITS_PER_WORD, &(this->bitsPerWord));

            if(result<0) result=-1;

        }

        //Set write speed

        if(result==0)

        {

            result = ioctl (this->spifd, SPI_IOC_WR_MAX_SPEED_HZ, &(this->speed));

            if(result<0) result=-1;

        }

        //Set read speed

        if(result==0)

        {

           result = ioctl (this->spifd, SPI_IOC_RD_MAX_SPEED_HZ, &(this->speed));

            if(result<0) result=-1;

        }

     

        if(result)     { printf("MCP3008 (%s) open failed!\x0A\x0D",devspi.c_str()); return -1; }

     

        printf("MCP3008 (%s) Open\x0A\x0D",devspi.c_str());

        return 0;    //Success

    }

     

    //==================================================================

    //Private SPI close

    //==================================================================

    int CMCP3008::spiClose()

    {

        int result=-1;

     

        result=close(this->spifd);

     

        if(result< 0)  result=-1;

        else result=0;

     

        return result;

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 9 years ago in reply to Former Member

    If the code works on your desk but not in the real environment, it is likely to be an electronic/electrical issue.

    The environment can have an effect.

    How long are the cables to the sensor, and what is the sensor? Probably some photo is needed showing in detail your arrangement, cables and sensor and the boards.

    Does the MCP3008 board have any input buffering? Most cheap ADC boards neglect this, and as a result can cause issues with incorrect readings. I've noticed the code eliminates two readings from six and then averages the remainder; was this because you're getting very noisy readings?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to shabaz

    The wire between the board and the unit under test is about 10cm long. The samples are all low impedance.  No buffering is used.  Only voltage dividers capacitors, and and transient suppressors are employed.  The dividers, and filters are very close to the device.  I have checked the signals for noise, using a scope, right at the device.  The signals are clean.

     

    The DC voltages are spot on when compared to readings from a Fluke 87.. 

     

    The habit of taking multiple readings subtracting the highest and lowest, then averaging the remainder is standard practice in my line of work.

     

    The inputs are filtered.  Vcc and Vref are filtered though a simple LC Pi filter.

    I just pulled the board out of the enclosure.  It has been running just fine for over 30 min on my desk. I expect that if I put the spare inside the enclosure, the problem will return.

     

    I just set the Fluke 87 altimeter to AC volts.. I checked Vcc, and Vref.  I am reading 2.2mV AC.  I will place the board back in the enclosure, and check it while connecting one cable at a time.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to Former Member

    The purpose of the circuit is to monitor a power supply, and a non-rechargeable battery.  Again all the samples are low impedance.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 9 years ago in reply to Former Member

    I see - it sounds like you know what you're doing with regards to the electronics side.

    The only other thing I can see is that the ::readAverage method should have

    while(cnt<6)

    instead of

    while(cnt<5)

     

    Apart from that, I can't see what else could be ocurring : (

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 9 years ago in reply to Former Member

    One other thing worth trying is to log the raw SPI content that is read from the ADC, e.g. dump it to the screen or a file the contents of data in ::spiWriteRead. Just in case it is possible to spot something from there when the fault occurs inside the enclosure. You can delete the memset function call there, it is not having any useful effect (nor a negative effect).

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to shabaz

    Thanx for all the help.  I will check my ground loops. 

     

    I think I will set a loop up that will throw the raw value to the screen every couple hundred milliseconds, and see what it takes to cause the problem.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to shabaz

    Do you know of a way to "Reset" the chip?  I have tried everything and I cannot get it to work.

     

    I have probed every line, and the signals are clean.  It should not matter how far away the voltage is so long as it is between 0 and 3.3v.

     

    The chip just dies and will do nothing until the power is cycled.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 9 years ago in reply to Former Member

    If the MCP is getting into some hanged or latched state, this sounds like it could be enough to damage it eventually, a reset may just hide the issue for a while.

    You may need isolation from whatever circuit you're connected to I'm afraid.

    Just resetting a chip could be done with a MOSFET or two to control the power to it, but I don't think this is a good solution for this issue unfortunately.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube