0Pricing
Arduino & IoT Academy · บทเรียน

ค่อย ๆ เพิ่มและลดแสง LED

วนค่าต่าง ๆ เพื่อสร้างเอฟเฟกต์แสงหายใจ

ค่อย ๆ เพิ่มและลดแสง LED เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ค่อย ๆ เพิ่มและลดแสง LED”

วนค่าต่าง ๆ เพื่อสร้างเอฟเฟกต์แสงหายใจ คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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