Servo Motor Automating

Looking for some help to automate a servo motor. I have a servo connected to an ESP32 with esphome. And I would like it to increase or decrease its angle of position by a certain amount when a button is pressed. Many automations i see are programming the motor to go to a pre defined position. My case, I would be changing the position in increments based on its current position when a button is pressed. This arduino code is exactly what I want to achieve, but in home assistant. I am not able to succeed automating it like that. Any help I would appreciate.

if (digitalRead(button) == LOW) { //if Value read of the button ==LOW:
pos++; //increases the value of the “pos” variable each time the push button of the left is pressed

@3ATIVE did an excellent tutorial on YouTube covering all aspects of controlling a servo motor.

Well worth a watch and subscribing to his channel.

~Brian

1 Like

Thank you Brian - I appreciate the support and the link :+1:

I’m using template number to help automation with servo.

number:

  - platform: template
    name: Servo Control
    id: servo_control
    min_value: 0
    max_value: 200
    step: 10
    optimistic: true
    set_action:
      then:
        - servo.write:
            id: my_servo
            level: !lambda 'return (x-100) / 100.0;'

Then on your button code (Gpio binary sensor) make automation for button press:
- number.increment: servo_control

which increments number by one step value.

This is currently how I have it setup. I see what your saying with the number.increment line. I just don’t know how to implement it into the gpio button configuration.

number:
  - platform: template
    id: servo_control
    name: Servo 1
    icon: mdi:sync
    optimistic: True
    min_value: -100
    max_value: 100
    initial_value: 0
    step: 2
    on_value:
      then:
        - servo.write:
            id: servo1
            level: !lambda 'return x / 100.0;'
          

servo:
  - id: servo1
    output: output1

output:
  - platform: ledc
    id: output1
    pin: 13
    frequency: 50 hz

binary_sensor:
  - platform: gpio
    pin: 
      number: 12
      mode:
        input: true
        pulldown: true
    name: "White Button"

on_press:
      then:
        - number.increment: servo_control

I use buttons to rotate a camera left/right like so…

servo:
  - id: barn_cam_servo
    output: pwm_output      
    auto_detach_time: 3s
    transition_length: 2s
    min_level: 4%
    max_level: 12%
    restore: true
    
    

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

number:
  - platform: template
    name: "Barn Servo"
    id: barn_servo_number
    min_value: -100
    initial_value: 0
    max_value: 100
    step: 20
    optimistic: true
    #internal: true
    on_value: 
      then:
        - servo.write:
            id: barn_cam_servo
            level: !lambda 'return x / 100.0;'    


button:
  - platform: template
    name: Servo Left
    id: camera_left
    on_press:
      - number.decrement:
         id: barn_servo_number
         #operation: Decrement         
         cycle: false
      - delay: 3s      
      - servo.detach: barn_cam_servo


  - platform: template
    name: Servo Right
    id: camera_right
    on_press:
      - number.increment:
         id: barn_servo_number
         #operation: Increment
         cycle: false
      - delay: 3s 
      - servo.detach: barn_cam_servo