PWM Causing Jitter - Teensy 4.1

Thundercat

Well-known member
Hi all,

I've been wracking my brain and spent untold hours trying to figure out the source of noise in a project I'm doing. It's a MIDI fader, and I've got an LCD screen and some buttons with RGB LEDs that I'm changing colors using PWM pins.

What happens is, randomly, the unit outputs MIDI values when the faders are not moving. About 10-20 events per hour. Not a deal breaker, but this is a product I sell, so absolutely not acceptable!

I'm using the ResponsiveAnalogRead library of course. And all fader pins have 150nf capacitors on them just prior to input pins. Plus careful detail to isolating the fader circuit from the rest of the unit with dedicated ground and power pins, plus ground plane and power plane - everything I could think to make this quiet.

But it's not quiet!

After countless hours, remaking the PCB multiple times, nothing was working, so I decided to do a "burn it to the ground" test. I made a copy of the code, then ripped out absolutely every routine one by one until I found what was causing the noise.

Well guess what? Using the PWM pins to change the buttons colors is causing the jitter! I know this 100% because without changing the color of the buttons using PWM, there is absolutely no noise whatsoever (no extraneous MIDI outputs, no jitter from the faders at all).

So this leads me to my question: what can I do about this?

Is it as simple as hanging some capacitance on each LED, from power to ground? If so, how much is recommended?

Or is there another way to handle this? I really don't want to make another PCB board, so a code solution would be great.

Note: I've already tried changing the PWM frequency to much lower, or much higher, but this did not seem to fix anything. Still noise.

Thank you so much for your insights.

Mike
 
Firstly you cannot expect analog inputs to stay rock steady, if the voltage falls close to an ADC step it can jump back and forth by one count, this is a fundamental property of digitizing any signal. So normally hysteresis is added in the code to stop this sending unnecessary signal onwards, typically 1 or 2 ADC counts of hysteresis will be enough if the analog wiring is well shielded from noise, but not having hysteresis will bite you in this scenario. The responsive analog read library claims to automatically hide noise, using some sort of hysteresis, but clearly its not quite good enough for you situation and you'll need to add hysteresis on the output of the library I think.

Secondly in electronics the term jitter is reserved for time variation only, your faders simply have noise, not jitter.

The noise is likely coming through the supply, because changing the PWM for the LEDs will change the average load on the supply as well as injecting the PWM frequency onto the supply rail too. No matter how much capacitance you add to the fader wipers its not going make any difference as the ADC is ratiometric from the supply voltage - adding more large decoupling caps on the supply might help, especially if there's enough to attenuate the PWM frequency.
 
Firstly you cannot expect analog inputs to stay rock steady, if the voltage falls close to an ADC step it can jump back and forth by one count, this is a fundamental property of digitizing any signal. So normally hysteresis is added in the code to stop this sending unnecessary signal onwards, typically 1 or 2 ADC counts of hysteresis will be enough if the analog wiring is well shielded from noise, but not having hysteresis will bite you in this scenario. The responsive analog read library claims to automatically hide noise, using some sort of hysteresis, but clearly its not quite good enough for you situation and you'll need to add hysteresis on the output of the library I think.

Secondly in electronics the term jitter is reserved for time variation only, your faders simply have noise, not jitter.

The noise is likely coming through the supply, because changing the PWM for the LEDs will change the average load on the supply as well as injecting the PWM frequency onto the supply rail too. No matter how much capacitance you add to the fader wipers its not going make any difference as the ADC is ratiometric from the supply voltage - adding more large decoupling caps on the supply might help, especially if there's enough to attenuate the PWM frequency.
Hi Mark, thank you so much. I appreciate your time to share your expertise!

I am using hysteresis already - I should have mentioned it. I'm not only using the RAR library, but also added checking if the counts go over 3 or 4 absolute value in the raw data, and if so, then allowing it to bit shift down to the needed values. In fact if I go any higher on the hysteresis threshold it starts to miss counts at the end, so I'm at the limit of what I can do with software smoothing or hardware smoothing via caps. I could bypass the RAR lib altogether and roll my own averaging routine, but I don't think is the answer. I think the RAR lib is pretty amazing, but it does take some getting used to, and experimentation.

Interestingly enough, when testing this exact hardware with just the RAR library and no PWM output at all on a totally stripped down example, it's complete rock solid, with NO hysteresis whatsoever and 0ms on the loop (I usually add a delay of at least 1ms up to 10ms for stability, but it wasn't needed on a simple test). Crazy rock solid - no unwanted output from the faders unless the faders are moved. So that was the huge red flag that, wait a minute, it isn't the faders that need attention - they already have the RAR lib going for them, and also capacitance at each input pin.

But still, SOMETHING was causing the jitter!

That's when I decided to do elimination testing, ripping out everything in the code that wasn't absolutely necessary, and discovered to my shock that the PWM output is what is causing the jitter. But I'm still new at all of this - it sounds like this is expected or obvious, so thank you for that heads up.

I'm already using 44uF capacitance on the supply rails, and 44uF on the fader supply rails as well, so I can't imagine I'd need more than that, do you think?

Would adding any capacitance to each PWM output pin would be useful? Or do you think the jitter is internal to the Teensy when using the PWM output?

With additional testing tonight, it appears that lowering the PWM frequency by a lot actually helps; I'm trying 2000 at the moment and it seems almost to have solve the issue (but not 100%). I don't know if there's any downside to using a very low PWM frequency; it's not visible or distracting. But again, it's not 100% solved, just better.

I'd love to hear any additional insights you or anyone else may have.

Thank you again, and all the best,

Mike
 
Back
Top