fade-pwm.ino

Arduino Nano Every · File : fade-pwm.ino · Language : Arduino · Use case : Variation douce d'une LED en PWM

The 4809 PWM differs from the 328P (TCA/TCD timers instead of Timer0/1/2) yet the analogWrite API stays identical: this 0-255 triangular fade on pin 10 shows the common use, with a flicker-free default frequency.

Hardware

An Arduino Nano Every (ATmega4809 at 20 MHz, same footprint as the Nano, micro-B USB, separate UART chip, 5 V pins, drop-in compatibility with the classic Nano). Nano Every, LED + 330 Ω resistor on a PWM pin (D9, D10, D11, D3...).

Wiring

LED (+) → resistor → D10, cathode → GND; PWM pins carry the ~ mark on the silicon.

How it works

analogWrite loads the timer setpoint: 0-255/256 duty cycle. A step of 2 at 100 Hz gives an even ramp; the full cycle lasts about 2.5 seconds.

Running it

Install the Arduino megaAVR Boards package in the IDE, select Arduino Nano Every, upload. The USB Serial is separate from hardware Serial1 (RX/TX pins): classic serial code works as is.

Variants

On the 4809 you can push the PWM frequency far higher (motor driving) through TCA/TCD registers, or drive a transistor base for stronger loads.

fade-pwm.ino

// Fade sur le Nano Every : analogWrite 8 bits sur broche PWM.
// Le 4809 permet aussi de changer la frequence PWM (TCA/TCD).
const int LED = 10; // broche PWM (marquee ~ sur le silicium)

int niveau = 0;
int pas = 2;
unsigned long dernier = 0;

void setup() {
  pinMode(LED, OUTPUT);
  analogWriteResolution(8); // 0..255 par defaut
  Serial.begin(9600);
}

void loop() {
  if (millis() - dernier < 10) return; // 100 Hz de rafraichissement
  dernier = millis();
  niveau += pas;
  if (niveau >= 255) { niveau = 255; pas = -2; }
  if (niveau <= 0)   { niveau = 0;   pas = 2; }
  analogWrite(LED, niveau);
  // le cycle complet dure ~2,5 s : visible a l'oeil, sans papillotement
  static int dernierPrint = -10;
  if (abs(niveau - dernierPrint) > 30) {
    dernierPrint = niveau;
    Serial.println(niveau);
  }
}

← Back to the board