analogWrite 0–255
Establezca el ciclo de trabajo para controlar el brillo del LED
analogWrite 0–255 es una lección gratuita de Arduino & IoT Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Arduino & IoT Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Arduino & IoT Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🌟
Preguntas frecuentes
¿La lección «analogWrite 0–255» es gratis?
Sí — el texto completo de «analogWrite 0–255» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Arduino & IoT Academy, actualiza a CoddyKit PRO. El curso de Arduino & IoT Academy incluye 4 lecciones en total.
¿Qué aprenderé en «analogWrite 0–255»?
Establezca el ciclo de trabajo para controlar el brillo del LED Practicas Arduino & IoT Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Arduino & IoT Academy?
No se requiere experiencia previa. Arduino & IoT Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «analogWrite 0–255»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Arduino & IoT Academy?
Sí. Cada lección de Arduino & IoT Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Qué hace realmente PWM
- analogWrite 0–255
- Atenúe un LED hacia arriba y hacia abajo
- El mando controla el brillo del LED