ESPhome servo controlled by a potentiometer

Has anyone ever controlled a servo with a potentiometer wired to a D1 mini ?

esphome:
  name: boat

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:

  services:
    - service: boat_servo    # add in config and add a automation     
      variables:
        level: float
      then:
        - servo.write:
            id: my_servo
            level: !lambda 'return level / 100.0;'

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

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

captive_portal:



sensor:
  - platform: adc
    pin: A0
    name: "Pot"
    update_interval: 5s
    # ...
    filters:
      - multiply: 3.3


# Output platform
output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: D1
    frequency: 50 Hz


# Configuration entry
servo:
  - id: my_servo
    output: pwm_output

How can I take the number from the Pot and position the servo?

1 Like
sensor:
  - platform: adc
    pin: A0
    name: "Pot"
    update_interval: 5s
    on_value:
      then:
        - servo.write:
            id: my_servo
            # For template values, range is [-1; 1]
            level: !lambda { return (x - 0.5) * 2 }
            # Converting [0;1] range of ADC to [-1;1]

# Output platform
output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: D1
    frequency: 50 Hz


# Configuration entry
servo:
  - id: my_servo
    output: pwm_output

That looks like it will work perfect. I’m getting a few errors compiling though.

/config/esphome/boat.yaml:57:24: warning: character constant too long for its type
   57 |             level: !lambda { return (x - 0.5) * 2 }
      |                        ^~~~~~~~~~~~~~~~~~~~~~~
/config/esphome/boat.yaml:57:55: warning: character constant too long for its type
   57 |             level: !lambda { return (x - 0.5) * 2 }
      |                                                       ^                     
/config/esphome/boat.yaml:57:95: warning: character constant too long for its type
   57 |             level: !lambda { return (x - 0.5) * 2 }
      |                                                                                               ^                       
/config/esphome/boat.yaml:57:127: error: empty character constant
   57 |             level: !lambda { return (x - 0.5) * 2 }
      |                                                                                                                               ^ 
/config/esphome/boat.yaml: In lambda function:
/config/esphome/boat.yaml:57:8: error: expected identifier before '(' token
   57 |             level: !lambda { return (x - 0.5) * 2 }
      |        ^
/config/esphome/boat.yaml: In lambda function:
/config/esphome/boat.yaml:58:3: error: expected '{' before '}' token
   58 |             # Converting [0;1] range of ADC to [-1;1]
      |   ^
/config/esphome/boat.yaml: In lambda function:
/config/esphome/boat.yaml:57:132: error: expected ';' before '}' token
   57 |             level: !lambda { return (x - 0.5) * 2 }
      |                                                                                                                                    ^
      |                                                                                                                                    ;
   58 |             # Converting [0;1] range of ADC to [-1;1]
      |                                                                                                                                     
/config/esphome/boat.yaml:58:3: error: no return statement in function returning non-void [-Werror=return-type]
   58 |             # Converting [0;1] range of ADC to [-1;1]
      |   ^
cc1plus: some warnings being treated as errors
*** [/data/boat/.pioenvs/boat/src/main.cpp.o] Error 1
========================== [FAILED] Took 4.55 seconds ==========================

Solution

level: !lambda 'return (x - 0.5) * 2;'

Thank you Daryl

1 Like

Good to see you got it working. Sorry - I always forget the quotes… :grinning: