How to filter PulseIn readings

I am looking for a way to filter the signal from the pulseIn function. I want to get rid of both spikes on the falling and rising edge.

I tried using two filters

Exponentially-Weighted Moving Average (EWMA) -> failed to remove the second spike (falling edge)
Debounce -> the value is not updated until a constant speed is reached. In my case it can take up to 30 seconds. Therefore, it is not applicable in my situation.

Board: Atmega2560 (Arduino Mega)
uint32_t pulse = pulseIn(13, LOW, 60000);
Signal that I measure -> from 20Hz (50% duty) to 1100Hz(50% duty)

The pulseIn function just returns a number. How does that "spike"?

Or, are you referring to electrical spikes you see on the input using an oscilloscope?

Have you tried an RC filter?

The problem is not in the electronics. The circuit works just fine. I feed the signal from the PWM generator. On the oscilloscope I can see the correct signal. I take the measurement directly on the microcontroller leg. Not even the slightest ripple.

PulseIn, instead of returning a value of '0' immediately,
returns slowly increasing values

Instead of: 3000 > 0
I have: 3000 > 10000 > 40000 > 0

Of course, the values I've given are only illustrative (different each time)

Can you post your sketch?

Sorry, that output makes no sense to me.

Please post a wiring diagram, the code, using code tags, and examples of the actual output.

What is the "PWM generator"? What is the actual waveform that it produces?

Seems the OP is confusing the A/D functions with the Pulse Length function.

I suspect a wiring problem -- no ground connection, or a floating input.

Strong chance it's just noise on the leg (hard to see with a CRO or DMM) or line capacitance . Just add a small capacitor from the leg to GND, or R+C if needed..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.