analogWrite 0~255
デューティー比を設定して LED の明るさを制御します
「analogWrite 0~255」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🌟
よくある質問
「analogWrite 0~255」レッスンは無料ですか?
はい。「analogWrite 0~255」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。
「analogWrite 0~255」で何を学びますか?
デューティー比を設定して LED の明るさを制御します ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Arduino & IoT Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「analogWrite 0~255」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このArduino & IoT Academyレッスンでコードを書いて実行できますか?
はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- PWM の実際の動作
- analogWrite 0~255
- LED を明るくしたり暗くしたりする
- つまみで LED の明るさを制御する