Servo Shield

Il Servo Shield pilota fino a otto servocomandi hobbistici in parallelo dalla OpenMV Cam tramite I2C, usando un controller servo / PWM PCA9685.

Servo Shield

Per il datasheet completo, le foto e l’acquisto consulta la pagina prodotto del Servo Shield.

Punti salienti

  • Controller servo / PWM PCA9685

  • Otto canali servo indipendenti tramite I2C

  • Impilabile con il Motor Shield e il Pan and Tilt Shield

Pinout

Pinout del Servo Shield

Riferimento dei pin

Pin

Funzione

P4

I²C SCL — clock verso il PCA9685

P5

I²C SDA — dati verso il PCA9685

Linea VIN

Alimenta i servocomandi (dal pin VIN della camera)

Linea 3.3V

Alimenta la logica del PCA9685

Linea GND

Massa comune tra servocomandi e camera

L’indirizzo I²C predefinito è 0x40. Collega il ponticello a saldare integrato per spostare l’indirizzo a 0x60.

Nota

Lo shield preleva l’alimentazione dei servocomandi direttamente dal pin VIN della camera. Su nessuna OpenMV Cam l’USB alimenta VIN, quindi VIN deve essere fornito esternamente (batteria, alimentatore da banco o simili): scegli una sorgente dimensionata per la corrente di stallo combinata di tutti i servocomandi che intendi pilotare.

Utilizzo

Pilota gli otto canali servo tramite il PCA9685 su I²C. L’intervallo di larghezza dell’impulso varia tra i servocomandi, quindi regola MIN_US e MAX_US in base ai tuoi — i valori tipici sono intorno a 1000–2000 µs:

import time
from machine import SoftI2C, Pin


class PCA9685:
    """Minimal PCA9685 driver — 12-bit PWM on any of 8 channels."""

    def __init__(self, bus, address=0x40, freq=50):
        self._bus = bus
        self._addr = address
        bus.writeto_mem(address, 0x00, b"\x00")            # reset Mode1
        prescale = round(25_000_000 / (4096 * freq)) - 1
        bus.writeto_mem(address, 0x00, b"\x10")            # sleep
        bus.writeto_mem(address, 0xFE, bytes([prescale]))  # prescale
        bus.writeto_mem(address, 0x00, b"\x00")            # wake
        time.sleep_us(5)
        bus.writeto_mem(address, 0x00, b"\xA1")            # restart + AI + allcall
        self._period_us = 1_000_000 // freq

    def set_duty(self, channel, duty):
        duty &= 0xFFF                                      # 12-bit
        if duty == 0:
            on, off = 0, 0x1000                            # FULL_OFF
        elif duty == 0xFFF:
            on, off = 0x1000, 0                            # FULL_ON
        else:
            on, off = 0, duty
        self._bus.writeto_mem(
            self._addr, 0x06 + 4 * channel,
            bytes([on & 0xFF, on >> 8, off & 0xFF, off >> 8]))

    def set_us(self, channel, pulse_us):
        self.set_duty(channel, (pulse_us * 4096) // self._period_us)


MIN_US = 1000  # full-left pulse width (microseconds)
MAX_US = 2000  # full-right pulse width

bus = SoftI2C(scl=Pin("P4"), sda=Pin("P5"))
pca = PCA9685(bus, address=0x40, freq=50)


def angle(channel, deg):
    pca.set_us(channel, MIN_US + (deg * (MAX_US - MIN_US)) // 180)


while True:
    for ch in range(8):
        angle(ch, 0)
    time.sleep_ms(2000)
    for ch in range(8):
        angle(ch, 180)
    time.sleep_ms(2000)

Il PCA9685 gestisce anche un PWM generico a 12 bit a qualsiasi frequenza — riutilizza la stessa classe con set_duty (0–4095) per, ad esempio, far dissolvere un LED sul canale 0 a 1 kHz. L’helper qui sotto scala un float 0,0–100,0% sull’intervallo di duty 0–4095 del chip:

import time
from machine import SoftI2C, Pin

bus = SoftI2C(scl=Pin("P4"), sda=Pin("P5"))
pca = PCA9685(bus, address=0x40, freq=1000)


def brightness(channel, pct):
    pca.set_duty(channel, int(pct * 4095 / 100))


while True:
    # Ramp up 0 → 100%.
    for pct in range(101):
        brightness(0, float(pct))
        time.sleep_ms(20)
    # Ramp down 100 → 0%.
    for pct in reversed(range(101)):
        brightness(0, float(pct))
        time.sleep_ms(20)