pwm-respiration.ino

LilyPad Arduino · File : pwm-respiration.ino · Language : Arduino · Use case : Faire respirer une LED avec la modulation de largeur d'impulsion

The LED ramps up then down thanks to analogWrite, which generates an 8-bit PWM on a ~-marked pin. The step reverses at the 0 and 255 bounds.

Hardware required

A LilyPad Arduino (ATmega168V/328V, 8 MHz, 3.3 V) with its sew tabs, programmed via an FTDI cable (breakout) and powered by 3.7 V (LiPo) or 5 V on V-REG. 1 LED + 330 Ω resistor.

Wiring

LED anode (through the resistor) on pin 9, cathode to GND.

How it works

analogWrite applies a rising then falling duty cycle; the eye perceives smooth brightness change. In the Arduino IDE, select « LilyPad Arduino » and the FTDI port, then Upload. Conductive thread adds a few ohms of resistance: keep runs short and insulate crossings.

Going further

Drive several LEDs in opposite phases, or a MOSFET for a full LED strip.

pwm-respiration.ino

// Respiration LED sur broche PWM (analogWrite 0..255).
const int LED = 9;
int niveau = 0;
int pas = 4;

void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  analogWrite(LED, niveau);
  niveau += pas;
  if (niveau <= 0 || niveau >= 255) pas = -pas; // inversion au bord
  delay(15);
}

← Back to the board