pwm-respiration.ino

Particle Boron · File : pwm-respiration.ino · Language : Arduino · Use case : Respiration de LED par PWM avec luminosité réglable à distance

A PWM LED breathes gently toward a target brightness changed remotely via Particle.function. The exponential smoothing avoids hard jumps, and the sinusoidal beat gives a lively look.

Hardware

A Particle Boron module (nRF52840, Wi-Fi + Bluetooth LE, cellular variant where applicable) with on-board antenna. 1 LED + 330 Ω resistor (the module pins are 3.3 V).

Wiring

LED anode to D0 through 330 Ω, cathode to GND. D0 is a PWM output on the module.

How it works

analogWrite applies the duty cycle; the loop filters the setpoint (5 % smoothing per 10 ms step) and modulates amplitude with a sine. regler() updates the target from the cloud.

Running it

Install the Particle CLI (particle setup), flash with particle flash --serial or OTA (particle flash <name> firmware.bin). The Device Cloud app or REST API complete the deployment.

Variants

Chain several channels for ambient lighting, or close the loop on a light sensor (LDR on A0).

pwm-respiration.ino

// Respiration LED sur D0 (broche PWM) + reglage via Particle.function.
const int LED = D0;   // sortie PWM (timer) sur le module

int niveauCible = 128;   // 0..255, reglable a distance
float niveau = 0;
unsigned long derniereEtape = 0;

int regler(String args) {
  int v = args.toInt();
  if (v >= 0 && v <= 255) { niveauCible = v; return v; }
  return -1;
}

void setup() {
  pinMode(LED, OUTPUT);
  Particle.function("luminosite", regler);
  Serial.begin(9600);
}

void loop() {
  if (millis() - derniereEtape < 10) return;
  derniereEtape = millis();

  // Rampe vers la consigne + leger battement sinusoidal.
  static float phase = 0;
  phase += 0.06;
  float battement = 0.25 * sin(phase);
  niveau += (niveauCible * (0.75 + battement) - niveau) * 0.05;

  analogWrite(LED, constrain((int)niveau, 0, 255));
}

// Regler depuis le cloud : Particle.function "luminosite" avec "200".

← Back to the board