|
Post by pt3r on Jan 30, 2021 15:39:07 GMT
Hi, I am in the process prototypin an AE module based on an Arduino pro micro and I have connected the wiper pin of a potentiometer to an analog pin of the arduino, and one of the outer pins of the potentiometer to ground and the other to Vcc. When I check the read value of analogRead() I notice that the value fluctuates the whole time even if I don't twist the potmeter's shaft. Is this normal or did I build my circuit to simplistic or is this to be solved in software through some kind of EMA.
|
|
|
Post by MikMo on Jan 30, 2021 16:04:05 GMT
It is normal the value returned by analogRead() fluctuates up and down by 1-2.
I don't think that this can be dealt with in hardware, so you need your Arduino code to handle it.
Hysteresis i think it is called.
Mikael
|
|
|
Post by MikMo on Jan 30, 2021 16:09:52 GMT
|
|
|
Post by pt3r on Jan 30, 2021 16:38:35 GMT
Thanks a lot, this solved my issue.
I kept looking in arduino fora and tutorials but this fluctuation did not really get mention or at least nobody seemed to care about it to see it as a problem.
|
|
|
Post by solipsistnation on Jan 31, 2021 6:35:45 GMT
You can also just >> and << it and ditch the least significant bit. 8)
Yep, it's called hysteresis.
|
|
|
Post by robertlanger on Jan 31, 2021 8:48:23 GMT
You can also just >> and << it and ditch the least significant bit. 8)
Yep, it's called hysteresis.
This might help a little bit, but is not a real hysteresis: Think the current lowest two bits are "1" and the new ADC value increases by 1, then the third bit jumps, and cutting the lowest two bits has no effect, except for getting a jump of +4 in the result - not really what we want ;-) A real hysteresis works like this: - create a buffer variable - calculate the absolute difference of the stored value to the newly read ADC value - If the difference to the buffer variable is greater than x (the hysteresis) then update the buffer and use this value, otherwise discard the ADC value. Use signed ints for this. Code snippet: #define HYSTERESIS 4 int16_t buffer; int16_t adcvalue; //in the loop: adcvalue = analogRead(0); if (abs(adcvalue - buffer) > HYSTERESIS) buffer = adcvalue; No warranty for the code, written on the smartphone and laying on the couch ;-)
|
|
|
Post by zaphodb on Feb 6, 2021 9:58:58 GMT
To avoid jitter you can use the ResponsiveAnalogRead library. This implements the hysteresis or averaging computation methods suggested by others here. In stead of reading the analog input directly this is done via a small detour to a ResponsiveAnalogRead object that will smoothen the input values. The amount of smoothing can be set using a parameter. This works very well!
Have a look at the example code of the library and your problems will be solved soon.
|
|