I want to measure some battery voltage in a range 0V-4.2V with MCP3008(VDD=VREF=3.3V).

I used a voltage divider with R1=2.7MOhm and R2=8.2MOhm, but it didn't return correct value, I used resistors too high?

How I calculate the resistors value to save the maximum amount of battery charge?

Thanks :)

• Were the values too high or too low? Do you have access to the data sheet? May 22, 2019 at 17:20
• user204580 I forgot to ask you one critical question. What is your SPI frequency? If you set your SPI frequency lower, then you can use high resistor values, and vice versa. Anyway, 1k to 10k should be OK, because your sample/conversion is much much smaller than 200ksps. May 27, 2019 at 15:07

Question

Use MCP3008 to measure voltage of range 0V to 4.2V.

Use a voltage divider with 2.7MΩ / 8.2MΩ to step down value for Rpi but not working.

Resistor values too high?

How to calculate the resistor values?

MCP3008 ADC operation is indeed very complicated. You need to go through the long answer below a couple of times, and skimmed through the references, before you can get a rough idea of what is going on in the following over simplified operation diagram.

Well, @Ghanima's answer is very good, but a bit too professional. The suggestion of using buffer amplifier is also newbie scary.

@Ghanima refers to MCP3008 datasheet Fig 4.1, 4.2. So let us first look at these two figures.

As Fig 4.1 illustrates:

1. The analog voltage to be converted (0V to 4.2V) charges the sample capacitor (20pf), in 1.5 clock cycles.

2. The problem now is that if the clock frequency is too high (in order to get high sample rate), the 1.5 cycle time is not long enough to "fully" charge the sample capacitor (20pF), before conversion starts, with the result that the converted result would be lower than it should be.

3. Of course we can lower the clock frequency (longer time to "fully“ charge the sample capacitor), but then the sample rate (which is proportional to clock frequency) is also lowered. Thus we need to make an engineering trade off, on in newbie language, a balance between "sample rate" and "accuracy" balance.

4. The other thing is that the charge time depends on the "source resistance", which is "more or less" the same, or of the same order (MΩ or kΩ) as the OP's voltage divider resistors.

5. It needs newbie scary mathematics (calculus, differential equations!) to calculate/analyse the appropriate range of resistors for different sample rate ranges

6. Of course the lazy engineers won't do the tedious calculations every time. They just use a very rough chart to get the resistor range from the sample rate range. This is Fig 4.2.

7. Sorry for the long winding stuff. Actually what we need to do is just looking at Fig 4.2, select your sample rate, and find the resistor range in no time, well, less than 3 seconds.

8. For example, if you want a sample rate of 200ksps, then use 1kΩ range. For 75ksps, use 10kΩ to 100kΩ range.

If you find the above explanation confusing, forget it, and just use Fig 4.2 to get the resistor range 1k, 10k or 100k. Or even forget Fig 4.2, just use 1k to 10k. You won't be too wrong, and MCP3008 won't explode, ... :)

References

How to get MCP3008 Sampling Rage - Rpi Forum Discussion

Appendices

Appendix A - MCP3008 ADC sample capacitor charging timing diagram

Appendix B - Why ADC results becomes not accurate again, if clock frequency goes "too low"?

Once upon a time I was a humble MCP3008 newbie. I dared not use high sample rate by setting SPI/clock too high. I used 100kHz, and even down to 50kHz, 10kHz. But then I surprisingly found that, after reaching a certain frequency value, the lower the frequency goes, the less accurate is the ADC result.

I thought hard, read the SAR algorithm, and finally found the reason. I also solved the puzzle of why the ADC SAR circuit can output the MSB of the results so fast (only 2 pulses after collecting sample). I was no longer a humble newbie. I upgraded myself to a MCP3008 ninja, ... :)

Understanding SAR ADCs: Their Architecture and Comparison with Other ADCs - Maxim Integrated

Appendix C - Over simplified MCP3008 sample time and convert Time

Those resistors are in fact too high. Section 4.1 "Analog Inputs" (p. 17) and figures 4-1 and 4-2 (p. 18) of the datasheet are of special interest:

For the A/D converter to meet specification, the charge holding capacitor (CSAMPLE) must be given enough time to acquire a 10-bit accurate voltage level during the 1.5 clock cycle sampling period. The analog input model is shown in Figure 4-1. This diagram illustrates that the source impedance (RS) adds to the internal sampling switch (RSS) impedance, directly affecting the time that is required to charge the capacitor (CSAMPLE). Consequently, larger source impedances increase the offset, gain and integral linearity errors of the conversion (see Figure 4-2).

So the source impedance needs to be low enough to charge / discharge the DAC capacitance of 20 pF. The required resistor is depending on the clock frequency, sampling rate, and acceptable accuracy. It could be necessary to be in the low kilo-ohm range here if you want a high sampling rate.

If it is desireable to have a high resistance of the voltage divider the use of a buffer amplifier is needed to provide a low source impedance to the DAC. Find an example of a buffer amplifier here.

• An example of using the buffered amplifier in the answer to this question: raspberrypi.stackexchange.com/questions/76079/… May 22, 2019 at 19:45
• I fully agree @Ghanima's recommendation of "using the low kilo-ohm impedance to increase the offset, gain, and intergral linear errors of the conversion." However his explanation is a bit too advanced for newbies who might not understand the meaning of "impedance", "offset", "gain" and integral linearity". I am think of giving a newbie friendly answer, suggesting which resistor range is for which sampling rate etc. His answer refers to MCP3008 datasheet Fig 4.1, 4.2. My newbie friendly answer would also show the pictures, saving newbie's trouble of googling. May 25, 2019 at 8:06

I can't find a clear statement in the MCP3008 spec regarding input current.

Personally I suggest trying 27k, 82k and going up from there if it works.