0Pricing
Arduino & IoT Academy · レッスン

LED を明るくしたり暗くしたりする

値をループさせて呼吸するような光の効果を作ります

「LED を明るくしたり暗くしたりする」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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 step

Now 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. ✨

よくある質問

「LED を明るくしたり暗くしたりする」レッスンは無料ですか?

はい。「LED を明るくしたり暗くしたりする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。

「LED を明るくしたり暗くしたりする」で何を学びますか?

値をループさせて呼吸するような光の効果を作ります ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Arduino & IoT Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「LED を明るくしたり暗くしたりする」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このArduino & IoT Academyレッスンでコードを書いて実行できますか?

はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. PWM の実際の動作
  2. analogWrite 0~255
  3. LED を明るくしたり暗くしたりする
  4. つまみで LED の明るさを制御する
← Arduino & IoT Academyに戻る