🛠 Faire varier la luminosité avec un potentiomètre (ADC + PWM)

Arduino UNO R3 · debutant · Schéma : IMG-uno-r3-debutant-3-potentiometre-fade-pwm.svg — potentiomètre 10 kΩ dont le curseur va sur A0, LED + 330 Ω sur la pin PWM 9.

Objectives

Understand the dual analog/digital conversion: the ATmega328P's 10-bit ADC (6 analog inputs, values 0–1023) and the PWM output (~980 Hz on pins 3, 5, 6, 9, 10, 11 — the only six PWM pins on the UNO). All linked together by map.

Hardware

Complete Code

// "Fade" classique : ADC 10 bits (0..1023) -> PWM 8 bits (0..255).
const uint8_t POT = A0;
const uint8_t PWM_PIN = 9;   // une des six pins PWM : 3, 5, 6, 9, 10, 11

void setup {
  Serial.begin(115200);
}

void loop {
  int brut = analogRead(POT);            // 10 bits : 0..1023
  uint8_t pwm = map(brut, 0, 1023, 0, 255);
  analogWrite(PWM_PIN, pwm);             // PWM ~980 Hz pilotée par un timer
  Serial.print("brut="); Serial.print(brut);
  Serial.print(" pwm="); Serial.println(pwm);  // suivez ça dans le Traceur série
  delay(10);
}

Expected Verification

When turning the potentiometer, the LED gradually turns off and then brightens, without any perceptible steps (PWM modulates the pulse width, the eye integrates it). The Serial Plotter (Tools > Serial Plotter) shows brut following 0→1023 and pwm following 0→255.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials