🛠 Potentiomètre et variation LED : l'ADC 10 bits et le PWM 8 bits version AVR
Objectives
The classic analog pairing on AVR: read a potentiometer with the 10-bit ADC (0–1023 on A0–A5) and control an LED's brightness with 8-bit PWM (0–255). You will memorize the six PWM pins on the Duemilanove (3, 5, 6, 9, 10, 11) and the 1023→255 scaling using map. Unlike the Due, everything here is 5 V: no level shifter is needed for your 5 V TTL modules.
Hardware
- 1 Arduino Duemilanove
- 1 10 kΩ potentiometer
- 1 LED + 1 220 Ω resistor
- 5 male-to-male wires, mini-USB cable
Complete code
// Potentiometre sur A0 -> luminosite LED sur broche PWM 9
const byte POT = A0; // ADC 10 bits : 0..1023, plage 0..5 V (AREF)
const byte LED_PIN = 9; // PWM 8 bits (broches PWM : 3,5,6,9,10,11)
void setup {
Serial.begin(9600); // suivi serie via la FTDI
}
void loop {
int brut = analogRead(POT); // 0..1023
byte consigne = map(brut, 0, 1023, 0, 255); // -> PWM 8 bits
analogWrite(LED_PIN, consigne);
Serial.print("brut="); Serial.print(brut);
Serial.print(" pwm="); Serial.println(consigne);
delay(50);
}
Expected verification
Turning the potentiometer, the LED gradually turns off and then rises to its maximum, without visible steps. The serial monitor displays the raw value 0–1023 and the PWM setpoint 0–255 that follows. At full turn in one direction or the other, you get 0 and ~1023 respectively (± noise).
Common errors
- Placing the LED on a non-PWM pin (e.g., 4 or 7):
analogWritehas no effect there, the LED remains off or steadily on; the only PWM pins are 3, 5, 6, 9, 10, 11. - Forgetting the 220 Ω resistor: the AVR pin tolerates 40 mA, but an LED without a resistor = LED (or pin) eventually burned out.
- Wrong range: the Duemilanove's ADC is 10-bit (0–1023), not 12-bit like on a Due; code copied from a Due tutorial using 4095 divides the brightness by four.
- Potentiometer powered at 3V3: the Duemilanove's 3V3 pin is produced by the FTDI and limited to 50 mA — for a simple potentiometer it works, but 5 V is more natural here; the measurement reference remains 5 V (modifiable via AREF, see the dedicated tutorial).
- LED blinking instead of varying: some oscilloscopes/cameras reveal the PWM at ~500 Hz — normal, the eye does not see it.
- Wiring the potentiometer's wiper to the ends: the reading does not change linearly; wiper on A0, the other two legs on 5 V and GND.
Other tutorials for this board
- Faire clignoter la LED 13 d'une Duemilanove (et choisir le bon ATmega)
- Bouton poussoir et moniteur série sur la Duemilanove (avec ses 0,5 s de silence)
- Ajouter un deuxième port série avec SoftwareSerial (GPS sur broches 10/11)
- Régler la référence de mesure avec AREF et analogReference
- Persister une calibration dans l'EEPROM de l'ATmega (512 octets ou 1 Ko)
- Convertir la Duemilanove en programmateur AVR (ArduinoISP) pour graver des puces nues
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinopwm-respiration.inoArduinopotentiometre-plotter.inoArduinoservo-balayage.inoArduinobuzzer-melodie.inoArduino