pont_serie.py

LattePanda 3 Delta (id: lattepanda-3) · File : pont_serie.py · Language : Python · Use case : Téléverser un sketch Arduino et lui parler par série

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 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 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. 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

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()

← Back to the board