analogWrite 0–255
Impostare il duty cycle per controllare la luminosità del LED
analogWrite 0–255 è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 2 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.
The Command That Does PWM
To produce PWM you call one function: analogWrite. You give it a pin and a level, and the chip handles the fast pulsing for you.
analogWrite(9, 200);The Value Range Is 0 to 255
The second argument goes from 0 to 255. That is 256 steps, because one byte holds exactly that many values.
0 Means Fully Off
Pass 0 and the duty cycle is zero. The pin stays LOW the whole time, so the LED is completely dark.
analogWrite(9, 0); // LED off255 Means Fully On
Pass 255 and the duty cycle is 100%. The pin behaves just like digitalWrite HIGH, so the LED is at full brightness.
analogWrite(9, 255); // LED fullThe Middle Gives You Dimming
A value of 128 is roughly half power, so the LED looks about half as bright. Values in between give you a smooth scale.
analogWrite(9, 128); // about halfBigger Number, Brighter Light
The relationship is simple: a higher value means more on-time and more brightness. Lower values dim the LED down.
Pick a PWM-Capable Pin
analogWrite only does true PWM on the tilde-marked pins. On a non-PWM pin it just snaps fully on or off at the halfway point.
Set the Pin as OUTPUT First
Just like a normal LED, declare the pin as OUTPUT in setup so it can drive current before you call analogWrite.
pinMode(9, OUTPUT);A Tiny Working Sketch
Here is the whole idea: in setup mark the pin OUTPUT, then in loop hold it at a steady mid-brightness level.
void setup() {
pinMode(9, OUTPUT);
}
void loop() {
analogWrite(9, 100);
}Brightness Is Not Perfectly Linear
Doubling the value does not exactly double how bright it looks, because human eyes sense light on a curve, not a straight line.
It Works on Motors Too
analogWrite is not only for LEDs. The same 0 to 255 value can set a motor's speed through a driver. ⚙️
Quick Check
Make sure the value range is crystal clear.
Recap: analogWrite Levels
analogWrite takes a pin and a value from 0 to 255: 0 is off, 255 is full, and the middle dims smoothly on PWM pins. 🌟
Domande Frequenti
La lezione «analogWrite 0–255» è gratuita?
Sì — il testo completo di «analogWrite 0–255» è 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 «analogWrite 0–255»?
Impostare il duty cycle per controllare la luminosità del LED 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 2 di 4.
Quanto tempo richiede la lezione «analogWrite 0–255»?
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