I want to measure current using sensor MLX91220 which gives output in analog. I have battery that I charge and discharge via switching MOSFET and want to measure current on this grid.

Problem is that it gives stable output when MOSFET’s gate PWM is either at 0% duty cycle or 100% but fluctuate a lot in middle range.

I found similar problem related to this. Suggestions were to use low pass filter and cutoff frequency depends on the PWM frequency and microcontroller’s ADC’s sample rate but I have no idea how to select from that. Also my duty cycle is changing during this period, so does that affect choosing frequency ?

I have seen current readings from in built BMS, those are very stable, what (or which method) is used in that ?