pwm-respiration.ino

Arduino Duemilanove — Board Fiche · 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

An Arduino Duemilanove (ATmega168 or 328P, 16 MHz, 5 V) with its mini-USB-B cable; the serial port appears once the FTDI driver is installed. 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 « Arduino Duemilanove » (or « Diecimila » for very old boards) and the FTDI port, then Upload. The ATmega168 leaves only 14 KB for the sketch.

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