Averaging a ADC input within a flowchart

TerryRy

Member
I seem to reached my personal limit and that of working with 8 bit integers within PICAXE Editor 6.

My problem is that when I read an ADC input (adc_C.1), I get +/- 3 jitter on the value read into the variable (varV). Heavy lowpass filtering 10k/10u has little impact. Layout is relatively good with lots of bypasses and a dedicated 3v3 regulator for the 20M2.

I could use 10 variables to average over 10 ADC samples but I don't have them free to use as they have other functions in the overall program. Can anyone show me how I could use a BASIC command sequence to achieve this without interfering with the operation of the rest of the flowchart function?

cheers
 

JimPerry

Senior Member
Zero a word variable - then read ADC 10 times (adding to word variable) then divide word by 10 as an integer?:confused:
 

PieM

Senior Member
@ TerryRy

Code:
symbol varV= W1
symbol average = W2
symbol coef = b5
symbol adc_C.1 = C.1

'init
coef = 4 'for instance: (more for smoothing)
readadc adc_C.1, average ' init average

'==================================
main:
do
	read adc_C.1, varV
	average = coef -1 * average + varV / coef

loop
Lissage.jpg
 

Goeytex

Senior Member
Jitter & offset can many times be related to grounding ( especially on a breadboard) and/or to a high impedance input.

Generally speaking the input impedance should be no more than 20K Ohms. So if using a potentiometer, a 10K works well.

The common or ground for the ADC input device should be as close as possible to the Picaxe ground pin, or have a trace/wire that goes to the same point at the Picaxe ground pin. For example, using a remote pot with three wires: Connect the V+ and 0V wires as near the Picaxe + V and 0V pins as possible, instead of to the breadboard power on the other side of the breadboard. This reduces the resistance that can cause offset and stray capacitance that can cause jitter.
 

Dippy

Moderator
I'd go along with Goeytex's advice.
And if you can't get it really good then use an averaging method as suggested by JimPerry.
You'll never get 100% zero jitter. You should be able to get better than 6.

Also note that "lots of bypasses" doesn't always mean 'suitable' bypasses. Ditto "heavy lowpass filtering".
You need to choose component values and types suitable to the task and positioned in the right place.
Are your decoupling/bypass components of adequate quality and specification?
Are they placed in an optimum position?
Is your signal source pure?
Is this a hairy breadboard or a nice tight PCB?
Can any external noise sneak in?
Is you primary supply battery, high-Qu bench linear PSU or Switched Mode?

Having input filter values as you have suggested may be inappropriate depending on your signal source and sample rate.

You don't need 10 variables for an average.
You can simply do a compound (variable=variable+ADC) in a loop and divide by whatever ; only one word variable.(Repeating Jim's suggestion).


A schematic and photo may help the readers to provide a good suggestion.
Pls include component values and types.
 

TerryRy

Member
Thank you every one for the very useful comments. I have decided to implement the rolling average method suggested using two variables. This should work well although I haven't tried it yet. Response time for the measurements is not important as the parameter being measured is soil moisture and a second of so for the reading to settle would be an advantage.

I have decided to have a closer look at the primary cause of the jitter in the first place before I hide it with software. Although I have a low pass filter (10k and 10u to ground) I will also place a 100n directly across each of the relevant PICAXE pins.

For interest I have a attached a circuit drawing and photo of the mock-up which is a proof of concept for an interface between analog soil moisture sensors and a cloud-based HydraWise irrigation controller.

Thanks again for your support.

cheersView attachment 17057breadboard photo.jpg
 

Attachments

Last edited:
Top