I'm using a number of analog sensors with the Netduino and I'm running into an issue with accuracy.
To remove the possibility that the sensor isn't stable, I've hooked a trimpot up to the analog input between gnd and 3.3V. I also have the netduino hooked up to a 9V wall wart (from Sparkfun, they say it's very stable, my multimeter agrees but I don't think it samples fast enough to really tell).
My code is incredibly simple:
public class Program { public static void Main() { AnalogInput trimpot = new AnalogInput(Pins.GPIO_PIN_A0); int lastValue = 0; while (true) { int reading = trimpot.Read(); Debug.Print(reading + " (" + (reading- lastValue) + " change)"); lastValue = reading; Thread.Sleep(1000); } } }The output from this code looks like:
232 (232 change) 230 (-2 change) 233 (3 change) 232 (-1 change) 231 (-1 change) 232 (1 change) 231 (-1 change) 234 (3 change) 230 (-4 change) 226 (-4 change) 230 (4 change) 233 (3 change) 231 (-2 change) 231 (0 change)As you can see, a difference between consecutive reads can be 4 or more steps, and this is with a guaranteed constant voltage from the trimpot. The difference between the value pf 234 and 226 2seconds later corresponds to a different of nearly 26mV which I would imagine is way outside of tolerance for a usable ADC.
Am I doing something wrong here? I could fix this by using an external ADC over SPI, but before I go down that route (ordering parts, etc) I'd like to know if I can change something to get better results or whether the external ADC (which is also 10 bits) will have the same issue.
I can also always just do multiple reads and average them, but I'm still a little confused about why they vary so largely.