averaging readings after making sure they are "sensible"

craigcurtin

Senior Member
Guys,

I have a Picaxe 40x1 that i wish to use to display a fuel level in my car.

I have a 10-180ohms sender that i have hooked in and running and can get sensible values back into the picaxe

I have a routine from another project (heating control for my house) that takes a reading once per minute, shuffles out the oldest reading (keep 5 samples in total) and then averages those numbers to produce a crude moving average.

I wish to do something similar with the "Fuel Gauge". i.e.

1) take a reading each minute,
2) check it is sensible (lets say within 5% of the moving average) if so
3) shuffle all the entries down the list by one, drop the oldest and add this new one to slot 1
4) Add them together and then average

I am OK on all of this except for the maths to check if the moving average is within 5% of the value just read - can anyone give me some pointers on how they would tackle the maths side please ?

regards

Craig
 

Andrew Cowan

Senior Member
average = slot1 + slot2+ slot3 + slot4 + slot5 / 5
105percent = average * 105 / 100
95percent = average * 95 / 100

if newvalue > 105percent OR newvalue < 95percent then reject_value

Is that the sort of thing you mean?

Andrew
 

craigcurtin

Senior Member
Why are so many people so much smarter than me !!

Thanks Andrew that was exactly what i meant and looks bloody obvious when expressed like that - thanks for taking the time

regards

Craig
 

westaust55

Moderator
Nothing wrong with the principle Andrew gave but here is another method :

average = slot1 + slot2+ slot3 + slot4 + slot5 / 5

tolerance = average * 5 /100

IF newvalue > average THEN
variance = newvalue - average
ELSE
variance = average - newvalue
ENDIF
If variance > tolerance THEN reject_value

An advantage is that if you want to adjust the tolerance you need only change one value.

A though here is: what when the fuel tank is low?
5% of an almost empty tank is almost zero so the tolerance can be zero and all ensuing readings may be rejected.
Could leave the gauge sitting on say 5% full as it in reality plummets to empty. :eek:
 

Adamey

Senior Member
I think once a minute is way too slow. The fuel level in your tank constantly changes when the vehicle is moving (and changes by a LOT). Vehicle fuel gauges use a "slosh module" (seriously, that's what it's called) to average out this rapidly changing reading and show an accurate reading on the dash.

Mechanical gauges often used a heating element attached to the fuel gauge needle driven by the fuel sender (one of the reasons fuel senders are often low-resistance). The current through this element would change rapidly as the fuel sloshed around, but the element couldn't rapidly change temperature so the current ended up getting "averaged" over time and the element temperature remained stable (which also made the needle stable).

Later gauges used a capacitor/resistor divider to accomplish a similar result. There are other electronic circuits as well, usually integrated into the cluster itself, but they all serve the same purpose - to average out the rapidly fulctuating voltage from the sender.


I think I would take a reading every 0.5 seconds for 30 seconds and average the 60 readings, then display this as the actual fuel level. No need to keep track of the previous fuel level as the 60 readings should give a fairly accurate result.

or

You could try a capacitor/resistor to average the voltage for you. :)
 

Dippy

Moderator
I'd also go along with a fat RC to act as a reservoir.
The RC effect would also remove any nasty noise picked up on the line.
 

John West

Senior Member
Why are so many people so much smarter than me !!
I sure don't begrudge them their smarts. I'm just glad they're here to help us, Craig. I wish everyone was smarter than me - more people to ask questions of.

Good common-sense software and hardware answers to technical questions.

I sure like this place.
 
Last edited:

craigcurtin

Senior Member
thanks for the additional feedback

Guys,

What would the Resistor Capacitor circuit look like for a newbie ?

Currently i have a regulated 5v supply for the Picaxe that i was going to use to drive the 10-180 ohm sender - the sender will be earthed on its 2nd terminal to the same earth as the picaxe.

The 5v to the sender will come back to the picaxe ADC pin through a 100 ohm resistor.

Would i just add a capacitor in series with the 100 ohm resistor and then to the ADC input ? if so what size Cap should i use ? (i am still at the basic level with the physical electronics - so baby words please !!)

regards

Craig
 

Andrew Cowan

Senior Member
Time period = R * C

Now, actually that time period is the time taken for the system to charge up to 37% of it's final value, but we can use the equation to get a feel of the right order of magnitude.

Say you want t to equal 1 second - with a 100 ohm resistor, that means a 10,000uF capacitor. Pretty big!

With a 10K resistor, a 100uF capacitor would give the same time period - much more reasonable.

The resistor is in series - the capacitor is in parallel:
Code:
----===------------- PICAXE
    10K     |
            |
            _
            _ 100uF
            |
            |
            |
           GND
Andrew

Edit - see Dippy's corrections on the next page
 
Last edited:

Dippy

Moderator
"time period is the time taken for the system to charge up to 37% of it's final value..."
Andrew , don't get your "time periods" confused with "time constants".

I think what you've said may (I repeat "may") confuse if he starts searching.

An capacitor current charge curve is exponential. (I'm sure you can find examples on nerdynet).
It starts off sharply when the capacitor is flat.
Then as we charge it up the actual current going in gets less.
As we head towards full charge the current is small.

Whilst the curves for different charging will be different in amplitude , the general shape remains the same. Thus we know that one RC Time constant period gives 63ish% of full charge. And we know that 5x RC Time constants will be 99% full charge.

Anyway, the rest is basically as Andrew says.

The only other thing is the 'absorbing' and decoupling effect of a capacitor.
The wire going to your PICAXE circuit is susceptible to electrical noise.
And the sensor/sender ciruit may also be electrically noisy.
A capacitor fitted across the line-gnd (as indicated by Andrew) can 'filter' noise and act as a reservoir for any LF transients.



These little things can often MUGGER UP your ADCing. Loads of averaging acts like a digital filter, but hey , why not catch it BEFORE it pops into your PICAXE. A 10p res-cap combo could save a lot of hassle.


But, bottom line, as your fuel sender voltage changes will be so slow I would be tempted to use quite a fat cap. I would probably //L it with a 47nF ceramic cap to act as a HF filter.
Slow/small changes will be quick, but big donkey ones will be slow. Ideal.

The RC section should be CLOSE to your PICAXE , not close to your sender. Keep noise out of wires.
 
Last edited:

craigcurtin

Senior Member
Thanks Andrew and Hippy once more

But, bottom line, as your fuel sender voltage changes will be so slow I would be tempted to use quite a fat cap. I would probably //L it with a 47nF ceramic cap to act as a HF filter.
Slow/small changes will be quick, but big donkey ones will be slow. Ideal.

The RC section should be CLOSE to your PICAXE , not close to your sender. Keep noise out of wires.
Dippy, What does //L (parallel ?? I guess?) mean

So is the 47nF cap instead/replacement for Andrews suggested size ?

What does this do to my readings ? i.e. how do i know when to read so that i get a consistent state from the CAP ? what happens if one reading is just before it hits a full charge and one just after a discharge ?

To cater for this type of condition do i just use Andrews averaging code ?

Craig

Craig
 
Last edited:

Dippy

Moderator
sorry, //D = "paralled with". Me lazy.

Yes, I rhyme with hippy ;)


Take a look at this.
http://www.tpub.com/neets/book2/3d.htm

That shows a capacitor charge / dicharge from 0% to 99.whatever% and back to 0%.
i.e. connected like Andrew's sketch on the previous page.
So, with a flat capacitor the voltage is applied....up to 99.whatever% and then allowed to discharge back through the resistor to ground.
Fill it up and then empty it.

Your fuel tank sender will change in current/voltage much more slowly.
The fastest it'll ever change is when you are at the petrol station or if someone cuts your fuel line.

Result.
The exponential charge/dicharge is uperimposed on top of the changes from your sender.
Hang on , here's a crappy drawing.... see below.


The graphs show the sender voltage decreasing as fuel level goes down.
I am ASSUMING that the sender is a traditional pot-on-a-stick-float type thing???? (If not then Plan B may be required)

The 'raw' voltage will have longish and shortish transients (sloshing, cornering, electrical noise etc.)
The RC provides 2 main things.
The fat electrolytic cap (220u/25V) provides a reservoir 'smoothing' out those longer transients.
The little (ceramic) cap decouples noise.
Result: smoother signal and a degree of protection to your PICAXE input.
Values are just a guess .. I mean 'guide'. YOU need to experiment and measure and test.

There is one other variable; absolute voltage.
If your sender output is up to 12V then you will have to include a potential divider at the front end to prevent ADC >V+ PICAXE.
And PICAXE decouplig of power pins is a definite requirement.

When do you ADC?
Timing unimportant as the hardware has done the averaging for you in effect.
If you get your time-constants optimal then an average of a couple will do.
PLUS it protects against noise too. A bonus ball.

Without filtering, you will need to do a lot of sampling.
BUT you should still have transient protection to provide robustness (and peace of mind!).

Sorry for crappy picture... I've got to mow the lawn.
 

Attachments

craigcurtin

Senior Member
Thanks Dippy

Yes the sender is a standard float type.

I plan on driving it at 5V (have been assured by the supplier this will be fine) - therefore (i asume) this is Picaxe ADC friendly and should not fry my input ?

I think i understand now and will put something together to try.

Luckily this will never be filled at the Service station - make all my own BioDiesel and Vegoil at home !!!

Craig

P.S. Good luck with the lawn !!
 

Dippy

Moderator
Lawn 50% done. Got bored. Need coffee.

Yes, have a go.
Basically it's just smoothing a wobbly signal into a smoother one AND also shunting any nasty spiky noise away.
Your PICAXE ADC will be much happier.

It's a commonly used method for ADCing where you suspect wobbly stuff.
Even with 'scope software ADCing you would add an input filter (often active) to prevent aliasing.

I would be tempted to use a variation of this method for any ADC measurements. And , in fact, a decoupling RC filter for digital inputs too where the input is coming from somewhere noise or where noise can be induced onto the line. It can prevent your PIC peripheral getting zapped.
Horses for courses.


If you can ever get hold of a nice 'scope take some time to play with filtering - once you have a feel for these things you can work out where all this stuff is useful.
Many people here think that automotive electronics are some kind of magic - they're not. But they DO require thought and care and (boo hiss) calculation, testing and the knowledge of component behaviour.

Good luck. You'll be fine. :)
 

craigcurtin

Senior Member
You must have a short attention span !!

I heard lawns in England were not that big !! How did you only get 1/2 way and stop ?

Thanks for the help - got the parts today and will try and breadboard it tomorrow

Craig
 

MartinM57

Moderator
Definitely need to experiment...

5600R and 220U is a RC time constant of 1.232 seconds - so assuming the pot-on-a-stick reacts pretty quickly, you will see cornering/braking effects if the fuel level at the pot changes under these conditions.

Bigger RC? Maybe 10K/470uF? - still "only" 4.7 seconds. Possibly something in the order of 1 minute is what you need?
Still do some PICAXE averaging?

Report back...
 

hippy

Technical Support
Staff member
As well as averaging ( hardware or software ) there's also 'slugging' or 'slewing', which would slow the result change and give a smoother reading. As the input changes the displayed value changes towards what that it is at some rate. If you get a sharp blip on the input the display will rise/fall slightly but if the blip is soon gone then the display will quickly go back to what it was.

Probably very similar to RC damping or averaging, just another way to do things. The advantage is that it's usually very easy to implement in software, similar to -

Do
ReadAdc pin, b0
Select Case b0
Case > b1 : inc b1
Case < b1 : dec b1
End Select
SerTxd( #b1 )
Pause 100
Loop
 

mh108

New Member
If the vehicle already has X volts (maybe 12?) connected to one side of the pot-on-a-stick, then would it not save some cable length and psu effort to just "tap into" the existing connection ?

One assumes that the power source the vehicle uses for measuring the level is well regulated, and that this would not "mess up" the readings the car gets :eek: on its own sensor display... Oh, and that you only wanted to measure the level with the ignition on...

Some kind of resistor divider could drop the 12v to 5v at the picaxe adc port (after the above cap filter), with a bonus advantage that the higher sensor voltage might also further suppress noise??

Just another angle... although between the lines it seems you have a good reason to run your solution disconnected..
 

craigcurtin

Senior Member
Definitely need to experiment...

5600R and 220U is a RC time constant of 1.232 seconds - so assuming the pot-on-a-stick reacts pretty quickly, you will see cornering/braking effects if the fuel level at the pot changes under these conditions.

Bigger RC? Maybe 10K/470uF? - still "only" 4.7 seconds. Possibly something in the order of 1 minute is what you need?
Still do some PICAXE averaging?

Report back...
OK starting to lose me here guys.

i will start with Dippys values and see how we go, will also do some ongoing averaging within the picaxe also

Can you explain what you mean by a time constant of 1.232 seconds ? How does this affect me taking measurements ?

regards

Craig
 

Andrew Cowan

Senior Member
That means short changes (such as a 200mS blip) won't affect the ADC much, but a larger change (such as driving round a corner for 10 seconds) would affect the reading.

The time constant shows the order of magnitude of the response rate - for a fuel tank that will take a couple of hours to decrease, a time constant of 60 seconds or so would not cause any error in readings.

A
 

Adamey

Senior Member
Another thing I forgot about is when you first start your car. You don't want to wait 60 seconds to get a reading. However, since it can be assumed the car was sitting still the value of the gauge should be pretty accurate.

So on initial power up of your circuit, whatever the initial reading is you display as your fuel level, then proceed to do the averaging over time.
 
Top