There are a lot of posts on this and many other forums that go something like "Why am I getting incorrect readings or weird behaviors from the analog input?"
And there are lots of responses that go something like, just use this code. If that code answer amounts to lengthen the "acquisition" step, it might work in certain cases. But in general the problem is electrical, not code, and it is fundamental to the kind of analog input that we have in microcontrollers including the Teensy and other Arduino family boards.
I wrote a longer post with illustrations in spice, you can read it here post on using a SAR adc with spice model illustrations
But, not everybody will click and it is such a recurring issue that maybe it is worthwhile to give a short synopsis here. Maybe some will take a look at the full post.
Lets start with those tutorials on using a thermistor with the analog input. I am not going to shame anyone, but lets say the "advise" often amounts to something like this.
(DON'T DO THIS!, well maybe but only if you also do... we'll take about it a moment)
So what is wrong? in a nutshell, the analog input is a SAR type ADC. The input pin is connected by a switch to a sampling capacitor, of about 30 or 40 pf. That 10k resistor prevents the sampling capacitor from being able to charge up to the input voltage before the end of the sampling window. (The value for the sampling capacitor will usually be in that range, it has to do with certain tradeoffs and the famous kT/C noise.)
This is part of a behavior known as kickback. But it is not just about reading the right voltage. There also can be spikes in whatever you attach to the input. So there are two behaviors, spikes and failure to read the right voltage.
Lets see how it works now in a more specific way.
Here is a SPICE model for the input stage for a SAR. It is a switched capacitor network. The first switch S1 connects the sampling capacitor C1 to the input. That is called the "acquisition" phase. Then S1 opens and switch S2 would normally connect the C1 to the conversion stage. That is called the "conversion" phase. Here S2 grounds the capacitor to clear it for the next clock cycle (we are studying the input for this discussion).
Here is an important detail: The conversion stage sees the voltage on C1 at the end of the acquisition phase. If C1 has not charged up to the input voltage in that time, then the reading is not correct. Finished? Not quite.
Okay, let's connect the thermistor to this input and see what happens.
Oops! See the red curve, that is the voltage at the input. Notice the voltage drop, and it is still low at the end of the sampling window.
Just to be completely clear, here is the voltage on the sampling capacitor. It is like the figure you see in textbooks for charging a capacitor. Anyway, it is obviously not going to work very well when R x C is large or the time is short.
At this point, let's mention three ways to solve this.
The best solution is (a) use a buffer with an RC to act as charge reservoir. Next best, (b) at least use a buffer. And if you insist on direct connection of the divider to the analog input, you might get away with it if you can (c) lengthen the sampling window/
Regarding (c), if you want 1% accuracy, a 1.5usec window might work. Similarly, for 12 bits, then 3 usec. I believe the API for the ADC has a control to set the sampling window, I do not know how long it can be.
Okay lets look at actual solutions. Here is the best. It looks like an RC has been inserted between the opamp and the analog input. But this is not about filtering exactly. The point here is that C2 acts as a charge reservoir for C1. And C2 is replenished through R1. True, R1xC2 is a filter, and you can choose R1 like that, but C2 has to be bigger than C1 and R1 has to be small enough that the opamp is able to replenish C2.
Here is what the current flows look like. Notice the current for C1 comes from C2, and the replenishment of C2 through R1
So that is the best solution. Here is second best, an opamp follower with a small resistor in series. I added a trace for the input pin of the opamp. We see a spike on the output from the R1, that of course means there is a smaller spike on the feedback network and we even see a small spike on IN+. That spike on IN+ is not likely to have much current behind it, so you might get away with it. But there are a few parts that are more sensitive than others. So be mindful if you do it this way. (We should be mindful always anyway)
(One additional comment, that can be very important too: If you are going to do (a) or (b), please do make a soldered board. The parasitic impedance, with solderless breadboards and jumper wires inserted into the current flow for the sampling capacitor, is not helpful at typical sampling rates.)
Okay that is the short version.
Please do find a moment to read the full post if this is something that might help you: post on using an analog input with illustrations in spice
And there are lots of responses that go something like, just use this code. If that code answer amounts to lengthen the "acquisition" step, it might work in certain cases. But in general the problem is electrical, not code, and it is fundamental to the kind of analog input that we have in microcontrollers including the Teensy and other Arduino family boards.
I wrote a longer post with illustrations in spice, you can read it here post on using a SAR adc with spice model illustrations
But, not everybody will click and it is such a recurring issue that maybe it is worthwhile to give a short synopsis here. Maybe some will take a look at the full post.
Lets start with those tutorials on using a thermistor with the analog input. I am not going to shame anyone, but lets say the "advise" often amounts to something like this.
(DON'T DO THIS!, well maybe but only if you also do... we'll take about it a moment)
So what is wrong? in a nutshell, the analog input is a SAR type ADC. The input pin is connected by a switch to a sampling capacitor, of about 30 or 40 pf. That 10k resistor prevents the sampling capacitor from being able to charge up to the input voltage before the end of the sampling window. (The value for the sampling capacitor will usually be in that range, it has to do with certain tradeoffs and the famous kT/C noise.)
This is part of a behavior known as kickback. But it is not just about reading the right voltage. There also can be spikes in whatever you attach to the input. So there are two behaviors, spikes and failure to read the right voltage.
Lets see how it works now in a more specific way.
Here is a SPICE model for the input stage for a SAR. It is a switched capacitor network. The first switch S1 connects the sampling capacitor C1 to the input. That is called the "acquisition" phase. Then S1 opens and switch S2 would normally connect the C1 to the conversion stage. That is called the "conversion" phase. Here S2 grounds the capacitor to clear it for the next clock cycle (we are studying the input for this discussion).
Here is an important detail: The conversion stage sees the voltage on C1 at the end of the acquisition phase. If C1 has not charged up to the input voltage in that time, then the reading is not correct. Finished? Not quite.
Okay, let's connect the thermistor to this input and see what happens.
Oops! See the red curve, that is the voltage at the input. Notice the voltage drop, and it is still low at the end of the sampling window.
Just to be completely clear, here is the voltage on the sampling capacitor. It is like the figure you see in textbooks for charging a capacitor. Anyway, it is obviously not going to work very well when R x C is large or the time is short.
At this point, let's mention three ways to solve this.
The best solution is (a) use a buffer with an RC to act as charge reservoir. Next best, (b) at least use a buffer. And if you insist on direct connection of the divider to the analog input, you might get away with it if you can (c) lengthen the sampling window/
Regarding (c), if you want 1% accuracy, a 1.5usec window might work. Similarly, for 12 bits, then 3 usec. I believe the API for the ADC has a control to set the sampling window, I do not know how long it can be.
Okay lets look at actual solutions. Here is the best. It looks like an RC has been inserted between the opamp and the analog input. But this is not about filtering exactly. The point here is that C2 acts as a charge reservoir for C1. And C2 is replenished through R1. True, R1xC2 is a filter, and you can choose R1 like that, but C2 has to be bigger than C1 and R1 has to be small enough that the opamp is able to replenish C2.
Here is what the current flows look like. Notice the current for C1 comes from C2, and the replenishment of C2 through R1
So that is the best solution. Here is second best, an opamp follower with a small resistor in series. I added a trace for the input pin of the opamp. We see a spike on the output from the R1, that of course means there is a smaller spike on the feedback network and we even see a small spike on IN+. That spike on IN+ is not likely to have much current behind it, so you might get away with it. But there are a few parts that are more sensitive than others. So be mindful if you do it this way. (We should be mindful always anyway)
(One additional comment, that can be very important too: If you are going to do (a) or (b), please do make a soldered board. The parasitic impedance, with solderless breadboards and jumper wires inserted into the current flow for the sampling capacitor, is not helpful at typical sampling rates.)
Okay that is the short version.
Please do find a moment to read the full post if this is something that might help you: post on using an analog input with illustrations in spice
Last edited: