PWM calculation

Configurations and calculation tools

PWM configurations (JSON)

Download optimized PWM settings as JSON structure

Please note that this list does not contain all possible configurations!
You can use the snippet below to search configs in your target frequency range.

Calculation snippet (Python)

def pwm_calc(period_hz):
    BASE_CLK_HZ = 48 * 1000 * 1000
    duty_cycle = 0.5

    #print("Searching for: " + str(period_hz) + "Hz")

    found = False
    for prescaler in range(1, 0xffff, 1):
        tick_time_hz = BASE_CLK_HZ / (prescaler + 1)
        #print("Prescaled base frequency " + str(tick_time_hz) + "Hz")
        if tick_time_hz < period_hz:  # Return if prescaled tickrate is lower than the period
            return (-1, None, None, None)

        if tick_time_hz % 2 == 0:  # Ignore uneven numbers
            for t_period_cnt in range(1, 0xffff, 1):
                t_period = (1 / (t_period_cnt / tick_time_hz))

                if t_period == period_hz:
                    t_on = t_period_cnt * duty_cycle
                    print('Found ' + 'prescaler:' + str(prescaler) + ' t_period:' + str(t_period_cnt) + ' t_on: ' + str(
                        t_on)
                          + ' for frequency (Hz) ' + str(t_period))
                    return period_hz, prescaler, t_period_cnt, t_on

# Select start and stop frequency in Hz. You can also specify the test step.
for i in range(1000,100000,100):
   pwm_calc(i)