Electronic – How to stablize an analog to digital reading

adcanalogmicrocontrollerpotentiometerstability

I've built a clock out of a PIC16F877, some LEDs and other stuff…

To set the time in 5 minute intervals, I'm using a 22K ohm potentiometer connected between GND and +5V with the contact connected to analog input RA0 on my MCU.

It works, but it badly fluctuates between values. It's a 10bit ADC, and I'm looking at the most significant 8bits, and if I get my pot roughly in the center, I get an average reading of 128, but it flickers between 127, 128 and 129… even if I leave it alone and don't touch it.

How can I stablise the reading? I could probably write firmware to take an average reading, which may help a bit, but before I mess around with that wondered if there is something I could add (caps, resistors, etc) to my circuit to make it more stable? In this part of the circuit, I just have the pot connected to the MCU, nothing else.

Best Answer

You have noise in your system, plain and simple. How you handle it is the same ways that you handle any other noise: remove the source of the noise, or filter out the noise. Some things to consider are:

  • Long cables can cause noise. Shorten them, or use shielded cables
  • Use differential measurements, if possible.
  • Make sure that you use good power distribution, routing, and filtering.
  • Make sure that your PCB is designed to reduce noise on the important signals.
  • Placing a cap on your ADC input pin, between the pin and GND, can help reduce noise from a pot. This C, plus the R that is your pot, form an RC low pass filter. The larger the cap, the better the filtering (but the slower the system will respond to changes of the input). I would start with a 0.1 uF cap, and go up from there.
  • Use a lower-value POT. 10K, 5K, or even 1K ohm. This effectively decreases the output impedance of your sensor (the pot) and reduces the chances of picking up noise. Make sure you do not exceed the power limits on the pot.
  • Filter the signal in software. It could be as simple as having a moving average, or you could do a more complex form of filter. The longer time that you average the signal, the better effective resolution you will get, with the obvious down-sides.
  • Make sure that you are operating the ADC in the MCU correctly.
Related Topic