Fast debounce needed

Hi, I have input from an anemometer head to a Teensy 4.0, it's a squarewave ranging in frequency from a few Hz up to about 30 Hz. Checking the switching on a scope, there is debounce apparent, but it's all inside a 15 microsecond window. Using the usual debounce tools available, with the timer set to a number of milliseconds, would certainly skew the results at higher wind speeds. What I need is a debouncer that will deal with the transients and keep time skew down to around 50 microseconds. I'm hoping that the speed of the CPU will be in my favour.

Appreciate any suggestions, thanks in advance.

My loop function will be close to this.......

void loop() {
bool currentState = digitalRead(inputPin);
if (currentState != lastState && currentState == HIGH) {
unsigned long currentTime = micros();
period = currentTime - lastTime;
Dperiod = (double) period ;
Hz = 1000000.0 / Dperiod ;
Speed = Hz * 4.0 ;
lastTime = currentTime;
Serial.print("Speed: ");
Serial.print(Speed);
}
lastState = currentState;
}
 
You could add something like that to your if-statement to detect a new pulse only after your defined time window:

Code:
if (.... && (micros()-lastTime > 15))
 
Change your code to detect both rising and falling transitions, and their timestamp. Do any processing needed, then wait until micros() - currentTime > 15. Should be fine as long as your signal duty cycle is about 50% and processing takes under 16ms
 
Thanks to all, good suggestions that I'll keep up my sleeve.

After a bit of playing round, came up with a pulse shaping circuit that it looks like will work. A 150n capacitor from the sense input to Vcc, and a 6K8 resistor from the input to 0v. The switch and a diode in series go between Vcc and the input, when the switch closes it immediately charges the cap up to 3volts, and when it opens the pin voltage decays slowly enough to keep the the bounce transients well away from Vlow. I only need a fast edge in one direction. Haven't run with software but on the scope it looks plenty clean enough.

The trick was to switch from the high side, to avoid possible issues with the diode forward voltage vs Vlow(max). I’m assuming that Teensy inputs aren’t Schmitt (haven’t got the energy to tackle the vast Teensy Chip level doco).
 
Another option could be a HW Timer + Input Capture, which often already have glitch filters built in.
Chapter 56 in the https://www.pjrc.com/teensy/IMXRT1060RM_rev3_annotations.pdf
The quadrature decoder logic can be bypassed (bit 9 in the CTRL register). It has a configurable glitch filter. After cleaning the edges it could fire an interrupt to start/stop another HW timer and precisely measure the frequency.
 
This is my favourite approach (parts 1 & 2):
 
Hi, I have input from an anemometer head to a Teensy 4.0, it's a squarewave ranging in frequency from a few Hz up to about 30 Hz. Checking the switching on a scope, there is debounce apparent, but it's all inside a 15 microsecond window.
Simply poll the input at 1kHz or so, that'll hide any bouncing on a 15µs timescale completely, but be fast enough to handle 30Hz with ease.

I think the scope showed bounce, not debounce(!)
 
Back
Top