logo elektroda
logo elektroda
X
logo elektroda

OpenBeken SG90 servo control configuration guidance, Home Assistant integration?

CodeFaux 417 15
ADVERTISEMENT
  • #1 21462070
    CodeFaux
    Level 2  
    Hi all.

    I've got a device (IR rx/tx) running OpenBeken, and I need to drive a servo with it. I can handle the pins and the soldering and all that, but I'm coming up short on configuration.

    I need to be able to set the angle (I don't care if it's 0~180, 0~1, 0~255, etc) and release the servo ~2sec later. I also wish to control the servo and IR tx/rx from Home Assistant.

    OpenBeken seems to only provide PWM, so I'm guessing I can use PWM to poke the servo, but I'm having an awful time looking up command docs and figuring out proper parameters. They're SG90 clones, 0~180 degrees, but I've got little to no experience working with both servos and OpenBeken. I've settled on GPIO9/PWM3, it's the only PWM configured on this device so that makes it channel 0 if I understand things correctly.

    Could anyone point me in the right direction for the next steps?
  • ADVERTISEMENT
  • #2 21462196
    p.kaczmarek2
    Moderator Smart Home
    Sure, it's a good idea. Let me try this out so we can figure out a config for you:
    Handheld oscilloscope with signal graph and breadboard with electronic components.
    @divadiow do you have any servos?

    @insmod I think it may be a good time to clear up pwm frequency settings and maybe add them per pin, can you check out this PR? https://github.com/openshwprojects/OpenBK7231T_App/pull/1554 WIP

    Added after 32 [minutes]:

    Set required pin to PWM_ScriptOnly. Give it channel 20. Just to be sure, use my PR or wait for merge. Use autoexec.bat:
    
    PWMFrequency 20
    
    startDriver httpButtons
    
    setButtonEnabled 0 1
    setButtonLabel 0 "Set -90"
    setButtonCommand 0 "setChannelFloat 20 5"
    
    setButtonEnabled 1 1
    setButtonLabel 1 "Set 0"
    setButtonCommand 1 "setChannelFloat 20 3"
    
    setButtonEnabled 2 1
    setButtonLabel 2 "Set 90"
    setButtonCommand 2 "setChannelFloat 20 1"
    

    It works, more or less:


    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #3 21462475
    insmod
    Level 22  
    >>21462196 LGTM for setting up frequency in pwm start, but i'm not sure about per channel frequency. Theoretically it's a good idea, but practically i've yet to encounter any device that would've required it.
    And isn't array size of 16 for periods a little too big? Set it to 6 and use pwmIndex, not pin index to access it. (and what would happen now if pins 24 or 26 are selected? out-of-bounds?)
  • ADVERTISEMENT
  • #4 21462561
    p.kaczmarek2
    Moderator Smart Home
    Actually I have considered it again and I also decided that maybe for now I'll stop with what we have now. Per channel frequency can be added later when we'll have someone show up who really needs this setting.

    For the use case from this topic, global frequency seems still fine.

    Array is a placeholder, I'll check size in a moment
    Helpful post? Buy me a coffee.
  • #5 21462609
    divadiow
    Level 34  
    p.kaczmarek2 wrote:
    @divadiow do you have any servos?

    Negative. I do not
  • #6 21463528
    CodeFaux
    Level 2  
    >>21462196

    This is a good start. Thank you very much. It is working. I didn't have PWM_ScriptOnly but an update fixed that.

    Is there a way I can set the script to release the servo automatically after ~2 sec or must I implement that myself? I need to release the servo so it doesn't overheat and melt if it's stuck, and so the user can adjust the control themselves if required. 'setChannelFloat 20 0' will release, but I need a pause while the servo moves.
  • ADVERTISEMENT
  • #7 21463683
    p.kaczmarek2
    Moderator Smart Home
    I'm not sure what you mean, are you asking how to make a delay? You can use delay_s within autoexec.bat script. Or are you asking how to entirely disable PWM? Well, I would assume that, for example, doing setPinRole to AlwaysLow or AlwaysHigh can do that, but PWM with 0 duty is the same as AlwaysLow...

    Or maybe I am overthinking it and you just need to read:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    and adjust:
    
    alias disable_with_delay backlog cancelRepeatingEvent 123456; addRepeatingEventID 5 1 123456 setChannel 1 0
    

    maybe into:
    
    alias setOn setChannelFloat 20 0.2
    alias setOff setChannelFloat 20 0
    alias run_and_disable backlog setOn ; cancelRepeatingEvent 123456; addRepeatingEventID 5 1 123456 setOff 
    

    Or you can do the same in script...
    Helpful post? Buy me a coffee.
  • #8 21465137
    CodeFaux
    Level 2  
    >>21463683

    A servo forces its position to match PWM signal.

    If you turn off PWM the servo releases and will let you turn it.

    I need to stop sending PWM after ~2 sec, any time PWM is enabled.

    Seems some modification of https://www.elektroda.com/rtvforum/topic4011890.html#20797236 should work.

    
    // want to turn off PWM on CH20 after 2 seconds
    // addRepeatingEvent   [IntervalSeconds][RepeatsOr-1][CommandToRun]
    alias turn_off_after_time addRepeatingEvent 2 1 setChannel 20 0
    // this should run turn_off_after_time every time that CH20 becomes not 0 but does nothing
    addChangeHandler Channel20 != 0 turn_off_after_time 
    


    But it does not work. turn_off_after_time works, and addChangeHandler works, but channel 20 changes don't trigger the alias. Any suggestions?
  • #9 21465242
    p.kaczmarek2
    Moderator Smart Home
    That change handler is called when a transition from 0 to non 0 value happens. It won't be called when CH20 transitions from non-zero to non-zero value.

    I think you rather need:
    
    alias turn_off_after_time addRepeatingEvent 2 1 setChannel 20 0
    alias set90deg backlog setChannelFloat 20 0.2; turn_off_after_time 
    

    The following will explicitly call delay and reset after you set given position in the servo.
    Helpful post? Buy me a coffee.
  • #10 21466620
    CodeFaux
    Level 2  
    >>21465242

    The 'addChangeHandler' Channel20 != 0 'turn_off_after_time' trigger is not called when a transition from 0 to non-0 happens. I know it should, but it does not. I tested it. That's why I said it doesn't work.

    You have provided an alias to set to 90 degrees. I do not want to change to 90 degrees. I need to turn off PWM, automatically, after ~2sec, any time PWM is turned on to any value. The angle is not known ahead. I don't always need 90 degrees. I need to turn off PWM after ~2sec, always. 'addChangeHandler' should provide this, but it does not work for me. Can we fix this?
  • #11 21466756
    p.kaczmarek2
    Moderator Smart Home
    Can you provide more information how to replicate addChangeHandler not working?
    I've tried to check addChangeHandler in self test but it passes both on my machine and on Github.

    Test commit:
    https://github.com/openshwprojects/OpenBK7231...mmit/8c01b0cf83aaa2f34d2498b25b163ac1c51ebe8e
    Screenshot:
    Piece of code in Visual Studio editor.
    Unless the problem is in the float version?
    It seems that's the case...
    Well, for now, I can try to add a work around, but I am not sure if it will work good with fractional parts, I will try to look into it a bit later more. I will just most likely adjust change callbacks to work with floating points internally.
    Helpful post? Buy me a coffee.
  • #12 21492769
    CodeFaux
    Level 2  
    p.kaczmarek2 wrote:
    Can you provide more information on how to replicate addChangeHandler not working?
    I've tried to check addChangeHandler in self test but it passes both on my machine and on Github.

    Test commit:
    https://github.com/openshwprojects/OpenBK7231...mmit/8c01b0cf83aaa2f34d2498b25b163ac1c51ebe8e
    Screenshot:
    Piece of code in Visual Studio editor.
    Unless the problem is in the float version?
    It seems that's the case...
    Well, for now, I can try to add a workaround, but I am not sure if it will work good with fractional parts, I will try to look into it a bit later more. I will just most likely adjust change callbacks to work with floating points internally.


    Here is my autoexec.bat set with buttons for initial testing. I will remove buttons and control differently later.

    
    PWMFrequency 20
    
    startDriver httpButtons
    
    setButtonEnabled 0 1
    setButtonLabel 0 "Close"
    setButtonCommand 0 "setChannelFloat 20 5"
    
    setButtonEnabled 1 1
    setButtonLabel 1 "Open"
    setButtonCommand 1 "setChannelFloat 20 1"
    
    setButtonEnabled 2 1
    setButtonLabel 2 "Release"
    setButtonCommand 2 "setChannelFloat 20 0"
    
    alias turn_off_after_time addRepeatingEvent 2 1 setChannel 20 0
    
    addChangeHandler Channel20 != 0 turn_off_after_time
    



    I expect: Click any button. Servo moves. ~2sec later, servo turns off.
    I get: Click any button. Servo moves. Servo never turns off.

    Version: "Built on Mar 2 2025 22:30:59 version 1.18.53"

    Device template;
    
    {
      "vendor": "Tuya",
      "bDetailed": "0",
      "name": "aaa",
      "model": "aaa",
      "chip": "BK7231N",
      "board": "TODO",
      "flags": "0",
      "keywords": [
        "TODO",
        "TODO",
        "TODO"
      ],
      "pins": {
        "6": "Btn;0",
        "7": "IRRecv;0",
        "8": "WifiLED;0",
        "9": "PWM_ScriptOnly;20",
        "26": "IRSend;0"
      },
      "command": "",
      "image": "https://obrazki.elektroda.pl/YOUR_IMAGE.jpg",
      "wiki": "https://www.elektroda.com/rtvforum/topic_YOUR_TOPIC.html"
    }
    
  • #13 21530807
    CodeFaux
    Level 2  
    Nevermind, I will find another solution. Thank you for your time.
  • #14 21530889
    p.kaczmarek2
    Moderator Smart Home
    I haven't seen this message. Maybe you can give a repeating event with time delay instead?

    Or .. use Berry. We've added Berry script 10 days ago, maybe you can use that:
    https://www.elektroda.com/rtvforum/topic4117238.html
    List of downloadable files from the Assets section with highlighted berry filenames.
    Helpful post? Buy me a coffee.
  • #15 21531333
    miegapele
    Level 15  
    Not mixing setChannel with setChannelFloat should work, like this:
    alias turn_off_after_time addRepeatingEvent 2 1 setChannelFloat 20 0
    

    The bug is that setChannel does not update float value properly and prevents event triggering
  • #16 21531343
    p.kaczmarek2
    Moderator Smart Home
    Maybe we should really migrate to float channels, but will it be safe? I wouldn't want to break stuff
    Helpful post? Buy me a coffee.

Topic summary

The discussion revolves around configuring an SG90 servo using OpenBeken firmware, specifically for integration with Home Assistant. The user seeks guidance on setting the servo angle and implementing a delay to release the servo after approximately 2 seconds to prevent overheating. Various responses provide insights into using PWM for servo control, setting up button commands in an autoexec.bat script, and handling timing with the addRepeatingEvent function. Issues with the addChangeHandler not triggering as expected are also addressed, with suggestions for workarounds and adjustments to the script to achieve the desired functionality.
Summary generated by the language model.
ADVERTISEMENT