🛠 Potentiomètre et variation LED : l'ADC 10 bits et le PWM 8 bits version AVR

Arduino Duemilanove — Board Fiche · debutant · Schéma : IMG-duemilanove-debutant-3-potentiometre-pwm-fade.svg — potentiomètre 10 kΩ câblé entre 5 V, A0 et GND sur une Arduino Duemilanove, LED + résistance 220 Ω sur la broche PWM 9, liste des broches PWM 3/5/6/9/10/11 surlignée, ADC 10 bits annoté.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials