Go Down

Topic: Stabalise analogread ? (Read 1 time) previous topic - next topic

preeves

Can somebody help me with stabilising analogread and a 10k Potentiometer please?

I have pin 1 connected to gnd, pin2 connected to analog1 and pin3 to 5v.
Using a usb powered Duemilanove 328.

Here is the code;
Quote

int potPin = 1;  
int val = 0;  

void setup() {
Serial.begin(9600);
Serial.println("Hello world!");
}

void loop() {
 val = analogRead(potPin);
 Serial.println(val);
 delay(1000);
}


These are the values I see when the pot is at maximum, I was hoping they would be consistent?
1014
1013
1013
1013
1013
1012
1013
1013
1013
1013
1013
1014
1013
1012
1014
1013
1013
1013

Side question, why with the pot at 0, are my values around 18 and not 0?

Thanks,
Paul

retrolefty

Those are pretty consistant readings. If you research the AVR data sheet you will find that they spec the overall accuracy as +/- 2 LSBs. The readings are also subject to any noise on the AVCC pin, used as the internal reference. The AVR analog inputs are very useful and handy to have, but one can't expect lab quality performance with such an inexpensive device.

One can add software 'filtering' to smooth out readings by oversampling and averaging the readings. The 'zero' error your seeing is most likely caused by the pot itself. To prove that just disconnect the wire going to the pot and wire a ground to the analog input and see what that measures.

Lefty

preeves

Thanks Lefty.
When you say oversampling...
Read say 10 values into an array, then average them, then start again?
Or just ignore value changes unless >3 say?

Thanks,
Paul

retrolefty

Quote
When you say oversampling...
Read say 10 values into an array, then average them, then start again?


Yep, thats what I meant. Even adding four readings in a row to a single variable and then divide that variable by four should show a good smoothing function, no need for an array.

Lefty

Spinlock


Go Up