PWM Servo control with ESPhome?

I see, thanks
Was worried I might fry the board or something from what I’ve read online
I’ll be using 20kg servo to turn a 1/2" water valve, would a 5V 2A power supply be enough?

Description:
Brand: DSSERVO
Item: DS3218 20KG Digital Servo
Operating Voltage: 4.8 ~ 6.8 DC Volts
Torque: 18kg.cm(5V), 21.5kg.cm(6.8V)
Speed: 0.16sec/60°(5V), 0.14sec/60°(6.8V)
Dead Brand: 3μs
Motor Type: DC Motor
Gear Type: 25T Copper & Aluminum
Working Frequence: 50-333Hz
Totation: 180°(PWM 500-2500μs) / 270°(PWM 500-2500μs)
Weight: 60 g (2.12 oz)
Size: 40 x 20 x 40.5 mm ( 1.58 x 0.79 x 1.60 in)


I’m not sure,

interesting question though, I’m running a tiny plane RC type servo.

Someone with more electrical experience would have a better answer.

But you should be able to check the spec sheet for your ESP32

From experience with driving servos with an ESP myself, I’d say not. The starting surge of that very high torque RC servo will be way over 2 Amps. You will know if there is an issue if ESP resets the moment you try drive the servo…

To detect this add some yaml code to set a LED only at boot for a second, if that triggers when you drive the servo you will know.
I powered my servo from it’s own 5V supply to avoid potential issues with spikes to the ESP’s 3.3V regulator.

Hi, not quite sure which line of code to add for the led since I’m still new to this

How do i connect external power to the servo?
Do i need something like this?

https://shopee.com.my/product/118950817/4201021436?smtt=0.5847450-1635553012.9

Or i could just use any 5v 2a power adapter, strip the usb cable and connect dupont cables to it?

Yes to both, but the UBEC is the better choice. A regular 2A USB charger may just do it, but maybe not. 3A is a bit marginal also, 4A would cover it I think.

Pick a spare digital IO pin (must be one that is output capable, which most are) put a red LED on it.
Use some code inside on_boot to turn on the IO, delay 1 second, turn off the IO. That will be the tell tale…

1 Like

I’m having hard time finding 4a adapter.
Seems like most store selling 5V/2A, 5V/3A, 6V/2A type.
I might have to settle with 5v/3a

Try 3A, if the servo stalls when trying to move the valve you will know if you need more.

Ideally you would use a variable bench power supply of higher current capability to prototype this design with, and record the current needed when bench testing with the valve in place. You can move the servo with a servo tester before trying the ESP. Then buy the appropriate size UBEC.
New Multi Servo Tester 3CH ECS Consistency Speed Controler Power Channels CCPM Meter RC Drone Helicopter Model Parts RC Hobby|control warehouse|power control cablepower pest control - AliExpress?

Thanks, saw a stress video for the 20kg servo, and it does indeed go slightly above 2-3 amps.

Power HD 20kg vs Annimos 20kg - Which servo is telling the truth? - YouTube

Would it be better to just get 12v/5a adapter, and connect to the UBEC?

Gavin, have you been able to get servo.detach to work? I have a servo controlled by esp8266 using ESPhome and after it rotates it keeps buzzing. I see online on many forums that detaching could help with this. However, when I have this implemented in the yaml for my servo, it doesn’t seem to actually detach it. There is current running through the servo still and it continues to buzz/hum.

I tried both the servo.detach and the auto_detach_time but it doesn’t seem to work for me. Did it work for you?

Hi Daniel, it did work. Did you add the time decimal and the s on the end. This caught me out orginally.
Does yours look like this?

  - id: my_servo
    output: pwm_output
    auto_detach_time: 2s

Thanks Gavin,

I was missing the s. Thanks!
Also, I realized I needed to reinstall it on my esp again, and not just restart my HA. It is now working nicely.

1 Like

Digging up an old thread here, but is anyone else now having trouble with the servo detach? I havent changed anything but the detach doesnt seem to be working for me anymore. Also I don’t suppose if anyone would know how to add the detach as a switch inside home assistant rather than doing it inside the esphome interface?

servo:
  - id: my_servo
    output: pwm_output
    auto_detach_time: 1s
    
  - id: my_servo_2
    output: pwm_output2
    auto_detach_time: 1s
    
  - id: my_servo_3
    output: pwm_output3
    auto_detach_time: 1s
    
  - id: my_servo_4
    output: pwm_output4
    auto_detach_time: 1s
        
  - id: my_servo_5
    output: pwm_output5
    auto_detach_time: 1s

Servo Component — ESPHome says that servos have a detach action you can use


servo.detach Action
This Action allows you to disable the output on a servo motor - this will make the servo motor stop immediately and disable its active control.

on_...:
  then:
  - servo.detach: my_servo

Note
This action can also be expressed as a lambda:
id(my_servo).detach();


Which you can utilize with automations to control the servo detaching with a home assistant switch for example by triggering a lambda: Automations and Templates — ESPHome

You can’t just guess or ask someone else to guess if your power supply is enough to run your servo or any other device, I cant stress how important it is to get in the habit of reading a spec sheet or getting on Google and pulling it off the internet. Most, if not all of your questions will be answered in there. Not only that but, any special requirements or things to avoid, pin-outs, wire color ID, etc, etc. Learning this good habit will save you tons of stress, hours in troubleshooting, costly damage to components or even burning your house down.

1 Like

I having a problem using 2 mg996r, D1 mini but when i press the button in HA its spinning 90 degrees and not 180.

  - id: garage_door
    type: bool
    restore_value: false
    initial_value: 'false'


cover:
  - platform: template
    device_class: garage
    name: "Vaccum garage door"
    id: vaccum_garage_door

    open_action:
      - servo.write:
         id: servo_right
         level: 100%
      - servo.write:
         id: servo_left
         level: 100%
      - globals.set:
          id: garage_door
          value: 'true'
      
    stop_action:
      - servo.write:
         id: servo_right
         level: -10%
      - servo.write:
         id: servo_left
         level: 10%
      - globals.set:
          id: garage_door
          value: 'false'
    lambda: |-
      return id(garage_door);
      
servo:
  - id: servo_right
    output: pwm_output_right
    restore: true

  - id: servo_left
    output: pwm_output_left
    restore: true


output:
  - platform: esp8266_pwm
    id: pwm_output_right
    pin: D4
    frequency: 50 Hz

  - platform: esp8266_pwm
    id: pwm_output_left
    pin: D3
    frequency: 50 Hz

any ides why its set to 100% and max the servo can spin is 180 degrees

Three thoughts.
One is the servo is this one - MG996R Servo 90° Rotation - Maker Store PTY LTD.
Two is you may need -100 to +100 to get full rotation either side of center.
Three. Check with a ‘servo tester’ or code one up on an Arduino temporarily to make sure the servo is capable of what you thought.
Servo Tester - Maker Store PTY LTD

Given I have not used the new ESPhome ‘servo’ function myself (I used pwm light function long ago) I may be wrong. Somebody will let me know.

I am going crazy with this one, hopefully someone can help me out.

I want to push the washing machine button using a servo, I managed to control the servo using esphome and home assistant but unfortunately I only have a 360 degrees servo that continuously rotates.

I am using this code in esphome:

api:

  services:
    - service: control_servo
      variables:
        level: float
      then:
        - servo.write:
            id: my_servo
            level: !lambda 'return level / 100.0;'





...



output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: D4
    frequency: 50 Hz

servo:
  - id: my_servo
    output: pwm_output

but by passing a value the servo starts spinning at a certain speed.
I would like to just have a on/off switch in home assistant and have the movement done in esphome, like when pressed the servo rotates for 0.5s forward (pressing the button) and then it goes back for 0.5 seconds.

Any idea on how to achieve this?
Thanks

1 Like

Hey Lorenz I was gonna do the same thing, did you manage to make it works?
can you please send the code or you solution?