logo elektroda
logo elektroda
X
logo elektroda

PWM Groups (synchronized PWMs) driver for OpenBeken - BK7231N - with dead time

p.kaczmarek2 2127 5
ADVERTISEMENT
  • Oscilloscope display showing PWM waveforms on a RIGOL screen.
    In the 1.17.388 version, OpenBeken introduced a PWM Groups driver. This driver enables the synchronization of two PWMs and facilitates setting a specified dead time between them. Of course, the duty cycles of each PWM groups are independently controlled and the frequency is also adjustable.


    Tuya's (Beken?) implementation
    The PWM groups driver is present in the used BK7231N SDK, but after few quick tests it's clear that's it not working correctly:
    Code: C / C++
    Log in, to see the code

    There are multiple issues with this code:
    - the second duty cycle is not used at all and both PWMs are using duty_cycle1
    - the frequency variable is not in fact a frequency, but rather a period
    - the following implementation breaks when a 0 duty_cycle is given


    My improvements
    I've performed some tests with the original version of the code, also by manually setting values for each PWM:
    Code: C / C++
    Log in, to see the code

    Result:
    Oscilloscope displaying PWM signal waveforms.

    Test code:
    Code: C / C++
    Log in, to see the code

    results:
    Oscilloscope screen displaying two synchronized PWM signals with dead time.

    Test code:
    Code: C / C++
    Log in, to see the code

    Result:
    View of Rigol oscilloscope screen with PWM signals

    Test code:
    Code: C / C++
    Log in, to see the code

    Result:
    Oscilloscope display showing PWM waveforms on a RIGOL screen.
    It seems that duty_cycle1, duty_cycle2 and duty_cycle3 describes when a PWM toggles self state. Based on that, I proposed the following implementation:
    Code: C / C++
    Log in, to see the code

    The code above still uses the incorrect name for period variable (it's called frequency), but I also resolved it.
    The tests have shown that the period and duty cycles should be calculated on the main oscillator frequency basis:
    Code: C / C++
    Log in, to see the code

    The duty values are assumed to be in percent and frequency is in Hz. The period values are calculates from them.

    Usage
    To use it in OBK, you need a BK7231N device. First, enable driver:
    
    startDriver PWMG

    Then, start the group:
    
    PWMG_Set 10 10 10 1000
    

    Full command syntax is the following:
    
    PWMG_Set Duty1Percent Duty2Percent DeadTimePercent Frequency PinA PinB
    

    Please note that the following will only work correctly for PWM pairs, like PWM0+PWM1, PWM2+PWM3, etc.
    Futhermore, for PinA/PinB you need to give a P-index, so PWM0 is not 0, it's 6.
    Please see screenshot below for explanation:
    Table showing GPIO interfaces and their corresponding IC pins.
    Of course, when typing command, type just "6", not "P6", like:
    
    PWMG_Set 10 10 10 1000 6 7
    


    Examples
    Command (1000Hz, 10% first, second 10%, dead 10%):
    
    PWMG_Set 10 10 10 1000
    

    Result:
    Oscilloscope display showing a PWM signal waveform.

    Command (1000Hz, 50% first, second 10%, dead 10%):
    
    PWMG_Set 50 10 10 1000
    

    Result:
    Rigol DS1054Z oscilloscope displaying PWM signals

    Command (5000Hz, 10% first, second 10%, dead 10%):
    
    PWMG_Set 10 10 10 5000
    

    Result:
    Oscilloscope screen displaying a PWM signal waveform.


    Summary
    PWM groups driver is now working, altough it still could be improved a bit. For example, the code is not currently checking for out of range values, and using them may yield unexpected results. Futhermore I have not tested how PWM behaves when user specified two PWMs that are not from the same group.
    The PWM groups driver was added per user request here, but I hope it can be useful for more people as well. Do you have any ideas how could you use such a driver? Let us know.

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11967 posts with rating 9997, helped 572 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 20888493
    niclauser
    Level 4  
    Thank you very much for creating the driver. I will test it in my project and report here.
  • ADVERTISEMENT
  • #3 20888500
    p.kaczmarek2
    Moderator Smart Home
    You're welcome, I think I will still take some time tomorrow to improve it futher, because with the current state of things, it's possible to "break" dead time by setting too high duty cycles for both PWMs. I think I will add an extra check for that in code that enforces dead time when incorrect arguments are passed.

    Do you have oscilloscope?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #4 20976850
    niclauser
    Level 4  
    Hello,

    Unfortunately, I've only recently gotten around to testing the new function again. I flashed a PEARL light chain ZX-5140-675 with Open Beken.

    See also my query here:
    https://www.elektroda.com/rtvforum/topic4010285.html#20840633

    The result is wonderful and you did a great job!!!

    By changing the values I was able to create different flashing frequencies for the fairy lights. Whatever is possible now, all the LEDs in the light chain are now easier, which was previously impossible due to the reverse parallel connection of the LEDs. Please watch the short video below.

    My problem, how can I switch the PWM groups on and off centrally? And would it be possible to integrate appropriate controls into the interface to adjust the frequencies? (Just like the light - dark - or the color controls on LED bulbs)












  • ADVERTISEMENT
  • #5 20990147
    p.kaczmarek2
    Moderator Smart Home
    That's a good news!

    What do you mean by "centrally"?

    You can script the device behaviour in autoexec.bat, see samples:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    You can also create a custom REST page for your device:
    https://www.elektroda.com/rtvforum/topic3971355.html
    Everything depends on your particular use case. Do you prefer to control your lights via Home Assistant or directly from OpenBeken?
    Helpful post? Buy me a coffee.
  • #6 20990894
    niclauser
    Level 4  
    By central I meant like an LED light bulb, an on/off switch and sliders for flashing frequency and light/dark.

    I prefer the control directly via OpenBeken.
ADVERTISEMENT