digitalWrite()와 달리 analogWrite()는 0 ~ 255의 값으로 LED 밝기를 조절할 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int led = 9;
int brightness = 0;
int fadeAmount = 5;

void setup()  {
  pinMode(led, OUTPUT);
}

void loop()  {
  analogWrite(led, brightness);
  brightness = brightness + fadeAmount;
 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }

  delay(30);
}