PWM Servo control with ESPhome?

@keithcroshaw @Khalid_Abu_Hamza @Olivier974 @mr.sneezy @Drew.B

Hi All,

I’m hoping for some help - I’m trying to control a servo from an esp32 and have followed the guides, but I must be doing something wrong as the servo doesn’t move. I’ve setup esphome and tested with turning on an led via home assistant, which works, so I believe I’m okay for pin numbers etc. I’ve also previously tested controlling the servo directly from the esp32 via micropython and that worked, so I’m pretty happy with my wiring up etc.

I’m using AZDelivery ESP32 NodeMCU Dev Kit C WiFi WLAN CP2102 ESP32-WROOM-32D from Amazon UK. It isn’t listed in the board types. I’ve tried a few, including nodemcu with no luck.

Code in ESPHome - robogarage.yaml

esphome:
  name: robogarage
  platform: ESP32
  board: esp32dev

# Enable logging
logger:

# Enable Home Assistant API
api:
  services:
    - service: control_servo
      variables:
        level: float
      then:
        - servo.write:
            id: my_servo
            level: !lambda 'return level / 100.0;'

ota:
  password: "abcdefg"

wifi:
  ssid: "xxxxxxxx"
  password: "xxxxxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Robogarage Fallback Hotspot"
    password: "xxxxxxx"

captive_portal:

servo:
  - id: my_servo
    output: pwm_output

output:
  - platform: ledc
    id: pwm_output
    pin: GPIO32
    frequency: 50 Hz

Then, in my configuration.yaml I have:

input_number:
  servo_control:
    name: Servo Control
    initial: 0
    min: -100
    max: 100
    step: 1
    mode: slider

automation:
  - alias: Write Servo Value to ESP
    trigger:
      platform: state
      entity_id: input_number.servo_control
    action:
      - service: esphome.robogarage_control_servo
        data_template:
          level: '{{ trigger.to_state.state | int }}'

and finally, the lovelace card:

type: entities
entities:
  - input_number.servo_control

If anyone can see ANYTHING obvious (or not so obvious) that I have done incorrectly or misunderstood, I would be incredibly grateful for your thoughts.

Maybe you’re still using the LED platform?
It might not matter but that might be a typo you forgot to change back.
I’ve only ever seen the pins be like D5 in my example, but I’ve only ever used the one board type.
And you don’t have an end device declared like the cover in my example.
Again that might not be necessary but I’m just looking what’s different between our esphome config files.

Steal mine and see if you can at least get it to twitch or something then work backwards from there?
ESPHome has been 100% trial and error for me.
Good luck!

Hello,

look at my last post : https://community.home-assistant.io/t/pwm-servo-control-with-esphome/111762/23

it only work for me with " " not ’ ’

try

@Olivier974 & @keithcroshaw
Thank you both so much for your time and input. I tried all your suggestions but didn’t have any luck. However, I have now identified my embarrassing mistake - I put the automation code in the config yaml and not the automations one. :flushed: I guess we live and learn!
Thanks again,

2 Likes

lol!

nice you find your mistake :wink:

1 Like

Solved! (My issue anyways)

For me there were two issues.

  1. Feather huzzah 32 simply did not want to servo… Just nope :person_shrugging:

  2. Since there were other pwm devices, there was interference or something. Took me forever to figure this out but I had to add channels. What’s important is that the servo needed to be non adjacent to the following channels. So servo on chanel 1 skip a channel then the next device on channel 3. In the esphome docs there’s a line that says two adjacent channels share the same timer.

I hope this helps someone!

2 Likes

GOT IT WORKING!

Ill leave this here though, hope it will help someone :slight_smile:

Hey all,

Trying to do this, but don’t have any context for what needs to go where on the HA side of things

Have worked out the following for anyone that’s stuck.

Config.yaml

input_number:
  servo_control:
    name: Servo Control
    initial: 0
    min: -100
    max: 100
    step: 1
    mode: slider

Place the below in your automation, sadly this yaml is not correct though and currently gives an error

Message malformed: required key not provided @ data[‘action’]

automation:
  - alias: Write Servo Value to ESP
    trigger:
      platform: state
      entity_id: input_number.servo_control
    action:
      - service: esphome.robogarage_control_servo
        data_template:
          level: '{{ trigger.to_state.state | int }}'

Id really love to know what the correct yaml is and, maybe more importantly, how this is working for you in the first place with this malformed yaml. Are you guys on super old versions and its been changed or?? I see this a lot, people seem to be often posting various things with the wrong syntax.

Thanks

EDIT

I managed to save the automation with this

alias: 'Flush Toilet '
description: ''
trigger:
  - platform: state
    entity_id: input_number.servo_control
condition: []
action:
  - data_template:
      level: '{{ trigger.to_state.state | int }}'
    service: esphome.robotarm_control_servo
mode: single

But its not working when I change the slider. No noise, no movement.

Here is my ESPHome.yaml

esphome:
  name: toilet
  platform: ESP32
  board: esp32dev

# Enable logging
logger:

# Enable Home Assistant API
api:
  services:
    - service: control_servo
      variables:
        level: float
      then:
        - servo.write:
            id: toilet
            level: !lambda 'return level / 100.0;'

ota:
  password: "XXXXXX"

wifi:
  ssid: "SSID"
  password: "password"
  
  manual_ip:
    static_ip: X.X.X.X
    gateway: X.X.X.X
    subnet: X.X.X.X

captive_portal:

output:
  - platform: ledc
    pin: GPIO04
    id: gpio_04_servo
    frequency: 50 Hz
    
servo:
  - id: toilet
    output: gpio_04_servo

I think I have this wired correctly, anything stick out?

GPIO 4

image

5V and GND

image

Servo pin colours

Servo

EDIT2

GOT IT WORKING

Make sure you name the service in your automation correctly esphome._control_servo .

alias: 'Flush Toilet '
description: ''
trigger:
  - platform: state
    entity_id: input_number.servo_control
condition: []
action:
  - data_template:
      level: '{{ trigger.to_state.state | int }}'
    service: esphome.toilet_control_servo
mode: single
1 Like

Just wondering about this set up. I have it all working and i’m able to use the ‘servo control’ slider to control the servo. But the ‘write servo value to ESP’ only disables the servo control slider. Has anyone been able to actually stop the servo running? If you listen to your servo it is constantly trying to turn, in time this will sure wear the servo out or if you use multiple servos you will hear the hum?

Im no expert but in the past i used 125 as a pulse length to turn the servo one way (not through home assist), 575 to turn the servo another and was also able to use 4096 to detach the servo which shut the power off and stopped the humming.

Anyone any thoughts on a way to incorporate that into the setup?

I think i may have jumped the gun with this one.

Noticed in the esphome info pages regarding servos, there’s some info on detaching. Will check that out now, if anyone else is interested the link is.

The link was posted earlier, but if anyone comes across my question, here’s the link again

This works perfect for me.
But my servo is vibrating?
Servo does not vibrating if i program with IDE…
Do you know a solution for this?

Hi, do you mind sharing how you connected 5v power supply to the esp32?
Pretty new to esphome

Hey,

I’m just using the USB connector for power

1 Like

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?