├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 3ATIVE 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Using Servos in Home Assistant with ESPHome 2 | 3 | Using Servos from ESPHome is quite simple. Check out the 🎬[YouTube tutorial](https://youtu.be/1f2tuS_PxIE) for setting up and options available. 4 | ___ 5 | ### The Basic Setup: 6 | 7 | #### 🛠 The `servo:` Component: 8 | ```yaml 9 | servo: 10 | - id: servo1 11 | output: output1 12 | ``` 13 | 14 | #### ❓ Optional Servo settings and calibration: 15 | ```yaml 16 | auto_detach_time: 5s # Optional - Default 0 (Always On) 17 | transition_length: 0s # Optional - Default 0 (Max Speed) 18 | min_level: 2.7% # Optional - Default 3% 19 | idle_level: 7.3% # Optional - Default 7.5% 20 | max_level: 12.2% # Optional - Default 12.5% 21 | ``` 22 | 23 | #### 🛠 The `output:` Component: 24 | ```yaml 25 | output: 26 | - platform: ledc # For ESP8266 use: esp8266_pwm 27 | id: output1 28 | pin: [YOUR GPIO] 29 | frequency: 50 Hz # MUST BE 50Hz 30 | ``` 31 | 32 | #### 🛠 The `number:` (Slider) Component: 33 | ```yaml 34 | substitutions: 35 | name: Servo Tester 36 | ``` 37 | 38 | ```yaml 39 | number: 40 | - platform: template 41 | id: servoSlider1 42 | name: $name 1 43 | icon: mdi:sync 44 | optimistic: true 45 | min_value: -100 46 | max_value: 100 47 | initial_value: 0 48 | step: 5 49 | on_value: 50 | then: 51 | - lambda: !lambda |- 52 | // Servo Write range is: -1.0 to 1.0, so divide by 100 53 | id(servo1).write(x / 100.0); 54 | ``` 55 | ___ 56 | ### Advanced Code-y Bits 57 | ##### 🎁 ADVANCED Part 1: Use `on_boot:` to set position at Start-up: 58 | ```yaml 59 | esphome: 60 | ... 61 | on_boot: 62 | priority: -100 63 | then: 64 | - lambda: !lambda |- 65 | id(servoSlider1).make_call().set_value(0).perform(); 66 | ``` 67 | 68 | ##### 🎁 ADVANCED Part 2-1: SINE Waving Servo 5: 69 | - Use ``interval:`` to Calculate Sine Values: 70 | ```yaml 71 | globals: 72 | - id: sine_wave_phase 73 | type: float 74 | restore_value: no 75 | initial_value: '0.0' 76 | 77 | - id: sine_wave_range 78 | type: float 79 | restore_value: no 80 | initial_value: '0.0' 81 | 82 | - id: sine_wave_speed 83 | type: float 84 | restore_value: no 85 | initial_value: '1.0' 86 | 87 | interval: 88 | - interval: 40ms 89 | then: 90 | if: 91 | condition: 92 | switch.is_on: sinEnable 93 | then: 94 | - lambda: |- 95 | auto t = id(sine_wave_phase); 96 | auto range = id(sine_wave_range); 97 | auto speed = id(sine_wave_speed); 98 | 99 | // Increment phase 100 | t += (2 * PI) / speed; 101 | if (t >= 2 * PI) t = 0; // Reset the phase after a full cycle 102 | 103 | id(sine_wave_phase) = t; 104 | int value = round(range * sin(t)); 105 | 106 | id(servoSlider5).publish_state(value); 107 | 108 | switch: 109 | - platform: template 110 | id: sinEnable 111 | name: $name 5 Enable Sine 112 | optimistic: true 113 | ``` 114 | 115 | ##### 🎁 ADVANCED Part 2-2: SINE Waving Servo 5: 116 | - The Two Extra `number:` Components to control the SINE Values : 117 | ```yaml 118 | - platform: template 119 | id: servoRange 120 | name: $name 5 Range 121 | min_value: 0 122 | max_value: 100 123 | initial_value: 0 124 | step: 5 125 | optimistic: True 126 | on_value: 127 | then: 128 | - lambda: !lambda |- 129 | id(sine_wave_range) = x; 130 | 131 | - platform: template 132 | id: servoSpeed 133 | name: $name 5 Speed 134 | min_value: 0 135 | max_value: 100 136 | initial_value: 5 137 | step: 1 138 | optimistic: True 139 | on_value: 140 | then: 141 | - lambda: !lambda |- 142 | id(sine_wave_speed) = 5 + (100-x); 143 | ``` 144 | ##### 🎁 ADVANCED Part 2-3: SINE Waving Servo 5: 145 | - Stop Spamming the ``logger:``: 146 | ```yaml 147 | logger: 148 | logs: 149 | number: none 150 | servo: none 151 | ``` 152 | --- 153 | 154 |
155 | 156 | ### 💖 Support This Project 157 | 158 | Found this useful? Want to say thanks and fuel future creations? 159 | 160 | **Your support keeps the creativity flowing!** 🍺✨ 161 | 162 | 163 | 164 | 171 | 178 | 179 |
165 | 166 | Buy Me A Coffee 167 |
168 | ☕ Buy me a Coffee 169 |
170 |
172 | 173 | Patreon 174 |
175 | 💖 Join on Patreon 176 |
177 |
180 | 181 | **Every contribution helps bring more awesome projects to life!** 🚀 182 | 183 |
184 | 185 | --- 186 | --------------------------------------------------------------------------------