กลับไปหน้ารวมไฟล์
led-blink-and-fade-with-arduino-9fb899-en.md

Analog Illusion: LED Fading

The LED Blink and Fade project introduces a vital concept in digital electronics: Pulse Width Modulation (PWM). A microcontroller is purely digital; it can output 5 Volts (ON) or 0 Volts (OFF). It cannot output 2.5 Volts. So how do we make an LED glow at 50% brightness?

button_led_basic_interaction_1772681969235.png

The PWM Concept

We use a trick on the human eye. By rapidly turning the pin ON and OFF thousands of times per second, the eye blends the light together.

  • If the pin is ON for 50% of the time and OFF for 50%, the LED appears half as bright.
  • If it is ON for 10% and OFF for 90%, it appears very dim.

The analogWrite() Function

You can only use PWM on specific pins on your board (marked with a squiggle ~, like ~3, ~5, ~6, ~9, ~10, ~11 on the Uno).

  1. The function analogWrite(pin, value) accepts a value between 0 (totally off) and 255 (totally on).
  2. The Breathing Loop: You create a for loop that rapidly increments a variable brightness from 0 up to 255 with a tiny 5-millisecond delay. The LED smoothly fades from black to blinding brightness!
  3. The Return Loop: A second for loop counts backward (brightness--) from 255 back to 0, making the LED smoothly dim back down.

Essential Layout

  • Arduino Uno/Nano: The PWM generator.
  • Any 5mm LED and a 220-ohm Resistor.
  • A Breadboard and Jumper Wires.

ข้อมูล Frontmatter ดั้งเดิม

title: "LED Blink and Fade with Arduino"
description: "The next step in coding! Move beyond simply turning a light ON and OFF by learning how to use Pulse Width Modulation (PWM) to create smooth, pulsing 'breathing' effects."
category: "Basic Electronics"
difficulty: "Easy"