I am using a potentiometer with 5% tolerance as a voltage divider in order to send voltage signals to a microcontroller. The mcu reads the voltage and converts it to digital numbers which is then converted to time. The problem that I am having is that when the system resets, the clock changes its reading, I don't really know why this is happening but it happens often.
-
\$\begingroup\$ Perhaps you could share the code so it can be examined for problems? \$\endgroup\$– Anindo GhoshCommented Nov 9, 2012 at 4:45
-
1\$\begingroup\$ Hmm, so I'm trying to parse the problem here. I think you're using the pot to set a time delay of some sort, is that accurate? If this is a microcontroller code problem, please post the code as well as the microcontroller you're using. Also, please define what "readying" means. \$\endgroup\$– SamuelCommented Nov 9, 2012 at 4:46
-
\$\begingroup\$ I don't know what reading means either, that must be a mistake lol. \$\endgroup\$– Andy GraddisCommented Nov 9, 2012 at 6:00
-
\$\begingroup\$ I am using a pic microcontroller, I don't believe there is any issues with the code, I think this is an electrical problem of some sorts. \$\endgroup\$– Andy GraddisCommented Nov 9, 2012 at 6:01
-
\$\begingroup\$ Ok, well, it's difficult to debug a problem without more information. We actually have less at this point, for instance, you've just said you don't know what a key word in your problem statement means. So, what's the problem? \$\endgroup\$– SamuelCommented Nov 9, 2012 at 6:58
2 Answers
I'd say you have a precision error. The microcontroller reads the voltage from the potentiometer with a reference to something, usually Vcc but it can also be referencing ground, or some other internal voltage.
If your voltage changes, the value converted by the ADC will change as well. Voltage changes can come about from many things, including but not limited to ambient temperature changes, power supply load, component temperatures, etc.
If everything is fairly consistent, you may just have a rounding error. For example prior to reset you might get an arbitrary value of 10.4 (for example), but after reset or a second ADC read you get 10.5. The latter would be rounded up to 11 if using integer math.
You might want to modify your code to have fewer or broader "steps" to convert to time, so that input values do not necessarily have to be as precise.
If you just stream an analog to digital conversion of the midpoint of a voltage divider to the serial port, you will almost certainly find that the value reported is not the same every single time. There is always some amount of noise in a system, and that will generally manifest itself as variation in the least significant bits of your ADC reading. In your case you probably only have 12 or 60 voltage steps you need to discriminate (based on the fact that it sounds like you're setting a clock). If you have a 10-bit ADC you can throw away most of the lower bits safely (i.e. shift your ADC reading to the right logically).
Every bit you shift to the right is a division by 2. So if you have a 10-bit value, the biggest value is 2^10 - 1 = 1023. You can divide that by 16 and still have 64 discernible values. If I were you I would just divide by 8 (shift right by 3) and operate on effective readings of 0 to 128. In doing so, you will effectively "filter out" the noisy bits of you readings and you'll get more predictable behavior.
Another approach is averaging a lot of temporally local readings. Accumulate 1000 back-to-back ADC readings into a 32-bit variable and then divide the resulting sum by 1000. Assuming the noise is unbiased, this will do a decent job of removing it because the errors will cancel out on average. I like this approach in general, but in your case I think ditching insignificant bits is a fine idea.