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?
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:
- The analog voltage to be converted (0V to 4.2V) charges the sample capacitor (20pf), in 1.5 clock cycles.
- 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.
- 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.
- 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.
- It needs newbie scary mathematics (calculus, differential equations!) to calculate/analyse the appropriate range of resistors for different sample rate ranges
- 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.
- 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.
- 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, … 🙂
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, … 🙂
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.
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.