or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

analog-io.mdconnection.mddigital-io.mdevents.mdindex.mdservo.mdsysex.md

servo.mddocs/

0

# Servo Control

1

2

Servo motor control with angle-based positioning for robotics, automation projects, and precise mechanical control applications.

3

4

## Capabilities

5

6

### Servo Position Control

7

8

Control servo motor position by specifying target angle in degrees.

9

10

```javascript { .api }

11

/**

12

* Control servo motor position by angle

13

* Automatically configures pin for servo control

14

* @param pin - Digital pin number connected to servo signal wire

15

* @param angle - Target angle in degrees (0-180)

16

* @param callback - Optional callback function called when position command is sent

17

*/

18

servoWrite(pin: number, angle: number, callback?: Function): void;

19

```

20

21

**Angle Range:**

22

- `0` degrees - Minimum position (typically far counter-clockwise)

23

- `90` degrees - Center/neutral position

24

- `180` degrees - Maximum position (typically far clockwise)

25

26

**Usage Examples:**

27

28

```javascript

29

// Move servo to center position

30

arduino.servoWrite(9, 90);

31

32

// Move servo to minimum position

33

arduino.servoWrite(9, 0);

34

35

// Move servo to maximum position

36

arduino.servoWrite(9, 180);

37

38

// Precise positioning

39

arduino.servoWrite(9, 45); // Quarter turn from center

40

arduino.servoWrite(9, 135); // Three-quarter turn from center

41

42

// With callback

43

arduino.servoWrite(9, 90, function() {

44

console.log('Servo moved to center position');

45

});

46

```

47

48

### Servo Sweep Animation

49

50

Create smooth servo movements and animations.

51

52

```javascript

53

// Simple servo sweep back and forth

54

let angle = 0;

55

let direction = 1;

56

57

setInterval(function() {

58

arduino.servoWrite(9, angle);

59

60

angle += direction * 2;

61

if (angle >= 180 || angle <= 0) {

62

direction = -direction;

63

}

64

}, 50);

65

```

66

67

### Multiple Servo Control

68

69

Control multiple servos independently for complex mechanical systems.

70

71

```javascript

72

// Control multiple servos

73

arduino.servoWrite(9, 45); // Servo 1 to 45 degrees

74

arduino.servoWrite(10, 135); // Servo 2 to 135 degrees

75

arduino.servoWrite(11, 90); // Servo 3 to center

76

77

// Synchronized movement

78

const servoPins = [9, 10, 11];

79

let targetAngle = 0;

80

81

setInterval(function() {

82

// Move all servos to the same angle

83

servoPins.forEach(pin => {

84

arduino.servoWrite(pin, targetAngle);

85

});

86

87

targetAngle += 10;

88

if (targetAngle > 180) {

89

targetAngle = 0;

90

}

91

}, 500);

92

```

93

94

### Sensor-Controlled Servo

95

96

Use sensors to control servo position for responsive mechanical systems.

97

98

```javascript

99

arduino.on('connect', function() {

100

// Control servo position based on potentiometer input

101

arduino.on('analogChange', function(event) {

102

if (event.pin === 0) { // Potentiometer on A0

103

// Map potentiometer reading (0-1023) to servo angle (0-180)

104

const angle = Math.round((event.value / 1023) * 180);

105

arduino.servoWrite(9, angle);

106

console.log(`Servo angle set to ${angle} degrees`);

107

}

108

});

109

});

110

```

111

112

### Robotic Arm Example

113

114

```javascript

115

const ArduinoFirmata = require('arduino-firmata');

116

const arduino = new ArduinoFirmata();

117

118

arduino.connect();

119

120

arduino.on('connect', function() {

121

console.log('Robotic arm controller ready');

122

123

// Define servo pins for robotic arm joints

124

const servos = {

125

base: 9, // Base rotation

126

shoulder: 10, // Shoulder joint

127

elbow: 11, // Elbow joint

128

wrist: 6 // Wrist joint

129

};

130

131

// Move to home position

132

function homePosition() {

133

arduino.servoWrite(servos.base, 90); // Center

134

arduino.servoWrite(servos.shoulder, 45); // Shoulder up

135

arduino.servoWrite(servos.elbow, 135); // Elbow bent

136

arduino.servoWrite(servos.wrist, 90); // Wrist center

137

console.log('Moved to home position');

138

}

139

140

// Predefined positions

141

function pickupPosition() {

142

arduino.servoWrite(servos.base, 90);

143

arduino.servoWrite(servos.shoulder, 120);

144

arduino.servoWrite(servos.elbow, 60);

145

arduino.servoWrite(servos.wrist, 0);

146

console.log('Moved to pickup position');

147

}

148

149

// Initialize to home position

150

setTimeout(homePosition, 1000);

151

152

// Demo sequence

153

setTimeout(function() {

154

pickupPosition();

155

setTimeout(homePosition, 2000);

156

}, 3000);

157

});

158

```

159

160

### Smooth Servo Movement

161

162

Create smooth servo transitions instead of immediate jumps.

163

164

```javascript

165

function smoothServoMove(pin, fromAngle, toAngle, duration, callback) {

166

const steps = Math.abs(toAngle - fromAngle);

167

const stepDelay = duration / steps;

168

const direction = toAngle > fromAngle ? 1 : -1;

169

170

let currentAngle = fromAngle;

171

172

const moveInterval = setInterval(function() {

173

arduino.servoWrite(pin, currentAngle);

174

currentAngle += direction;

175

176

if (currentAngle === toAngle) {

177

clearInterval(moveInterval);

178

if (callback) callback();

179

}

180

}, stepDelay);

181

}

182

183

// Usage: move servo from 0 to 180 degrees over 2 seconds

184

smoothServoMove(9, 0, 180, 2000, function() {

185

console.log('Smooth movement completed');

186

});

187

```

188

189

### Servo Position Feedback

190

191

While standard servos don't provide position feedback, you can track commanded positions.

192

193

```javascript

194

class ServoController {

195

constructor(arduino, pin) {

196

this.arduino = arduino;

197

this.pin = pin;

198

this.currentAngle = 90; // Assume starting at center

199

}

200

201

moveTo(angle, callback) {

202

if (angle < 0 || angle > 180) {

203

console.error('Servo angle must be between 0 and 180 degrees');

204

return;

205

}

206

207

this.arduino.servoWrite(this.pin, angle, () => {

208

this.currentAngle = angle;

209

console.log(`Servo on pin ${this.pin} moved to ${angle} degrees`);

210

if (callback) callback();

211

});

212

}

213

214

getCurrentAngle() {

215

return this.currentAngle;

216

}

217

218

moveRelative(deltaAngle, callback) {

219

const newAngle = Math.max(0, Math.min(180, this.currentAngle + deltaAngle));

220

this.moveTo(newAngle, callback);

221

}

222

}

223

224

// Usage

225

const servo1 = new ServoController(arduino, 9);

226

servo1.moveTo(45);

227

servo1.moveRelative(30); // Move 30 degrees from current position

228

```

229

230

## Constants

231

232

```javascript { .api }

233

// Pin mode for servo control

234

ArduinoFirmata.SERVO = 4; // Servo control mode

235

```

236

237

## Servo Specifications

238

239

### Standard Servo Characteristics

240

- **Control Signal**: PWM signal with 20ms period (50Hz)

241

- **Pulse Width Range**: 1ms (0°) to 2ms (180°), with 1.5ms for center (90°)

242

- **Angle Range**: 0° to 180° (some servos may have different ranges)

243

- **Response Time**: Typically 0.1 to 0.2 seconds per 60° movement

244

- **Torque**: Varies by servo model (measured in kg⋅cm or oz⋅in)

245

246

### Power Requirements

247

- **Voltage**: Most servos operate at 4.8V to 6V

248

- **Current**: Varies by servo size and load (typically 100mA to 2A+)

249

- **Power Supply**: External power supply recommended for multiple or high-torque servos

250

251

### Wiring

252

Standard servo connections:

253

- **Red wire**: Power (4.8V - 6V)

254

- **Black/Brown wire**: Ground (GND)

255

- **Yellow/White/Orange wire**: Signal (connect to Arduino digital pin)

256

257

## Compatible Pins

258

259

Most Arduino digital pins can control servos, but PWM-capable pins provide the most reliable control:

260

261

### Arduino UNO Servo Pins

262

- **Pins 3, 5, 6, 9, 10, 11**: PWM-capable pins (recommended)

263

- **Pins 2, 4, 7, 8, 12, 13**: Digital pins (also supported)

264

265

### Simultaneous Servo Limit

266

- Arduino UNO can typically control 8-12 servos simultaneously

267

- More servos may require external servo controller boards

268

- Each servo adds processing overhead to the Arduino

269

270

## Servo Types

271

272

### Standard Servos

273

- **Rotation**: 0° to 180° (approximately 180° of travel)

274

- **Applications**: Robotic arms, camera mounts, control surfaces

275

- **Control**: Position-based control

276

277

### Continuous Rotation Servos

278

- **Rotation**: Full 360° continuous rotation

279

- **Control**: Speed and direction rather than position

280

- **Applications**: Robot wheels, conveyor systems

281

282

**Note**: Continuous rotation servos use the same `servoWrite()` function, but angle values control speed and direction rather than position.

283

284

## Common Applications

285

286

### Camera Gimbal

287

```javascript

288

// Two-axis camera gimbal control

289

const panServo = 9; // Horizontal pan

290

const tiltServo = 10; // Vertical tilt

291

292

// Center gimbal

293

arduino.servoWrite(panServo, 90);

294

arduino.servoWrite(tiltServo, 90);

295

296

// Smooth pan movement

297

let panAngle = 90;

298

setInterval(function() {

299

arduino.servoWrite(panServo, panAngle);

300

panAngle += 2;

301

if (panAngle > 160) panAngle = 20;

302

}, 100);

303

```

304

305

### Robotic Joint Control

306

```javascript

307

// Multi-joint robot control with sensor input

308

arduino.on('analogChange', function(event) {

309

switch(event.pin) {

310

case 0: // Joystick X-axis controls base rotation

311

const baseAngle = Math.round((event.value / 1023) * 180);

312

arduino.servoWrite(9, baseAngle);

313

break;

314

case 1: // Joystick Y-axis controls arm elevation

315

const armAngle = Math.round((event.value / 1023) * 180);

316

arduino.servoWrite(10, armAngle);

317

break;

318

}

319

});

320

```

321

322

## Best Practices

323

324

### Servo Control Guidelines

325

1. **Power Supply**: Use external power for multiple or high-torque servos

326

2. **Startup**: Allow servos to reach position before sending new commands

327

3. **Speed**: Don't send position updates too frequently (limit to ~20Hz)

328

4. **Range**: Stay within 0-180° range to avoid servo damage

329

5. **Wiring**: Keep signal wires short and away from power wires to reduce noise

330

331

### Performance Optimization

332

```javascript

333

// Throttle servo updates to avoid overwhelming the servo

334

let lastServoUpdate = 0;

335

const SERVO_UPDATE_INTERVAL = 50; // 50ms minimum between updates

336

337

function updateServo(pin, angle) {

338

const now = Date.now();

339

if (now - lastServoUpdate > SERVO_UPDATE_INTERVAL) {

340

arduino.servoWrite(pin, angle);

341

lastServoUpdate = now;

342

}

343

}

344

```