pwm-respiration.ino
The H7 timers reach PWM frequencies far beyond AVRs: 100 kHz here, ideal for filtering into real analog voltage with a plain RC. The gamma curve keeps the breathing natural.
Hardware
An Arduino Portenta H7 (ST STM32H747 dual-core module Cortex-M7 @ 480 MHz + M4 @ 240 MHz, 8 MB SDRAM, high-density connector), powered by USB-C or 5 V. 1 LED + 330 Ω resistor (pins are 3.3 V).
Wiring
LED + 330 Ω between D9 and GND. For a smooth analog voltage: RC 10 kΩ / 100 nF after the pin.
How it works
analogWriteFrequency reconfigures the Mbed timer; analogWrite applies the duty cycle. The loop ramps in a triangle with gamma 2.2 every 10 ms.
Running it
Arduino IDE with the « Arduino Mbed OS Portenta Boards » core: pick « Portenta H7 » (M7 core), the USB-C port, then Upload. The DFU mode M7/M4 selector also loads the M4 core.
Variants
Use the real DAC (A0 output) for a continuous voltage, or drive a DC motor with 20 kHz PWM.
pwm-respiration.ino
// Respiration LED sur PWM D9, frequence relevee a 100 kHz.
const int LED = 9;
float niveau = 0, pas = 0.02;
unsigned long t0 = 0;
void setup() {
pinMode(LED, OUTPUT);
analogWriteFrequency(LED, 100000); // 100 kHz : hors spectre audible
Serial.begin(9600);
}
void loop() {
if (millis() - t0 < 10) return;
t0 = millis();
niveau += pas;
if (niveau >= 1.0f) { niveau = 1.0f; pas = -pas; }
if (niveau <= 0.0f) { niveau = 0.0f; pas = -pas; }
analogWrite(LED, (int)(powf(niveau, 2.2f) * 255));
}