🛠 Variateur d'intensité LED avec PWM

Arduino Pro Mini · intermediaire · Schéma : IMG-pro-mini-intermediaire-4-promini-pwm-led.svg — LED avec résistance 220 Ω sur broche PWM 9 de l'Arduino Pro Mini, potentiomètre sur A0 pilotant analogWrite

Objectives

Use Pulse Width Modulation (PWM) to vary the brightness of an LED: the Arduino core exposes analogWrite(pin, 0-255) on the 6 PWM pins of the Pro Mini (3, 5, 6, 9, 10, 11 — 8-bit resolution via the ATmega328P timer). The potentiometer connected to A0 (0-1023) is scaled to 0-255 to control the duty cycle. Remember the absolute limit: 40 mA maximum per pin, regardless of whether it is the 5 V or 3.3 V model.

Hardware

Complete code

// Variateur PWM : potentiometre A0 -> LED broche 9
const uint8_t POT = A0;
const uint8_t LED_PWM = 9;      // broche PWM (3, 5, 6, 9, 10 ou 11)
uint32_t derniere = 0;

void setup {
  pinMode(LED_PWM, OUTPUT);
  Serial.begin(57600);
}

void loop {
  if (millis - derniere >= 20) {
    derniere = millis;
    int brut = analogRead(POT);              // 0..1023
    uint8_t rapport = map(brut, 0, 1023, 0, 255);
    analogWrite(LED_PWM, rapport);           // 0..255
    Serial.println(rapport);
  }
}

Expected verification

Turning the potentiometer, the LED gradually turns off (0) then increases in brightness up to full intensity (255) without steps. By looking at the LED through a phone camera in slow motion, you can distinguish the PWM flickering while the eye perceives continuous light. On the 3.3 V model, the PWM "high" voltage is 3.3 V: choose the resistor accordingly (the LED will be slightly less bright than at 5 V).

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials