clignotante.py

LattePanda Alpha · File : clignotante.py · Language : Python · Use case : Faire clignoter une LED depuis Python (broche du MCU)

The LattePanda pattern: the Python script runs on the x86 PC, the physical pin lives on the ATmega32U4, and PinPong bridges them in one line. The Arduino world becomes a PC library.

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 LED + 330 Ω resistor on the 50-pin header (MCU pin D13).

Wiring

Anode on the header's D13 pin (see the schematic), 330 Ω resistor, GND on a GND pin of the same header.

How it works

Board().begin() opens the internal serial port to the MCU; Pin(D13).write_digital drives the pin Arduino-style. The PinPong firmware must be flashed on the 32U4 (shipped with the library). 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

Modulate the period with a sensor, or drive two LEDs from two Python threads.

clignotante.py

#!/usr/bin/env python3
# Clignotement LED via PinPong : Python cote hote, GPIO sur l'ATmega32U4.
# Installation : pip install pinpong
from pinpong.board import Board, Pin
from pinpong.extension.unipiot import *   # chargement standard
import time

Board().begin()                    # detecte le MCU (port serie interne)
led = Pin(Pin.D13, Pin.OUT)

print("Clignotement D13 (Ctrl-C pour arreter)")
try:
    while True:
        led.write_digital(1)
        print("ON")
        time.sleep(1)
        led.write_digital(0)
        print("OFF")
        time.sleep(1)
except KeyboardInterrupt:
    led.write_digital(0)
    print("Arret propre")

← Back to the board