Hi,
I would like to control 2 servos with a esp32cam using the pca9685. Connection to the pca9685 works as I also control some lights with it.
The sliders in Home Assistant do not seem to generate logs in the esp32.
Where did I do wrong?
esphome
# Enable Home Assistant API
api:
password: ""
services:
- service: control_servo_1
variables:
level: float
then:
- servo.write:
id: my_servo_1
level: !lambda 'return level / 100.0;'
- service: control_servo_2
variables:
level: float
then:
- servo.write:
id: my_servo_2
level: !lambda 'return level / 100.0;'
i2c:
sda: 12
scl: 13
scan: true
id: bus_a
pca9685:
id: pca9685_1
address: 0x40
frequency: 60 #500
output:
- platform: pca9685 #Ch5 - servo 1
id: 'pca_channel5'
channel: 5
- platform: pca9685 #Ch6 - servo 2
id: 'pca_channel6'
channel: 6
servo:
- id: my_servo_1
output: pca_channel5
- id: my_servo_2
output: pca_channel6
Home Assistant:
automation.yaml:
#automation:
- alias: Write Servo Value to ESP
trigger:
platform: state
entity_id: input_number.servo_control1
action:
# Replace livingroom with the name you gave the ESP
- service: esphome.esp32camera_control_servo1
data_template:
level: '{{ trigger.to_state.state | int }}'
- alias: Write Servo2 Value to ESP
trigger:
platform: state
entity_id: input_number.servo_control2
action:
# Replace livingroom with the name you gave the ESP
- service: esphome.esp32camera_control_servo2
data_template:
level: '{{ trigger.to_state.state | int }}'
configuration.yaml:
input_number:
servo_control1:
name: Servo Control1
initial: 0
min: -100
max: 100
step: 1
mode: slider
servo_control2:
name: Servo Control2
initial: 0
min: -100
max: 100
step: 1
mode: slider
mykey
October 29, 2022, 10:28pm
2
Hey Kobold, did you ever get this working? it looks like I am at the same stage you were back in Jan.
Hi mykey,
sorry, I ran against a wall with this and sidelined it
Were you creating your input_number entity in HA? I believe you are supposed to create it in the ESP device and use “number”, not “input_number.”
See the last example shown on this page:
I got it working using your code but making this change. Hopefully you can get it to work as well. Thanks!
Marown
(Marown)
October 29, 2023, 7:20am
5
Hi mofonics,
is it possible that you can post the finished code you got working?
IM finding a way to control some Servos with my ESP8266 and PCA9685.
There are many videos for android but i cant find any documentation about it with HA only directly with the ESP8266
Thank you
philpo
(Phil)
February 7, 2024, 11:55pm
6
I use an esp32 and esphome to drive continuous rotation (360) MG90 servos with the PCA9685, and here is my working code. Some things to note:
Since the PCA9685 is primarily intended to drive LEDs, the range is from 0 to 100, not -100 to +100. No idea if this is a bug or not.
The values I found for a 360 servo are:
stop: around 35
forward: from 40 on up (e.g. 60 is about half speed)
reverse: from 30 down (e.g. 15 is about half speed)
Here is the esphome code for the I2C bus to the PCA9685. Note the choice of which channel to use for each servo is arbitrary (0-15).
# there are 2 I2C buses on the ESP32
i2c:
# define an I2C bus for the PCA9685 PWM servo driver board
- id: bus_a
sda: GPIO23
scl: GPIO22
scan: True
# 16-channel(0-15) PWM driver board with I2C control
pca9685:
id: pca9685_1
i2c_id: bus_a
address: 0x40
frequency: 60 # 60Hz for servo, 500Hz for LEDs
output:
- platform: pca9685 #Ch2 - servo 1
id: 'pca_channel2'
channel: 2
- platform: pca9685 #Ch3 - servo 2
id: 'pca_channel3'
channel: 3
Here is the code for the template numbers for servo control:
# Define a number template to control servo 1
number:
- platform: template
name: Servo 1
id: servo1
min_value: 0
restore_value: False
max_value: 100
optimistic: true
step: 1
set_action:
then:
- servo.write:
id: my_servo1
level: !lambda 'return x / 100.0;'
# - display.page.show: page1
# Define a number template to control servo 2
- platform: template
name: Servo 2
id: servo2
min_value: 0
restore_value: False
max_value: 100
optimistic: true
step: 1
set_action:
then:
- servo.write:
id: my_servo2
level: !lambda 'return x / 100.0;'
# - display.page.show: page1
And some handy buttons to drive the servos:
# buttons for servo control presets
# note that we use number.set rather than servo.write
# so that the number slider updates on the web page and in HA
- platform: template
name: "servos_forward"
icon: "mdi:rotate-right"
on_press:
then:
- number.set:
id: servo1
value: 10
- number.set:
id: servo2
value: 60
- platform: template
name: "servos_reverse"
icon: "mdi:rotate-left"
on_press:
- number.set:
id: servo1
value: 60
- number.set:
id: servo2
value: 10
- platform: template
name: "servos_stop"
icon: "mdi:stop"
on_press:
then:
- number.set:
id: servo1
value: 35
- number.set:
id: servo2
value: 35
Regards
2 Likes