🛠 MicroPython sur la STM32F4DISCOVERY : REPL interactif et pilotage des LED

STM32F4DISCOVERY (STM32F407VGT6 Discovery kit) · debutant · Schéma : la STM32F4DISCOVERY reliée par son port micro-USB OTG FS à un ordinateur ; sur l'écran, une fenêtre de REPL MicroPython affichant le prompt >>> et la commande led.on() ; les quatre LED PD12 à PD15 sont annotées.

Objective

Turn the STM32F4DISCOVERY into an interactive Python interpreter using the official STM32F4DISC port of MicroPython. The REPL runs over USB (CDC serial on the OTG FS, enabled by MICROPY_HW_USB_FS in the board configuration): unlike Arduino mode, the USB serial port works immediately here, without an external cable. The port configuration also enables the DAC, the hardware random number generator, and servos.

Hardware

Installation

  1. Get the STM32F4DISC firmware (published builds or compilation in ports/stm32 with make), official source: ports/stm32/boards/STM32F4DISC from the micropython repository.
  2. Flash it into the 1024 KB flash of the STM32F407VGT6 via the embedded ST-LINK.
  3. Reconnect to the OTG port: a USB serial port appears; open it at 115200 baud (Thonny, mpremote, picocom...).

Complete code

# Dans le REPL (Ctrl-D puis coller) ou dans un fichier main.py :
from machine import Pin
import time

# Broche verifiee dans ports/stm32/boards/STM32F4DISC/mpconfigboard.h
leds = [Pin('PD12', Pin.OUT),  # verte
        Pin('PD13', Pin.OUT),  # orange
        Pin('PD14', Pin.OUT),  # rouge
        Pin('PD15', Pin.OUT)]  # bleue
user = Pin('PA0', Pin.IN)      # bouton USER, actif a l'etat haut

while True:
    for led in leds:
        led.on; time.sleep_ms(120); led.off
    print("btn:", user.value)

Two hardware specifics to keep in mind from the start

Expected verification

MicroPython v... on ...; STM32F407 with STM32F407VG is displayed when connecting the OTG port. The program runs the four LEDs and displays btn: 0 or btn: 1 depending on the button state. Press Ctrl-C to interrupt the loop and return to the prompt.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials