Aumentare e ridurre gradualmente la luminosità di un LED
Scorrere i valori per ottenere un effetto luce respirante
Aumentare e ridurre gradualmente la luminosità di un LED è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Arduino & IoT Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Arduino & IoT Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
From Steady to Breathing
One fixed value gives a steady glow. To make an LED fade, you simply keep changing the analogWrite value over time. 🌬️
The Plan: Count Up Slowly
To fade in, walk the brightness from 0 toward 255 in small steps, pausing briefly between each one so the change is visible.
A Loop That Climbs
A for loop is perfect for stepping through values. It counts from 0 to 255 and writes each one to the LED pin.
for (int b = 0; b <= 255; b++) {
analogWrite(9, b);
delay(10);
}delay Sets the Speed
The delay inside the loop controls how fast the fade feels. A bigger pause makes a slower, gentler rise.
delay(10); // 10 ms per stepNow Fade Back Down
To fade out, run a second loop that counts the brightness back down from 255 to 0, again pausing each step.
for (int b = 255; b >= 0; b--) {
analogWrite(9, b);
delay(10);
}Put Both in loop()
Place the up loop then the down loop inside loop. Arduino repeats loop forever, so you get an endless breathing effect.
Step Size Changes the Feel
Counting by 1 is silky smooth. Increase the step to b += 5 and the fade speeds up but turns slightly choppy.
for (int b = 0; b <= 255; b += 5)Stay Inside the Range
Keep your counter between 0 and 255. Writing values outside that range just wraps or clamps and breaks the smooth ramp.
An Alternative: One Variable
Instead of two loops, keep one brightness variable and a direction. Add or subtract each pass and flip direction at the ends.
brightness += direction;
if (brightness == 0 || brightness == 255)
direction = -direction;Use a PWM Pin
This only fades on a PWM pin like 9. On a plain pin the LED would jump straight from off to on with no smooth ramp.
Why It Looks So Calm
The breathing look works because each small step nudges the duty cycle a little, and your eye averages the tiny changes.
Quick Check
Think about what makes a fade smooth.
Recap: Make It Breathe
Step the brightness from 0 to 255 and back with small delays. Loop it forever on a PWM pin and your LED breathes. ✨
Domande Frequenti
La lezione «Aumentare e ridurre gradualmente la luminosità di un LED» è gratuita?
Sì — il testo completo di «Aumentare e ridurre gradualmente la luminosità di un LED» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Arduino & IoT Academy, passa a CoddyKit PRO. Il corso Arduino & IoT Academy include 4 lezioni in totale.
Cosa imparerò in «Aumentare e ridurre gradualmente la luminosità di un LED»?
Scorrere i valori per ottenere un effetto luce respirante Eserciti Arduino & IoT Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Arduino & IoT Academy?
Non è richiesta alcuna esperienza precedente. Arduino & IoT Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Aumentare e ridurre gradualmente la luminosità di un LED»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Arduino & IoT Academy?
Sì. Ogni lezione Arduino & IoT Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Che cosa fa davvero il PWM
- analogWrite 0–255
- Aumentare e ridurre gradualmente la luminosità di un LED
- La manopola controlla la luminosità del LED