Servo Motion Control
Implement smooth angular motion. This project demonstrates how to use 16-bit Pulse Width Modulation (PWM) to control the precise position of a virtual micro-servo motor.
Project Goal: Configure a 50Hz PWM signal on GP15. Sweep the duty cycle between 1638 (0°) and 8192 (180°) to create a continuous back-and-forth motion.
1. Wiring Blueprint
| From Component | Servo Wire | Pico Pin | Notes |
|---|---|---|---|
| SG90 Servo | Signal (Orange) | GP15 | 50Hz PWM Required |
| SG90 Servo | VCC (Red) | 3V3 | 3.3V Power Rail |
| SG90 Servo | GND (Brown) | GND | Common Ground |
2. JavaScript Implementation
The SDK allows for direct 16-bit duty cycle manipulation, providing ultra-smooth movement.
JavaScript (SDK)
import intarva, { PWM, Pin, sleep } from 'intarva';
const srv = new PWM('GP15');
srv.freq(50); // Standard 50Hz for Servos
async function run() {
console.log("Servo Motion: Initializing sweep...");
const min_duty = 1638; // 0 degrees
const max_duty = 8192; // 180 degrees
const step = 200; // Resolution of movement
while (true) {
// Sweep Forward
for (let duty = min_duty; duty < max_duty; duty += step) {
srv.duty_u16(duty);
await sleep(50);
}
// Sweep Backward
for (let duty = max_duty; duty > min_duty; duty -= step) {
srv.duty_u16(duty);
await sleep(50);
}
}
}
intarva.on('ready', run);