analogWrite 0–255
Ustawianie współczynnika wypełnienia w celu sterowania jasnością diody LED
analogWrite 0–255 to bezpłatna lekcja Arduino & IoT Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Arduino & IoT Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🌟
Często zadawane pytania
Czy lekcja „analogWrite 0–255” jest bezpłatna?
Tak — pełny tekst „analogWrite 0–255” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Arduino & IoT Academy, przejdź na CoddyKit PRO. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „analogWrite 0–255”?
Ustawianie współczynnika wypełnienia w celu sterowania jasnością diody LED Ćwiczysz Arduino & IoT Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Arduino & IoT Academy?
Nie wymagamy żadnego doświadczenia. Arduino & IoT Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „analogWrite 0–255”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Arduino & IoT Academy?
Tak. Każda lekcja Arduino & IoT Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Co naprawdę robi PWM
- analogWrite 0–255
- Płynne rozjaśnianie i przyciemnianie diody LED
- Pokrętło steruje jasnością diody LED