B4R Question PWM Frequency for ESP8266 Accuracy Tested

KiloBravo

Active Member
Licensed User
I saw a thread on the Freq Range of an ESP8266. I wrote a quick program and it looks like the Frequency is set at Compile time. IE you cannot dynamically change it in your code. Anyway I used the esp8266extra libraries. Code Fragment is below. Just a quick Test. I have a decent Tektronix 60 Mhz scope. The scope should be accurate.
What I found was up to 20Khz the PWM Frequency was accurate to negative 2.5% to 5%. (It was never higher than what I set it to)
Depending on the Pulse Width used the Frequency was low by 1% to 5%. It was least accurate in the middle ranges of pulse width (320 to 768).
The most accurate Frequency was when the Pulse Width was at the low and high range <354 or >768, The it was low by 1 % to 2.5%.
Also, The Frequency would not go above 38.8 Khz, no matter what I set it to 40,000, 50,000, or 60,000.
Similar to what I found above, from 20 Khz to 38.8 Khz the PWM Frequency was accurate to negative 5% to 10%. (It was never higher than what I set it to)
Depending on the Pulse Width used the Frequency was low by 2 to 10%. The frequency was also least accurate in the middle ranges of the pulse width (320 to 768).
The most accurate Frequency was when the Pulse Width was at the low and high range <354 or >768, The it was low by 1 % to 2.5%.
Hope this makes sense! ESP8266 at 80 Mhz, Arduino IDE 1.85, B4R 3.30.
Below is some code fragments from the program I used. Hopefully I don't have any cut and paste errors.
This was a quick test while I was mutli-tasking. :cool:

B4X:
    Public p_width As UInt = 0
    Public pwm_freq As UInt = 20000 ' I manually changed this to test and reloaded the program to test it. 
'
    esp.AnalogWriteFreq(pwm_freq)
   pwm_pin.Initialize(dp.D5,pwm_pin.MODE_OUTPUT)
   pwm_pin.DigitalWrite(False)
   pwm_pin.AnalogWrite(0)
'
    For p_width = 64 To 960 Step 64
        Log("PWM Freq= ", pwm_freq, " P Width= ", p_width)
        pwm_pin.AnalogWrite(p_width)
        Delay(5000)
        LedToggle
    Next
 
Top