🛠 Variateur lumineux en PWM (broches 3, 5, 6, 9, 10)

Arduino Nano Every · intermediaire · Schéma : IMG-nano-every-intermediaire-1-nanoevery-pwm-led.svg — Nano Every avec une LED et sa résistance de 220 ohm sur la broche 9, un oscilloscope dessinant le signal PWM, et la liste des broches PWM autorisées annotée sur la carte

Objectives

Vary the brightness of an LED using analogWrite (PWM) and understand the specific constraint of the Nano Every: PWM is only available on pins 3, 5, 6, 9, and 10. Pin 11, which supports PWM on a classic Uno/Nano, is only a digital I/O pin on this board — a common pitfall when reusing shields.

Hardware

Complete Code

// Potentiometre A0 -> variation PWM sur broche 9
// Rappel: PWM possible uniquement sur 3, 5, 6, 9, 10 (pas 11 !)
const uint8_t LED_PWM = 9;
const uint8_t POT = A0;
uint32_t tPrecedente = 0;

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

void loop {
  if (millis - tPrecedente < 20) return;
  tPrecedente = millis;

  uint16_t brut = analogRead(POT);            // 0..1023
  uint8_t rapport = map(brut, 0, 1023, 0, 255);
  analogWrite(LED_PWM, rapport);              // 0..255
  Serial.print("rapport="); Serial.println(rapport);
}

Expected Verification

The LED gradually turns off and then increases in brightness as you turn the potentiometer. Using an oscilloscope or logic analyzer on pin 9, you will see a square wave whose duty cycle follows the input value. Test the same code on pin 11: the LED simply stays on or off — proving that PWM is not available there.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials