Monitoring PWM- best way for newbie?

I have tried to do my own homework before posting, but can't find anything simple enough for me to follow.

Is there an easy way to read a PWM signal (0-5vdc) and integrate the total on-time? What I would like to do is make a gauge readout showing how fast fuel is being consumed by an engine. I have the output display under control, and I think I can manage the conversion once I get the input coded, but I need some help figuring out how best to get my data.

Is Pulsein the method I want to use? Or is there a better way? I am only concerned with steady-state data, so I don't need latency to be super low. I could sample intervals, say twice per second, average the data over the interval, then update the display and that would be plenty. The frequency will vary of course, but really what I need for data is the "area under the curve" over time.

If I can easily get a frequency reading from the same pin that's giving me the on-time data, then great- but it's not really a priority.

Appreciate any advice on this

What sort of frequency are we talking about

Less than 100 Hz.

I have some stuff there about measuring frequencies either by counting over time (you decide the time) or measuring the period.

Not sure this will help, but isn't "reading" a PWM signal the same as measuring the average voltage? If so, just read the PWM voltage. For a square wave, measuring the area under the curve is trivial. It's just average voltage * time.

If I understand your question correctly, you could measure the voltage at regular intervals and multiply by the interval (in seconds) to get a number proportional to the fuel consumed during the interval. Frequency of the PWM does not matter because you're reading the average.

Javaman- I'm not sure. How does one read PWM voltage? DigitalRead I thought just took a snapshot of the present voltage- which, if true, won't work. PulseIn gives the pulsewidth, but the reference page says it's good for 10ms and up- also, it doesn't give any significance to the frequency so I can't get a time integral out of it.

Or would I need to build an RC circuit to filter the signal, then do an AnalogRead? Seems kludgy to me, but what do I know? Is that a reasonable approach?

Nick- Thank you, I think your page is probably the real answer to my question- unfortunately I am not familiar enough with programming to follow most of it. I was only anticipating this toy needing about 20 lines of code, maybe I should find something smaller to start with?

Hm...not being an electronics expert, I assumed there was an easy way to get average voltage through some kind of component or filter. If not, you could try averaging it like this:

// measure PWM voltage (duty cycle between 0.0 and 1.0) // connect a jumper from digital pin 3 to analog pin 2 // start serial monitor to see average voltage

float average, alpha=.99, newValue, oldValue=0.0, duty=.8;

void setup() { pinMode(3, OUTPUT); Serial.begin(9600); }

void loop() { analogWrite(3, 255*duty); newValue = analogRead(2); average = oldValue*alpha + (1-alpha)*newValue; Serial.println(average); oldValue = average; delay(50); }

Play around with this - all you need is one wire. Try it for different duty cycles. The closer to 1.0 you make alpha, the better the average but the slower the response time. This shouldn't be a problem since you are only concerned with steady-state data.