pwm-respiration.ino
The R4 core exposes analogWriteFrequency() (like the Due): the LED PWM is pinned at 20 kHz to remove flicker visible on video. The gamma curve makes the breathing look natural.
Hardware
An Arduino UNO R4 WiFi (Renesas RA4M1 @ 14 MHz + companion ESP32-S3 Wi-Fi/BLE, 12x8 LED matrix, RGB LED) powered by USB or 6-24 V. 1 LED + 330 Ω resistor.
Wiring
Anode to D9 through 330 Ω, cathode to GND. Most R4 digital pins are PWM.
How it works
analogWriteFrequency reconfigures the hardware timer, analogWrite applies the 8-bit duty cycle; the loop ramps in a triangle with a 2.2 power curve.
Running it
Arduino IDE 2.x with the « Arduino UNO R4 Boards » core, « Arduino UNO R4 WiFi » board, serial port then Upload. The ESP32-S3 coprocessor updates separately if the IDE asks.
Variants
Drive the setpoint via the DAC or a serial command, or extend to three LEDs (RGB gradient) with three PWM pins.
pwm-respiration.ino
// Respiration LED sur D9 (PWM) avec gamma et frequence reglables.
const int LED = 9;
float niveau = 0, pas = 0.02;
unsigned long t0 = 0;
void setup() {
pinMode(LED, OUTPUT);
analogWriteFrequency(LED, 20000); // 20 kHz : inaudible et LED-friendly
analogWriteResolution(8); // consignes 0..255
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; }
// Gamma 2.2 pour une perception lineaire a l'oeil.
float gamma = powf(niveau, 2.2f);
analogWrite(LED, (int)(gamma * 255));
}