manxam
Posts: 22
Joined: Sat Feb 09, 2013 1:55 am

MCP3008 SPI Inaccurate readings..

Sat Feb 23, 2013 2:50 am

I was previously using these sensors on the Gertboard using the ATMega 328p and the values were all as expected. I'm now trying my hand at an MCP3008 on the same breadboard and read via SPI but the results are "strange"

I have two identical TMP36's and two identical LDRs. The TMP36s are about an inch apart and the LDRs are about an inch apart. Using the ATMega the LDR values were within a couple of points of each other and the TMP36s were within .5 of a degree.

The output from the MCP using python and SpiDEV I get results such as the following:

TMP0: 212 18.3 C
TMP1: 207 16.7 C
LDR1: 507
LDR2: 369

I've even gone so far as to add a bunch of delays between reads as well as reading the pin once and then throwing away the first read in case (as per an issue I read about with Arduinos multiplexed ADC figuring it may apply here).

If anyone can shed some light on the subject I'd really appreciate it!

My code is on pastebin http://pastebin.com/scj0wCmd

Thanks!

User avatar
tony99
Posts: 19
Joined: Mon Mar 04, 2013 2:03 am

Re: MCP3008 SPI Inaccurate readings..

Mon Mar 04, 2013 11:56 pm

Hi,

FYI, MCP3008 is limited to 200ksps, and is quite capable of doing a good job at Analog-Digital conversion.

The key thing is to make sure that you do not exceed the data rate - both minimum and maximum. This means that the SPI sample clock needs to be at least 10kHz, and at most 3.6 MHz. Stay within these levels, and your readings should be OK.

You might find, if you're taking a lot of fast readings that your first few readings a little off. Just discard these.

The main thing to be concerned about is the stability of VREF. We've noted that if you power the ADC with the 5V power supply from the Raspberry Pi, and run ethernet at the same time, that there can be variation in the +5V power level. If this is fed directly into VREF of the 3008, then your readings will be off (whenever the ethernet lights flash - or at least that's our experience). (This is not a fault of the Raspberry Pi - the +5V was never intended to be a "quiet" supply).

(shameless plug)
We are using the MCP3008 in our RAS-DAS-1 data acquisition card for the Raspberry PI. In our case, we provide choices for the VREF via temperature compensated reference, or a user programmable reference (as well as external sources if desired).

http://www.kickstarter.com/projects/254 ... spberry-pi

Best Regards,

Tony

manxam
Posts: 22
Joined: Sat Feb 09, 2013 1:55 am

Re: MCP3008 SPI Inaccurate readings..

Tue Mar 05, 2013 12:18 am

Thanks for the reply Tony. I'm running the MCP3008 via the Pi's regulated 3V3. What would be the reason for running it at an unregulated 5V? I would expect the issue you mentioned below to be an issue when run that way due to to power supply stability and/or fluctuations.

P.S I took a look at your kickstarter and it seems like a really interesting project though not within the scope of anything I'd use it for. I hope you meet your goal though as it looks very well thought out!

User avatar
tony99
Posts: 19
Joined: Mon Mar 04, 2013 2:03 am

Re: MCP3008 SPI Inaccurate readings..

Tue Mar 05, 2013 3:16 am

Hi Manxam,

Sometimes you might have a signal with a larger voltage swing than 3.3V.

The MCP3008 is specified to run faster at VDD=VREF=5.0V (200ksps) vs VDD=VREF=2.7V (75 ksps). Also, if you look at the performance specifications, you will see that the INL, DNL, and all other specs are much better at the higher voltage.

The biggest reasons that I see avoid use of the 3.3V from the raspberry pi is to avoid overloading the raspberry power supply, and to prevent digital noise from entering the system. (When you're sampling at 10 bits of resolution, and using a 3.3V reference, each step is just 3.3mV - so if everything is working right it's very sensitive).

I've found the best performance from the MCP3008 comes when you supply it with a nice quiet linear supply at 5V. It all depends on how much quality you want from signal you are trying to sample.

On the RAS-DAS-1 board for instance, we can supply VDD at 5.0V (either from the RPi supply, or from an external source), and set VREF using a 2.5V temp. comp. precision reference. That reference would allow for very accurate readings.

Best of Luck,

Tony

PS : Thanks for the kind words about our kickstart - we hope it will be successful. I think that the only thing the Raspberry Pi is missing is a good analog-digital-converter, so we're trying to fix that. :D

gstreeter
Posts: 106
Joined: Sun Sep 02, 2012 11:11 am
Location: UK

Re: MCP3008 SPI Inaccurate readings..

Wed Mar 06, 2013 12:55 pm

You might like to check out my posting on the Gertboard forum "Gertboard ADC with TMP036" (http://www.raspberrypi.org/phpBB3/viewt ... 58#p276458)

There's an issue in that the MCP3008 has a relatively low input impedance which causes the TMP036 output voltage to sag and give under-readings. The TMP has a high output impedance and cannot provide the current needed to charge the MCP sample and hold capacitor.

Using an op-amp to buffer the TMP036 fixes this issue. You do not see this with the ATMega328 as it has high impedance inputs on the A-to-D pins.

See Section 6.3 of the MCP3008 data sheet: http://www.adafruit.com/datasheets/MCP3008.pdf

manxam
Posts: 22
Joined: Sat Feb 09, 2013 1:55 am

Re: MCP3008 SPI Inaccurate readings..

Fri Mar 08, 2013 7:30 pm

I really appreciate your responses. So, given your answer, would it be preferable to use an Atmega for LDR and TMP36 readings and outputting data via serial vs using an MCP3008? This would just be for measuring light level and temperature in a room. One LDR and one TMP36 per room hacked into the end of an RJ45 plug (I hope) which I will just plug into one of my ethernet jacks in a room (20-30 feet from wiring closet) and then from wiring closet to MCP3008 or Atmega.

Thanks again!

gstreeter
Posts: 106
Joined: Sun Sep 02, 2012 11:11 am
Location: UK

Re: MCP3008 SPI Inaccurate readings..

Sun Mar 10, 2013 6:12 pm

Check out the TMP036 data sheet http://dlnmh9ip6v2uc.cloudfront.net/dat ... _36_37.pdf

You will have to take into account the low current output of the TMP and the loss over the long line and will probably need to boost this as per the description on page 15.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MCP3008 SPI Inaccurate readings..

Sun Mar 10, 2013 6:35 pm

Personally I think you are using a way too fast A/D for what you are measuring. 200ksps for room temperature, that if somebody is measuring fast, that is ONCE per second. Light is similar

Personally considering the accuracy of LDR I would use a I2C A/D like PCF8591 (8 bit) which is specced for lower voltages and is ideal for these types of readings. Place a PCF8591 on a board with LDR, TMP36 with caps to help filter/average the levels, take power from cable. That way the analog path is short and communications is digital over the long cable runs.

Using reasonable grade cable the PCF8591 can work with the 5V being distributed over the cabling as the device will work down to 2.5V, put separate fuses (100mA or less) on the 5V runs to each unit at network point to avoid cable faults blowing anything further up.

There are other 10bit and more I2C A/Ds for slow sampling. Years ago I did a test setup that had a 12bit A/D at the end of 20ft of cable which used the I2C clock for clocking the A/D, which reduced the noise for the light level readings we were taking many times a second as this was a test setup in a darkened room.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

manxam
Posts: 22
Joined: Sat Feb 09, 2013 1:55 am

Re: MCP3008 SPI Inaccurate readings..

Mon Mar 11, 2013 12:21 am

Paul, OK now you're really piqued my interest and opened another can of worms so-to-speak :)

What's the benefit to using a slower ADC? With the MCP30008 on the SPI bus I just have a loop that reads every 5 seconds or so, takes a few readings back to back with a small delay in between, and then averages the result. I do this with both the TMP and LDRs. Does moving to a slower ADC make this reading more accurate for these type of sensors?

Regarding the 8 bit vs 10 bit. I realize that this won't affect the LDR much but won't I lose a lot of temperature resolution (i.e accuracy) with the TMP36 as I'll have 0-254 vs 0-1023 for readings?

Again, thanks for your help and patience with the answers guys!

Cheers!

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MCP3008 SPI Inaccurate readings..

Mon Mar 11, 2013 12:59 am

manxam wrote:Paul, OK now you're really piqued my interest and opened another can of worms so-to-speak :)

What's the benefit to using a slower ADC? With the MCP30008 on the SPI bus I just have a loop that reads every 5 seconds or so, takes a few readings back to back with a small delay in between, and then averages the result. I do this with both the TMP and LDRs. Does moving to a slower ADC make this reading more accurate for these type of sensors?
Look at this another way to take measurements at 5 samples/sec even aggregated up to over 16 channels is a max rate ADC of 80sps, so a 200ksps is overkill for your needs.

However the main factor you are forgetting 8bits measured AT the sensor is more accurate than the 40ft+ round trip for sensor to A/D at any higher resolution. The unit has to be powered so your analog sensor is
  • being fed by 20ft+ of cable, with its voltage drops and noise pickup,
  • to retuirn down 20ft+ of cable a low voltage and low current signal, susceptible to the same voltage drops and noise pickup
No doubt most of the noise will be mains. mobile phones, spikes of nearby equipment.

If you put a low power A/D at the distal end (by the sensor) that clocks the A/D from something like the I2C clock then the analog signals can be kept electrically cleaner, simple low value caps on the analog signals and any op-amps will improve stability and range matching. If necessary you could ensure the A/D and sensors runs of 3V3 reference or similar with a cheap bandgap reference, thus negating any cable voltage drops for about 1V5 in cable runs.

By using this method helps to keep A/D and comms noise out of the signals being measured. If you want you find a low power 10 or 12 bit I2C ADC, the main point is keep the analog circuit between sensor and ADC SHORT gives better results..

Prove the design. Make a PCB, test that then make as many as you like.

I2C devices allows for simpler cabling, and are address selectable. If you want there are I2C switches so you can level translate and isolate each run until it is needed.
Regarding the 8 bit vs 10 bit. I realize that this won't affect the LDR much but won't I lose a lot of temperature resolution (i.e accuracy) with the TMP36 as I'll have 0-254 vs 0-1023 for readings?
Actually it is 0-255 vs 0-1023 to be pedantic :lol: These are the resolution, cable runs pickup and everything else affect the accuracy, and in A/D system terms the Effective Number Of Bits (ENOB) you may have 10 bit resolution but you may not get all possible values due to range of sensor, noise and other characteristics.

However any measurement close to the sensor is going to be more a lot more accurate whatever resolution you achieve. If you calculate what range and steps you can get from an 8 bit converter, bearing in mind the TMP36 is rated as being +/-2 deg C accuracy being able to read to 0.5 deg C steps or smaller seems pointless. To get that or better accuracy needs careful design of the thermal dynamics and thermal mass of the sensing area. Otherwise the temp sensor is affected by air currents or the temperature of its package leads compared to the die sensing area. See sections on Mounting Considerations in datasheet. Put too big a heatsink on the board and you will get wrong readings as the chip is being held at slightly lower temp, conversely if the chip self warms, your readings will be high. Seriously consider thermal paste and a piece of metal attached to the device if you want accurate results.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: MCP3008 SPI Inaccurate readings..

Mon Mar 11, 2013 1:07 am

Have a look at AD7995 for 4 channels 10 bit SAR ADC with I2C interface in 8 pin chip

That is the first I found there are many others

When I last looked at doing distal end A/D I was measuring light from LCD where we had to be sure we were able to capture the light level change when LCD went from 0 to 1 out 0-255 range on a single colour or as all RGB together.Hence eliminating noise and if I remember correctly 2 channel 12bit A/D to take two angular measurements very close to each other about 12 years ago.

Hence doing light measurements in a darkened room, we also measured background light level.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

manxam
Posts: 22
Joined: Sat Feb 09, 2013 1:55 am

Re: MCP3008 SPI Inaccurate readings..

Mon Mar 11, 2013 4:48 am

I really appreciate the very thorough response Paul. You've given me a lot more information and advice than I expected. I'll order a couple different I2C ADC and tinker around with them vs the MCP30008 and the ATMega ADC. We'll see what works best for this setup. This isn't mission critical stuff but I wouldn't mind the additional accuracy or lessened headaches.

Cheers for everything!

joaoneves1
Posts: 2
Joined: Thu Apr 04, 2013 12:04 pm

Re: MCP3008 SPI Inaccurate readings..

Thu Apr 04, 2013 12:17 pm

Hi all

I'm trying to make readings as fast as I can with the MCP3008. At first I tried the spidev library for python, but I only could do ~1400 S/s. Is there anyway to do it faster, i.e., go to the limit of the MCP3008? If someone have an example code I'd appreciate

Thanks in advance.

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: MCP3008 SPI Inaccurate readings..

Thu Apr 04, 2013 1:17 pm

Question: how are you measuring the throughput and is this without any other IO? Sometimes the limiting factor for peripheral IO is how the data is being recorded and not so much how it is being acquired. As a first test, I would do a simple read loop of the MCP3008 with no data storage and no user display to the terminal, to get the maximum possible read rate. If the read rate is acceptable, you can then look at threading the code so as to not block SPI reading while doing other operations.

If you have the SPI clock rate set to the maximum supported by the MCP3008 and are limited by the Python interpreter, the next option is a C/C++ application.

Enjoy!
Bill

Return to “Interfacing (DSI, CSI, I2C, etc.)”