Filter, Averaging or any other option?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
RainySun
 
Posts: 73
Joined: Thu Nov 18, 2021 2:55 pm

Filter, Averaging or any other option?

Post by RainySun »

Hey, I currently using analog input pin A1 to measure voltage, and what I have noticed is that voltage reading tends to fluctuate within 0.2V or sometimes 0.4V at the beginning. Below is a simple code that I used, and I would like to know a way to reduce fluctuation. I have thought of using averaging or filter but I would like to know if there are methods that I can use that would take less time or does not require sampling. I hope to hear your response.

Code: Select all

import time
import board
from analogio import AnalogIn
analog_in = AnalogIn(board.A1)

def get_voltage(pin):
    return (pin.value * 3.3) / 65536

while True:
    print((get_voltage(analog_in),))
    time.sleep(0.05)
Attachments
Analog.PNG
Analog.PNG (25.32 KiB) Viewed 160 times

User avatar
tannewt
 
Posts: 3298
Joined: Thu Oct 06, 2016 8:48 pm

Re: Filter, Averaging or any other option?

Post by tannewt »

What board are you using? The AnalogIn implementation varies per-microcontroller.

User avatar
RainySun
 
Posts: 73
Joined: Thu Nov 18, 2021 2:55 pm

Re: Filter, Averaging or any other option?

Post by RainySun »

I am using ItsyBitsy nRF52840.
https://www.adafruit.com/product/4481

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: Filter, Averaging or any other option?

Post by danhalbert »

Are you measuring a high-impedance analog voltage? It can take a bit of time to charge the capacitance in the pin circuitry if not much current is available.

On many microcontrollers, it can be a good idea to throw away the first few analog reads. The ADC's can take time to stabilize. I don't know that this is a problem with the nRF52840 in particular, but if it solves your problem, go ahead. You may not need to do any averaging once it stabilizes.

User avatar
RainySun
 
Posts: 73
Joined: Thu Nov 18, 2021 2:55 pm

Re: Filter, Averaging or any other option?

Post by RainySun »

I am measuring directly from a DC power supply.
I think I will do as you have mentioned. Trash the first few data, and average the rest.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”