🛠 Faire varier la luminosité avec un potentiomètre (ADC + PWM)
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
- 1 Arduino UNO R3 + USB-B cable
- 1 10 kΩ potentiometer (3 legs: one to 5 V, one to GND, the wiper in the middle)
- 1 LED + 1 330 Ω resistor
- Wiring: potentiometer wiper to A0; LED + 330 Ω to pin 9 (must be one of the six PWM pins).
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
- Wiring the LED to a non-PWM pin (e.g., pin 4): analogWrite has no effect, the LED remains off.
- Confusing the ranges: sending a value 0–1023 to analogWrite (which expects 0–255) saturates at 255.
- Wiring the potentiometer using only two legs: the wiper no longer follows anything, the value jumps around randomly.
- Not powering the potentiometer with 5 V: at 3.3 V the ADC range is reduced (the ADC measures relative to AREF, 5 V by default).
- The board's 3.3 V pin is limited to 50 mA: do not connect multiple LEDs without calculation.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-poussoir.inoArduinopwm-fade.inoArduinocapteur-lumiere-ldr.inoArduinoservo-balayage.inoArduinoruban-neopixel.inoArduino