How to Program Multiple Servos in a Scene with Home Assistant
The world of home automation has long been dominated by static devices—lights that turn on, switches that flip, and thermostats that adjust. But what if your smart home could move? What if it could wave, point, lift, or tilt? This is the exciting frontier unlocked by the humble micro servo motor. These tiny, precise actuators are the muscles of the DIY automation world, and when orchestrated in groups through Home Assistant, they can bring scenes to life with dynamic, physical motion. This guide will walk you through the concepts, hardware, and code needed to program multiple servos to perform coordinated actions within your automated scenes.
Why Micro Servos? The Magic of Precise, Programmable Motion
Unlike standard DC motors that simply spin, a micro servo is a closed-loop system. You send it a target position (typically as a pulse width), and its internal circuitry and gearing work to move and hold that exact angle, usually within a 180-degree range. This makes them incredibly useful for applications requiring controlled movement: * Animated Displays: Wave a tiny flag on a holiday, tilt a sign, or animate a model's arm. * Physical Indicators: Point a needle on a custom gauge for stock prices, weather data, or energy usage. * Interactive Projects: Open a tiny door for a pet feeder, pan a small camera, or create a moving "attention" device for notifications. * Seasonal Decor: Create spooky tombstones that tilt or festive characters that perform a sequence.
Their low power consumption (often running on 5V), compact size (some as small as a quarter), and ease of programming make them the perfect actuator for adding kinetic elements to your Home Assistant ecosystem.
Building the Foundation: Hardware and Connectivity
Before writing a single line of YAML, you need a solid hardware setup. Controlling multiple servos directly from a Home Assistant server (like a Raspberry Pi) is not advisable due to power and pin limitations. The standard and most reliable approach involves a dedicated microcontroller.
The Essential Hardware Stack
- Micro Servo Motors (SG90/MG90S): These are the workhorses. SG90s are incredibly common and cheap for light-duty tasks. For more torque and metal gears, step up to the MG90S.
- Microcontroller (ESP32/ESP8266): An ESP32 is highly recommended for multiple servos. It has more pins, processing power, and built-in Wi-Fi. The ESP8266 (like a D1 Mini) can work for 2-3 servos.
- Power Supply: This is critical. Do not power servos from the microcontroller's USB port. A servo in motion can draw significant current, causing brownouts or resets. Use a dedicated 5V power supply (like a bench supply or a sturdy USB adapter) for the servos. Ensure the grounds (GND) of the microcontroller and the servo power supply are connected.
- PCA9685 Servo Driver (Optional but Recommended): For scenes involving more than 4-5 servos, this I2C-based 16-channel driver is a game-changer. It handles all the pulse generation offloading work from the microcontroller and provides excellent, jitter-free control. It also simplifies wiring and power management.
The Connectivity Path: ESPHome
The easiest way to integrate this hardware into Home Assistant is ESPHome. It compiles custom firmware for your ESP32/8266, exposing the servos as native Home Assistant entities (like number or switch entities for position control). The configuration is done in simple, powerful YAML files.
Crafting the Code: ESPHome Configuration for Multiple Servos
Here is a sample servo_scene_controller.yaml configuration for an ESP32, controlling three servos directly and outlining the PCA9685 method.
yaml esphome: name: servo-scene-controller platform: ESP32 board: nodemcu-32s
wifi: ssid: !secret wifissid password: !secret wifipassword
Enable logging and the Home Assistant API
api: encryption: key: !secret apiencryptionkey
ota: password: !secret ota_password
For servos connected directly to the ESP32 GPIO pins
servo: - id: servoshoulder output: pwmoutput1 - id: servoelbow output: pwmoutput2 - id: servowrist output: pwmoutput3
output: # Define PWM outputs for specific GPIO pins - platform: ledc id: pwmoutput1 pin: GPIO16 frequency: 50Hz # Standard servo PWM frequency - platform: ledc id: pwmoutput2 pin: GPIO17 frequency: 50Hz - platform: ledc id: pwm_output3 pin: GPIO18 frequency: 50Hz
Expose each servo as a number entity in Home Assistant (0° to 180°)
number: - platform: servo name: "Shoulder Position" id: shouldercontrol servoid: servoshoulder minvalue: 0 maxvalue: 180 step: 1 - platform: servo name: "Elbow Position" id: elbowcontrol servoid: servoelbow minvalue: 0 maxvalue: 180 step: 1 - platform: servo name: "Wrist Position" id: wristcontrol servoid: servowrist minvalue: 0 max_value: 180 step: 1
Configuration for High-Channel Counts: The PCA9685
For a 16-servo holiday display, the ESPHome configuration becomes even cleaner with the PCA9685.
yaml
... (esphome, wifi, api sections remain the same) ...
i2c: sda: GPIO21 scl: GPIO22 scan: true
Configure the PCA9685
pca9685: id: pca_board address: 0x40 # Default I2C address
servo: - id: servochannel0 pca9685id: pcaboard channel: 0 minlevel: 5.5% maxlevel: 10.5% # Calibrate these for your specific servos - id: servochannel1 pca9685id: pcaboard channel: 1 # ... repeat for channels 2 through 15 ...
number: - platform: servo name: "Snowman Wave Arm" servoid: servochannel_0 # ... number entity configuration ...
After flashing this to your device, the servos will appear as controllable entities in your Home Assistant interface.
Choreographing Motion: Creating Scenes and Automations
With the servos available as entities, the real fun begins—orchestrating their movement. You won't use Home Assistant's built-in Scene feature directly, as it captures static states, not timed sequences. Instead, you use scripts and automations.
Writing a Script for Coordinated Movement
A script allows you to define a sequence of actions. Here, we create a waving motion for a two-servo arm (shoulder and elbow).
yaml
In your scripts.yaml or within a script: section in an automation
wavehello: mode: restart # If triggered again, restart the sequence sequence: # Move to initial position quickly - service: number.setvalue target: entityid: number.shoulderposition data: value: 90 - service: number.setvalue target: entityid: number.elbow_position data: value: 30 - delay: 0.5s
# Wave sequence - repeat a pattern - repeat: count: 3 sequence: - service: number.set_value target: entity_id: number.shoulder_position data: value: 120 - delay: 0.3s - service: number.set_value target: entity_id: number.shoulder_position data: value: 60 - delay: 0.3s # Return to rest position - service: number.set_value target: entity_id: number.shoulder_position data: value: 90 - service: number.set_value target: entity_id: number.elbow_position data: value: 90 Triggering Servo Scenes with Automations
Now, tie that script to any Home Assistant event to create a true "scene."
yaml
In your automations.yaml alias: "Wave Hello When Doorbell Rings" trigger: platform: state entityid: binarysensor.frontdoorbell to: "on" action: service: script.wavehello
alias: "Animate Weather Indicator at 9 AM" trigger: platform: time at: "09:00:00" action:
A more complex script could check weather conditions and set a servo to point to "Sunny", "Rainy", or "Cloudy" on a dial.
service: script.setweatherdial
Advanced Sequencing with Lambdas in ESPHome
alias: "Wave Hello When Doorbell Rings" trigger: platform: state entityid: binarysensor.frontdoorbell to: "on" action: service: script.wavehello
alias: "Animate Weather Indicator at 9 AM" trigger: platform: time at: "09:00:00" action:
A more complex script could check weather conditions and set a servo to point to "Sunny", "Rainy", or "Cloudy" on a dial.
service: script.setweatherdial
For ultra-smooth, complex, or perfectly timed sequences that shouldn't rely on network calls, you can write lambda functions directly in your ESPHome configuration. This code runs on the microcontroller itself.
yaml
Inside an ESPHome switch or binary_sensor definition to trigger the sequence
switch: - platform: template name: "Perform Startup Sequence" turnonaction: then: # This lambda directly controls the servo components - lambda: |- id(servoshoulder).write(90.0); delay(500); id(servoelbow).write(150.0); delay(300); for (int i = 0; i < 5; i++) { id(servowrist).write(30.0); delay(200); id(servowrist).write(150.0); delay(200); } id(servoshoulder).write(0.0); id(servoelbow).write(0.0); id(servo_wrist).write(90.0);
Pro Tips for Reliable Multi-Servo Performances
- Power is Paramount: Always use a dedicated, high-current (2A+) 5V supply for your servo bank. Capacitors (e.g., a 1000µF electrolytic across the power rails near the servos) can smooth out sudden current draws.
- Calibrate Each Servo: Not all servos are mathematically identical. Use the
min_levelandmax_levelparameters in ESPHome to map the 0-180° range to the actual pulse widths your specific servos respond to (often between 500µs and 2500µs). - Mind the Wiring: Use a common ground. Keep signal wires away from power wires if possible. For longer runs, consider using twisted pair or shielded cable.
- Avoid "Servo Jitter": Jitter (small, nervous movements) is often caused by power supply noise or PWM signal interference. The PCA9685 drastically reduces this. Ensure your power supply is clean and well-regulated.
- Start Simple: Test each servo individually in ESPHome's "Call Service" tab (
number.set_value) before writing complex scripts. This isolates hardware vs. software issues.
By combining the precise physical control of micro servos with the vast integration and scheduling capabilities of Home Assistant, you move beyond a smart home into the realm of a living home. The gentle wave of a figurine, the precise turn of a dial, or the synchronized motion of a display—these are the touches that transform automation from mere convenience into wonder and delight.
Copyright Statement:
Author: Micro Servo Motor
Source: Micro Servo Motor
The copyright of this article belongs to the author. Reproduction is not allowed without permission.
Recommended Blog
- Light Switch Automation Using Micro Servos: Low-Cost Smart Hack
- Automated Doorbell Covers or Flaps with Micro Servos
- Using Micro Servos for Automated Door Locks in Smart Homes
- Servo-Controlled Sliding Doors in Furniture: Design Tips
- Using Micro Servos to Automate Fireplace Screens or Shades
- Best Practices for Wiring and Power Management in Servo-Driven Smart Devices
- Exploring Low-Cost Micro Servos for Smart Home Prototyping
- Automated HVAC Vent Louvers Using Micro Servos
- Quiet Servo Motors: Choosing Low Noise Micro Servos for Home Devices
- Security Covers for Keypads or Cameras Controlled by Servos
About Us
- Lucas Bennett
- Welcome to my blog!
Hot Blog
- The Role of Micro Servo Motors in Smart Retail Systems
- Advances in Signal Processing for Micro Servo Motors
- Future Micro Servo Types: Trends & Emerging Technologies
- The Role of Micro Servo Motors in Smart Home Devices
- The Importance of Gear Materials in Servo Motor Performance Under Varying Accelerations
- The Relationship Between Signal Width and Motor Angle
- Understanding Pulse Width Modulation for Servo Control on Raspberry Pi
- Case Study: Micro Servos in Delivery Drone Drop Mechanism
- Micro Servo MOSFET Drivers: Improving Efficiency in Drone Circuits
- Bias to Torque Ratings: kg-cm, N-cm, oz-in and Unit Conversions
Latest Blog
- How to Program Multiple Servos in a Scene with Home Assistant
- Firmware Upgrades and Calibration Tools for Micro Servos in Drones
- Designing a Micro Servo Robotic Arm for Recycling Applications
- How Torque and Speed Affect Motor Performance
- The Relationship Between Motor Torque and Starting Current
- The Impact of Gear Materials on Servo Motor Performance Under Varying Signal Fidelity
- Using Micro Servos for Light Indicators or Marker Drops in Survey Drones
- How to Implement Heat Shields in Motor Design
- Synchronizing Multiple Micro Servos in RC Airplanes’ Flap Systems
- The Role of Micro Servo Motors in Autonomous Delivery Systems
- Micro Servo Motors in Smart Religious Systems: Enhancing Efficiency and Control
- Understanding the Role of the Electronic Speed Controller in RC Cars
- Specification of Mechanical Angle vs Electrical Angle (in some designs)
- The Importance of Gear Materials in Servo Motor Performance Under Varying Signal Distortions
- How to Find Quality Micro Servo Motors on a Budget
- Understanding Impedance Matching in PCB Layouts
- Using Metal Case Micro Servos in RC Boat Rudders
- Using Micro Servos for Multi-camera Swivel Pods on Survey Drones
- Micro Servo Motor Lifespan: What Drone Hobbyists Should Know
- How to Build a Remote-Controlled Car with Line Following Capabilities