servo.py

LattePanda Alpha · 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 Alpha (x86 Intel m3-7Y30, Windows 10 or Linux) with its ATmega32U4 co-processor, Arduino Leonardo compatible, on the 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. The ATmega32U4 is a genuine Leonardo: flash it as « Arduino Leonardo » in the IDE through its internal COM port. Host-side, DFRobot's lattepanda Python library talks to it over serial.

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