🛠 Servo moteur sur PWM0 (GPIO18) : 50 Hz et 2,5 à 12,5 %

Raspberry Pi Compute Module 1 (CM1) · intermediaire · Schéma : IMG-cm1-intermediaire-1-servo-pwm-gpio18.svg — Un servo moteur branché sur GPIO18 du Compute Module 1, avec le chronogramme PWM 50 Hz annoté des impulsions 0,5 ms à 2,5 ms correspondant à 2,5 % à 12,5 % de rapport cyclique

Objectives

Control a standard angle servo using the CM1's hardware PWM. On the BCM2835, the PWM0 channel is routed to GPIO18 via alternate function. A standard servo expects a 20 ms period (50 Hz) and reads position based on pulse width: approximately 1 ms at center, with the useful range extending from 0.5 ms (2.5% duty cycle) to 2.5 ms (12.5%). Configure the PWM via the pwm device tree overlay, then use sysfs/pigpio, avoiding software PWM which suffers from jitter.

Hardware

Complete Code

# Activer le PWM matériel
echo 'dtoverlay=pwm-28' | sudo tee -a /boot/config.txt
sudo reboot

# Pilotage pigpio (daemon PWM matériel)
sudo pigpiod
pigs p 18 0            # arrêt
pigs p 18 64           # ~1,56 % -> butée gauche (50 Hz)
pigs p 18 128          # ~3,1 % -> centre
pigs p 18 256          # ~6,25 %
pigs p 18 512          # ~12,5 % -> butée droite

# Balayage doux
for d in 64 96 128 160 192 224 256; do pigs p 18 $d; sleep 0.3; done

Expected Verification

The servo moves decisively to each setpoint and holds its position without excessive buzzing. The useful range extends between pigs p 18 64 (0.5 ms/2.5%) and pigs p 18 512 (2.5 ms/12.5%) for an 8-bit output at 50 Hz; beyond 12.5%, the servo hits its mechanical stop and draws excessive current. Hardware PWM does not exhibit the dropouts associated with software methods during SD card access.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials