servo.py

LattePanda 3 Delta (id: lattepanda-3) · File : servo.py · Language : Python · Use case : Piloter un servo depuis le PC

The servo on the header obeys a Python function: the library translates angle into 50 Hz PWM on the 32U4's timer. The PC adds what Arduino cannot: camera, AI, database.

Hardware required

A LattePanda 3 Delta (x86 Intel N5105, Windows 10/11 or Ubuntu) with its Arduino-compatible co-processor (ATmega32U4) wired to the two 50-pin headers. 1 SG90 servo + external 5 V supply (never the header 5 V beyond a micro servo).

Wiring

Signal on the 50-pin header's D9, + to the external supply, grounds common (header GND tied to the supply's).

How it works

Pin.PWM_OCR configures the MCU's hardware timer; the angle function converts degrees → duty cycle. The serial bridge adds ~1 ms, invisible to the mechanics. Host side: pip install pinpong then python3. MCU side: the ATmega32U4 shows up as a COM port — program it with the Arduino IDE (« Arduino Leonardo » board).

Going further

Face-tracking servo with OpenCV (the real point of the x86+MCU duo).

servo.py

#!/usr/bin/env python3
# Servo d'angle sur D9 — PinPong commande le PWM du 32U4.
from pinpong.board import Board, Pin
from pinpong.libs.adc_stepper import *     # modules PWM/servo inclus
import time

Board().begin()
servo = Pin(Pin.D9, Pin.PWM_OCR)

def angle(degres):                      # 0..180 -> duty 2,5..12,5 %
    duty = 25 + int(degres * 100 / 180)  # base 1000 = 100 %
    servo.write_analog(duty * 21)        # 10 bits : 0..1023

print("Balayage 0-180-0")
for a in list(range(0, 181, 10)) + list(range(180, -1, -10)):
    angle(a)
    print(f"{a} degres")
    time.sleep(0.08)

← Back to the board