pont_serie.py
The 32U4 stays an autonomous Arduino: this PWM-control sketch, driven by pyserial from the host, shows the duo in raw form — no library, protocol of your choice. The bridge every project ends up using.
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 actuator on D9 (LED + 330 Ω, or transistor + lamp).
Wiring
The MCU's internal serial port appears as COM3 (Windows) or an internal tty (Linux) — check Device Manager or dmesg.
How it works
The MCU sketch reads a whole line, maps 0-100 to 8-bit PWM and acknowledges; Python writes/reads in mirror. The 2 s sleep covers the 32U4 USB reset on port open. 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
A JSON protocol on this channel, or a watchdog: no command for 5 s and the MCU cuts the PWM.
pont_serie.py
#!/usr/bin/env python3
# Cote MCU (a televerser d'abord, carte « Arduino Leonardo ») :
# void setup(){ Serial.begin(9600); }
# void loop(){
# if (Serial.available()){
# int v = Serial.readString().toInt();
# analogWrite(9, map(v, 0, 100, 0, 255)); // consigne 0-100 -> PWM D9
# Serial.print("recu "); Serial.println(v);
# }
# }
# Cote hote : envoyer des consignes et lire les accusés.
import serial, time
port = serial.Serial("COM3", 9600, timeout=1) # port du MCU (Linux : /dev/ttyS*)
time.sleep(2) # le 32U4 se reinitialise a l'ouverture
for consigne in range(0, 101, 10):
port.write(f"{consigne}\n".encode())
reponse = port.readline().decode().strip()
print(f"consigne {consigne:3d} -> MCU: {reponse}")
time.sleep(0.3)
port.close()