🛠 Servo moteur sur PWM0 (GPIO18) : 50 Hz et 2,5 à 12,5 %
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
- A Compute Module 1 on an IO Board
- 1 angle servo (SG90/MG90S) powered separately at 5 V if high torque is required
- GPIO18 (pin 12 on J6) connected to the signal wire, with common grounds
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
- Using software PWM (
pwmmode without hardware): micro-interruptions cause the servo to jitter; verify that the overlay is active (ls /sys/class/pwm). - Powering the servo from the CM1's 3.3 V rail: insufficient current causes the SoC to reset when the motor starts; use an external 5 V supply with a common ground.
- Duty cycle out of range: above 12.5% or below 2.5%, the servo stalls at its limits and overheats.
- Confusing PWM0 (GPIO18) and PWM1 (GPIO13): the alternate function pinout differs; consult the BCM2835 function table.
- Forgetting the common ground between the servo power supply and the CM1: the servo vibrates randomly.
Other tutorials for this board
- Flasher l'eMMC du CM1 avec rpiboot et le jumper J4
- Premier boot : Bullseye legacy, rails 3,3 V et ordre des alimentations
- Première LED sur GPIO17 : banques, courants et broches réservées
- BME280 sur I2C0 (GPIO2/3) : activer i2c_arm et lire l'ID 0x60
- Caméra officielle sur le connecteur FPC CSI du CM1
- Boot sans EEPROM : fallback USB et bootcode.bin sur eMMC
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash