PWM Servo control with ESPhome?

Hi, I’ve tried it, and I am having trouble with it, can anybody here confirm that they have pwm servo control output working via ESPhome on a ESP8266 module of any type ?
(if so the code you use would be great to see).
Cheers,
Martin

Looks like I’m perhaps on my own with ESPhome and PWM servo control projects…
This is what I’m trying to use below. If I remove the Servo references it connects to WiFi and HA fine, if I add back the Servo references the NodeMCU will not connect to the router anymore. Reverting to the version without Servo then will connect as it did before. In the case of the full version that won’t connect, it will drive a servo when triggered by GPIO, so the servo code works, just breaks WiFi.
Clues anybody, I’ve tried two NodeMCU’s and also different pin combinations, fast and normal connect ?

esphome:
  name: servo_test2
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  
  fast_connect: true
# Enable logging

# Enable logging
logger:
  level: VERBOSE

# Enable Home Assistant API
api:

ota:

servo:
  - id: my_servo
    output: pwm_output

output:
  - platform: gpio
    pin: D1
    inverted: False
    id: gpio_d1_led
    
  - platform: esp8266_pwm
    id: pwm_output
    pin: D7
    frequency: 50 Hz

binary_sensor:
  - platform: gpio
    pin: D8
    name: "Multisensor Motion"
    device_class: motion
    
    on_press:
      then:
        - output.turn_on: gpio_d1_led
        - servo.write:
            id: my_servo
            level: 100.0%          
    on_release:
      then:
        - output.turn_off: gpio_d1_led
        - servo.write:
            id: my_servo
            level: -100.0% 
1 Like

try hitting up Otto on the Discord chat

I’ve tried via the Github issues path.
I really just need to find somebody who’s used the Servo component in a real project to know it works for somebody else.

Definitely try Discord, Otto (creator of ESPhome) is on there a fair bit and will be able to explain it. In saying that, I’d be keen to see you get it sorted too as this is something I may try one day

Have you got a link to the Discord you talk to him on ?

Just spent quite some time researching what Discord is, and installing/joining up to it, but I can’t locate his chat or directions to find it…

1 Like

Go yo esphome.io. There is a link at the top which says discord

This config is working for me, except I had to use -50% to +50% range
Are you sure the wifi settings is the guilty here? In my case when enabling wifi the servo does some tiny glitches (chatting) which might be due the esp cannot set a pwm output steady (this is a know limitation of esp8266)
Try setting the range to +10% and -10% and if it works increase them until you find the limits.

Thanks for ideas, will try here in a few days (on the road now).
What happened when you tried ±100% out of interest.
True, I’m not sure if WiFi settings are an issue, but I tried different connect speed, static IP etc. If I remove Servo then it connects in every WiFi setting.

The servo shuts off. It moves nowhere, and you can move the horn with your hand.

Try using another pin, D5, D6 or D8
I think the ESP8266 is PWM only on these pins.

My test analogue RC servo works fine on my NodeMCU V3, at +/-100% throw, with no chattering. I was very pleased how it drives the servo actually.

What dies is WiFi, once I include that code, no longer connects to Wifi, but servo responds to a GPIO button on the NodeMCU…

I have a ESP32 board arrived, I’m going to try the same code on that.

1 Like

After far too much faffing around with my two misbehaving cheap ESP8266 NodeMCU’s with the ESPhome Servo component without success, I now have full success driving the RC servo with an ESP32 NodeMCU instead.
I don’t know what’s wrong with these two ESP8266 NodeMCU’s I had (and I don’t care now) but the same servo, same AM312 motion sensor and same 5V power supply and cables works fine on the new ESP32 (which is a cheap ESP-WROOM-32 from Ebay).
As the ESP32 is not supported for the Servo component (apparently the libraries don’t exist yet AFAIK) I used the ESP32 LEDC output component.

esphome:
  name: servo_actuator_1
  platform: ESP32
  board: esp32doit-devkit-v1

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  fast_connect: false

# Enable logging
logger:
  level: VERBOSE

# Enable Home Assistant API
api:

ota:

sensor:
  - platform: wifi_signal
    name: "Servo actuator #1 WiFi Signal Sensor"
    update_interval: 60s  
    
binary_sensor:
  - platform: status
    name: "Servo actuator #1 Status"
    
  - platform: gpio
    pin: GPIO23
    name: "Servo actuator #1 Motion"
    device_class: motion 
    
    on_press:
      then:
        - output.turn_on: triggered_led
        - output.set_level:
            id: gpio_21_servo
            level: 100.0% # 100% = 2mS as set by 'max_power'

    on_release:
      then:
        - output.turn_off: triggered_led
        - output.set_level:
            id: gpio_21_servo
            level: 0.0%  # 0% = 1mS as set by 'min_power'     

# Using Light component to set a standard RC servo position
light:
  - platform: monochromatic
    output: gpio_21_servo
    name: "Servo actuator #1 - Servo Pos"
    default_transition_length: 0ms
    
# Using ledc output to control a standard 50Hz RC servo over the normal 1 to 2 mS pulse range
output:
  - platform: ledc
    pin: GPIO21
    id: gpio_21_servo
    frequency: 50 Hz
    min_power: 5.0%    # 5% at 50Hz is 1mS  (20mS cycles)
    max_power: 10.0%   # 10% at 50Hz is 2mS (20mS cycles)

  - platform: gpio
    pin: GPIO22
    inverted: False
    id: triggered_led

The servo control in HA appears as a light of course…

3 Likes

This should be translated to a feature request in esphome feature tracker. I mean to support servo in esp32. Would you add it?

Hello,

i want to make the same with an ESP32-cam but it doesnt work… :

  name: garage_motor
  platform: ESP32
  board: esp-wrover-kit

wifi:
  ssid: "xxxxxxxxx"
  password: "xxxxxxxxxxxxxx"
  manual_ip:
    static_ip: 192.168.1.48
    gateway: 192.168.1.1
    subnet: 255.255.255.0
  
# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "xxxxxxxxxxxx"

ota:
  password: "xxxxxxxxx"
  
# ESP32-CAM
esp32_camera:
  external_clock:
    pin: GPIO0
    frequency: 20MHz
  i2c_pins:
    sda: GPIO26
    scl: GPIO27
  data_pins: [GPIO5, GPIO18, GPIO19, GPIO21, GPIO36, GPIO39, GPIO34, GPIO35]
  vsync_pin: GPIO25
  href_pin: GPIO23
  pixel_clock_pin: GPIO22
  power_down_pin: GPIO32
  vertical_flip: false
  horizontal_mirror: false
  resolution: 640x480 

  name: garage_motor
  # ... Frame Settings

sensor:
  - platform: wifi_signal
    name: "WiFi Signal garage_motor"
    update_interval: 60s
    id: signalWgm

binary_sensor:
  - platform: status
    name: "ServoRotate actuator Status"
    
    on_press:
      then:
        - output.set_level:
            id: gpio_13_servoR
            level: 100.0% # 100% = 2mS as set by 'max_power'
    on_release:
      then:
        - output.set_level:
            id: gpio_13_servoR
            level: 0.0%  # 0% = 1mS as set by 'min_power'

switch:
  - platform: restart
    name: "Restart Cam garage_motor"
  
output:
  # Flashlight 
  - platform: gpio
    pin: GPIO4
    id: gpio_4
  # Servo Rotate : Using ledc output to control a standard 50Hz RC servo over the normal 1 to 2 mS pulse range
  - platform: ledc
    pin: GPIO13
    id: gpio_13_servoR
    frequency: 50 Hz
    min_power: 5.0%    # 5% at 50Hz is 1mS  (20mS cycles)
    max_power: 10.0%   # 10% at 50Hz is 2mS (20mS cycles)
    
# Flashlight    
light:
  - platform: binary
    output: gpio_4
    name: $hostname lightgm
    
  # Using Light component to set a standard RC servo position
  - platform: monochromatic
    output: gpio_13_servoR
    name: "ServoRotate Position"
    default_transition_length: 0ms

If someone can help, i will appreciate :wink:

thanks

Olivier

Oliver, I’m a newbie also but can try help, but could you edit and repost the YAML in the same style format as it is in ESPhome, with the back-tick method, it’s going to be easier to read ?

Place three ‘back ticks’ a line before and line after you paste the YAML…
(back tick ` is the character under the ‘tilde’ ~ on the top LHS of a US keyboard)

What errors are you getting? What do the EspHome logs say?

@mr.sneezy r.sneezy
thanks i dont know how to do it lol…i have modify

@ OttoWinter said “ledc” output might be used by internals on ESP32-CAM, its not like a “ordinary” ESP32…

thanks @Drew.B @mr.sneezy

no error, no logs, the image of the cam no longer works…as i said just up : OttoWinter said “ledc” output might be used by internals on ESP32-CAM, its not like a “ordinary” ESP32…

i have try another way : use a esp8266 to control tilt and yaw and try this exemple :

but now my hassio stucks and fail to login to lovelace page…

here is my config esphome:

  name: capteur_garage
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "xxxxxx"
  password: "xxxxxxxxxxx"
  manual_ip:
    static_ip: 192.168.1.46
    gateway: 192.168.1.1
    subnet: 255.255.255.0

# Enable logging
logger:

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

ota:
  password: "xxxxxxxxx"
  
i2c:
  sda: D2
  scl: D1
  scan: True

binary_sensor:
  - platform: status
    name: "Status Wifi"
    id: etatWcg

sensor:
  - platform: wifi_signal
    name: "WiFi Signal"
    update_interval: 60s
    id: signalWcg

  - platform: bmp085
    pressure:
      name: "Pression Atmospherique Garage"
      id: pressure
    update_interval: 60s
    
  - platform: dht
    pin: GPIO14
    temperature:
      name: "Temperature Garage"
      id: temperature
    humidity:
      name: "Humidity Garage"
      id: humidite
    model: DHT22
    update_interval: 60s

switch:
  - platform: restart
    name: "Restart Capteur Garage"
    
output:
  - platform: gpio
    pin: 
      number: GPIO0
      mode: OUTPUT_OPEN_DRAIN
      inverted: yes
    id: relaisg
    # PinPwm configuration entry servo rotate
  - platform: esp8266_pwm
    pin: GPIO02
    id: pwm_out_ServoR
    frequency: 50 Hz 
fan:
  - platform: binary
    output: relaisg
    name: "Relais Garage"

servo:
  - id: servoR
    output: pwm_out_ServoR

config input_number: (yaml dont exist, i create one)

servoR_control:
    name: Servo Rotate Control
    initial: 0
    min: -100
    max: 100
    step: 1
    mode: slider

automation :

automation:
  - alias: Write Servo Value to ESP
    trigger:
      platform: state
      entity_id: input_number.servoR_control
    action:
      # Replace livingroom with the name you gave the ESP
      - service: esphome.capteur_garage_control_servo
        data_template:
          level: '{{ trigger.to_state.state | int }}'

after that the server is only accessible by samba file in explorer…no more login possible.

Sure there is something wrong in one or more of thoses files…

I search…lol

If you see something…thanks!

Oliver you may well need to try a standard ESP32 module first, rather than the ESP32-CAM module like Otto suggested. The camera code may use up peripherals or ram that is needed by LEDC.
Also, the esp8266_pwm component ONLY applies to the ESP8266, does not run yet on the ESP32’s.
When this stuff happens to me I try strip back the project code to only the problem part, and hack at it till that bit is fixed, then introduce back the other parts around it.