PWM Servo control with ESPhome?

@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.

i understand but i have try to control servo with an additionnal esp8266 : wemos D1mini, not a esp32 or esp32cam :
i have put back the configuration for my esp32cam, that work fine, and want to put a wemos d1 mini for the servo, in the EspHome documentation, the esp8266 works with servo component, right?

Yes right, the 8-bit esp8266 works fine with the ESPhome servo component for most people.

Done… :slight_smile:

It take 2 hours lol but it works great : Tilt and Yaw on my esp32Cam :
https://www.thingiverse.com/thing:3579507

…it was “just” the trigger that doesnt work because i put :
'{{ trigger.to_state.state | int }}'
and it must be :
"{{ trigger.to_state.state | int }}"
the log help to fix it…
@Drew.B :wink:

thanks for your attention and help @mr.sneezy and @Drew.B

1 Like

Thanks guys,

with a bit of tweaking I got it working!
Only thing is my servo keep on spinning now and it does not stop?

ESPCONFIG

esphome:
  name: petfeeder
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "Mistery"
  password: "YouWishYouKnew"

# 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:

servo:
  - id: my_servo
    output: pwm_output

 # PinPwm configuration entry servo rotate
output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: GPIO02
    frequency: 50 Hz

If it’s a standard RC type servo and the motor spins continuously it normally means a gear has stripped inside.

I have 2 servo motors mg995 model.
One is modified to spin continuously and one just the original one. As for both the keep on working as the modified one just spins 360 degrees the othee one does not but you can hear by the sound it makes it is trying to spin more then possible.

I note now you are using the servo component unlike me (I’m using a ESP32).
What you might need to do is reduce the percentage a bit and see if the servos behave properly. Try a minimum of 20% and a maximum of 80% and see how that goes. Some servos don’t like being driven to the theoretical limits for RC servos (1mS to 2mS).

Thanks! I got it spinning again. But still to control it does not work. All I want to do is to open and close is for a few seconds a day.

this is what i want to manage. :smiley: So hope anybody can help or give me some new idea’s to finalize this before my holidays :smiley:

I love the simplicity of your implementation.

Is there a way you can enter the PPM values? 0.5ms for 0°, 1.5ms for 90°, and 2.5ms for 180°.
Or if it’s a percentage input, try max 12.5%.

It spins faster but still unable to control it please any help?

Did you mod this servo with the 2x 2.2k resistors?

Is he using a non standard servo (i.e. not for RC models), as they typically use 1mS to 2mS for full 0-100% travel and can become unstable outside that.

Servo PWM diagram link

To get a MG995 to spin continuously, you need to mod it by removing the position feedback pot normally. So you use two resistors in place of the pot to replicate a partial rotational movement. As the servo’s feedback loop never reaches the PPM input position, it just keeps spinning endlessly until you input the centre position signal (or a signal inside the value of the resistor you used) It can be really difficult to stop a servo rotating accurately once it’s been modded.

Ah thanks, I see. Yes I can imagine that without adjusting for more dead band, it would be devilishly hard to get it to stop. To make the feeder device he linked though, why would it need to spin 360 continuous, it looks like that one used a normal travel range ?
(a rhetorical question no need to answer :slight_smile:

I agree with you, probs not necessary to do a full 360. Hopefully @Khalid_Abu_Hamza got it sorted?

Got a feather huzzah32 and an sg90 servo; all I get is buzzing and some grinding when I move the servo control (as outlined on the esphome website).
Power is bypassing the esp (from the same wall wart 2.5a)…

“Experiment with” the values for min_level and max_level, but all it seemed to do is change the pitch at which the servo squealed (occasionally moving a bit).

Got this to work on the nodeMCU, but I need a small micro controller, tried the esp32 pico and couldn’t get it to work either (same deal)… is this a limitation on some controllers?

Any help would be greatly appreciated!

``
servo:

  • id: my_servo
    output: cerveau

output:

  • platform: ledc
    pin: GPIO21
    id: cerveau
    frequency: 50 Hz
    min_power: 2.5% # 5% at 50Hz is 1mS (20mS cycles) was 5
    max_power: 9% # 10% at 50Hz is 2mS (20mS cycles) was 10
    ``