Servo Motor
#Description
The servo motor is powerful and can be turned to a specific position. It can only be turned around 180 degrees. This could help to build an open/close mechanism or if you need a controlled position.
#Links
#Units
- Percent%
- Degree°
#Functions
#JavaScript / Node.js
ServoMotor component for controlling servo motors. Extends AnalogActor with servo-specific functionality (0-180 degrees).
#Constructor options
| Option | Type | Required | Description | Inherited from |
|---|---|---|---|---|
startAngle | number | optional | — | |
minUs | number | optional | — | |
maxUs | number | optional | — | |
minValue | number | optional | — | AnalogActor |
maxValue | number | optional | — | AnalogActor |
invert | boolean | optional | — | DigitalActor |
pin | number | DigitalPin | required | — | DigitalActor |
#Methods
to(degrees: number)→ thisMove servo to a specific angle (in degrees). Overloads: - to(angle) - Immediate move - to(angle, ms) - Timed move over ms - to(angle, ms, rate) - Timed move with rate control
get angle→ numberGet current angle of the servo. Alias for value getter with servo-specific naming.
sweep()→ thisSweep the servo back and forth. Overloads: - sweep() - Sweep full range (0-180) - sweep(low, high) - Sweep between low and high - sweep(options) - Sweep with full options
stopAfterMs(delayMs: number)→ thisStop any current animation after a delay.
stopAfterSweepCycles(cycles: number)→ thisStop a sweep after N full cycles (low->high->low == 1).
detach()→ thisDetach the servo (stop sending PWM signals).
write(angle: number)→ thisOverride write method to use servo-specific command. Maps to the 'to' method for immediate positioning.
pulse(interval: number = 1000)→ thisfrom AnalogActorPulse the component at a given interval (Arduino-side animation)
fadeTo(targetValue: number, duration: number = 1000)→ thisfrom AnalogActorFade to a target value over time (Arduino-side animation)
on()→ thisfrom DigitalActorTurn the component on (no arguments) or register an event listener.
off()→ thisfrom DigitalActorTurn the component off
toggle()→ thisfrom DigitalActorToggle the component state
set(value: 0 | 1)→ thisfrom DigitalActorSet the value of the component
blink(interval: number = 500)→ thisfrom DigitalActorBlink the component at a given interval (Arduino-side animation)
stop()→ thisfrom DigitalActorStop any running animation (blink, pulse)
get isOn→ booleanfrom DigitalActorCheck if the component is currently on
get isAnimating→ booleanfrom DigitalActorCheck if an animation is running
get value→ number | undefinedfrom ComponentBaseGet the current value of the component.
#Example
import { Board, ServoMotor } from "@blokdots/components";
const board = new Board("/dev/ttyACM0");
await board.open();
const servoMotor = new ServoMotor({ board, pin: "D5" });#C++ (Arduino)
Actor-style wrapper around Arduino Servo library
#Required libraries
#include <Servo.h>— Servo
#Setup
#include "blokdots.h"
ServoMotor myComponent(5);#Constructor
ServoMotor(pin, invert, startAngle, minUs, maxUs)Basic constructor (no EventBus)
| Parameter | Type | Default |
|---|---|---|
pin | uint8_t | required |
invert | bool | false |
startAngle | uint8_t | 90 |
minUs | uint16_t | 544 |
maxUs | uint16_t | 2400 |
ServoMotor(bus, pin, evtChange, invert, startAngle, minUs, maxUs)Constructor with EventBus for trigger support (angle change event)
| Parameter | Type | Default |
|---|---|---|
bus | EventBus | required |
pin | uint8_t | required |
evtChange | uint8_t | required |
invert | bool | false |
startAngle | uint8_t | 90 |
minUs | uint16_t | 544 |
maxUs | uint16_t | 2400 |
#Methods
to(degrees)to(degrees [, ms [, rate]])
| Parameter | Type | Default |
|---|---|---|
degrees | int | required |
to(degrees, ms)| Parameter | Type | Default |
|---|---|---|
degrees | int | required |
ms | uint16_t | required |
to(degrees, ms, rate)| Parameter | Type | Default |
|---|---|---|
degrees | int | required |
ms | uint16_t | required |
rate | uint16_t | required |
write(angle)Keep your existing write() API (manual control cancels animations)
| Parameter | Type | Default |
|---|---|---|
angle | int | required |
angle()→ intemittedValue()→ intFor triggers: current value as int
detach()attached()→ boolsweep()sweep(low, high)| Parameter | Type | Default |
|---|---|---|
low | int | required |
high | int | required |
sweep(low, high, interval, step)| Parameter | Type | Default |
|---|---|---|
low | int | required |
high | int | required |
interval | uint16_t | required |
step | uint8_t | required |
sweep(options)| Parameter | Type | Default |
|---|---|---|
options | const SweepOptions | required |
stopAfterMs(delayMs)Stop any current animation after delayMs (0 disables)
| Parameter | Type | Default |
|---|---|---|
delayMs | uint32_t | required |
stopAfterSweepCycles(cycles)Set to 0 to disable.
| Parameter | Type | Default |
|---|---|---|
cycles | uint16_t | required |
stop()stop() Stop any active animation (and clears auto-stop)