🛠 Variateur lumineux en PWM (broches 3, 5, 6, 9, 10)
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
- 1 × Arduino Nano Every
- 1 × LED + 1 × 220 Ω resistor
- 1 × 10 kΩ potentiometer (connected to A0)
- Jumper wires, micro-USB cable
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
- Using
analogWriteon pin 11 (or 4, 7, 8…): no PWM effect, the pin remains in digital on/off mode; checkdigitalPinHasPWM(pin)in your code. - Forgetting the series resistor with the LED: while 5 mA per LED might seem safe, it unnecessarily stresses the chip.
- Expecting the same frequency as the classic Nano: the megaAVR core remaps the timers (configurable TCA/TCD), so PWM frequencies are not identical to the old core — this matters for motor control or audio playback.
- Value confusion:
analogWritetakes values from 0-255, whileanalogReadreturns values from 0-1023 — hence the need formap. - "Flickering" LED at low duty cycle: this is the perceptible flicker of PWM at low frequencies; normal in educational contexts.
Other tutorials for this board
Code examples for this board
multi-serie.inoArduinoadc-filtre.inoArduinointerrupt-bouton.inoArduinonvm-consignes.inoArduinosommeil-every.inoArduinowatchdog-secours.inoArduino