Bird
Raised Fist0
Arduinoprogramming~10 mins

LED brightness control with PWM in Arduino - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - LED brightness control with PWM
Start
Set PWM pin as OUTPUT
Loop Start
Increase brightness value
Write PWM value to LED pin
Delay for visible change
If brightness > max?
YesReset brightness to 0
Back to Loop Start
The program sets a pin for PWM output, then repeatedly increases brightness by changing PWM value, writes it to the LED, delays, and resets after max brightness.
Execution Sample
Arduino
int ledPin = 9;
int brightness = 0;
void setup() {
  pinMode(ledPin, OUTPUT);
}
void loop() {
  analogWrite(ledPin, brightness);
  brightness += 5;
  if (brightness > 255) brightness = 0;
  delay(30);
}
This code gradually increases LED brightness using PWM on pin 9, then resets to 0 to repeat the cycle.
Execution Table
StepbrightnessCondition brightness > 255?ActionPWM Output to LED
10NoWrite PWM 0, brightness += 50
25NoWrite PWM 5, brightness += 55
310NoWrite PWM 10, brightness += 510
415NoWrite PWM 15, brightness += 515
520NoWrite PWM 20, brightness += 520
...............
52255NoWrite PWM 255, brightness += 5255
53260YesReset brightness to 0260 (not written)
540NoWrite PWM 0, brightness += 50
💡 Loop runs indefinitely; brightness resets to 0 after exceeding 255.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5...After 52After 53After 54
brightness0510152025...25505
Key Moments - 3 Insights
Why does brightness reset to 0 after 255?
Because PWM values must be between 0 and 255. When brightness exceeds 255 (see step 53 in execution_table), it resets to 0 to start the cycle again.
Why do we add 5 to brightness each loop?
Adding 5 increases brightness gradually for a smooth fade effect. This is shown in the action column of execution_table where brightness increments by 5 each step.
What happens if delay(30) is removed?
Without delay, brightness changes too fast to see the fade effect clearly. The LED would appear to jump brightness instantly instead of smoothly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the brightness value at step 4?
A10
B15
C20
D5
💡 Hint
Check the brightness column at step 4 in the execution_table.
At which step does brightness reset to 0?
AStep 52
BStep 54
CStep 53
DStep 50
💡 Hint
Look for the row where condition brightness > 255 is Yes and action resets brightness.
If we change brightness increment from 5 to 10, how does it affect the execution_table?
ABrightness increases faster, fewer steps before reset
BBrightness increases slower, more steps before reset
CBrightness stays the same, no change
DBrightness resets at 5 instead of 255
💡 Hint
Increasing increment means brightness reaches 255 quicker, so fewer steps before reset.
Concept Snapshot
Arduino PWM controls LED brightness by sending values 0-255.
Use analogWrite(pin, value) to set brightness.
Increase value gradually in loop for fade effect.
Reset value after 255 to repeat.
Add delay for visible smooth change.
Full Transcript
This example shows how to control LED brightness on Arduino using PWM. The program sets a pin as output and uses analogWrite to send brightness values from 0 to 255. Each loop increases brightness by 5, writes it to the LED, then delays 30 milliseconds. When brightness goes above 255, it resets to 0 to start fading again. The execution table traces each step, showing brightness values, conditions, and actions. Key moments explain why brightness resets and why delay is important. The quiz tests understanding of brightness values at steps and effects of changing increment.

Practice

(1/5)
1. What does analogWrite(pin, 128) do to an LED connected to the specified pin?
easy
A. Sets the LED brightness to about half of its maximum brightness.
B. Turns the LED fully off.
C. Turns the LED fully on at maximum brightness.
D. Causes the LED to blink on and off repeatedly.

Solution

  1. Step 1: Understand PWM value range

    The analogWrite function accepts values from 0 (off) to 255 (full brightness).
  2. Step 2: Interpret the value 128

    128 is about half of 255, so the LED brightness will be about half of maximum.
  3. Final Answer:

    Sets the LED brightness to about half of its maximum brightness. -> Option A
  4. Quick Check:

    PWM value 128 = half brightness [OK]
Hint: Remember 0=off, 255=full brightness, 128=half brightness [OK]
Common Mistakes:
  • Thinking 128 turns LED off
  • Confusing analogWrite with digitalWrite
  • Assuming 128 means blinking
2. Which of the following is the correct syntax to set PWM brightness on pin 9 to maximum in Arduino?
easy
A. analogWrite(255, 9);
B. digitalWrite(9, 255);
C. analogWrite(9, 255);
D. analogRead(9, 255);

Solution

  1. Step 1: Recall analogWrite syntax

    The correct syntax is analogWrite(pin, value) where pin is the pin number and value is 0-255.
  2. Step 2: Check each option

    analogWrite(9, 255); uses analogWrite(9, 255); which is correct. Others have wrong function names or argument order.
  3. Final Answer:

    analogWrite(9, 255); -> Option C
  4. Quick Check:

    Correct function and argument order = analogWrite(9, 255); [OK]
Hint: analogWrite(pin, value) sets PWM; pin first, value second [OK]
Common Mistakes:
  • Swapping pin and value arguments
  • Using digitalWrite instead of analogWrite
  • Using analogRead instead of analogWrite
3. What will be the effect of this Arduino code snippet on an LED connected to pin 6?
for (int brightness = 0; brightness <= 255; brightness += 51) {
  analogWrite(6, brightness);
  delay(100);
}
medium
A. The LED will stay at full brightness without change.
B. The LED will blink on and off 5 times quickly.
C. The code will cause a compile error due to wrong syntax.
D. The LED brightness will increase in 5 steps from off to full brightness.

Solution

  1. Step 1: Analyze the for loop increments

    The loop variable brightness starts at 0 and increases by 51 until it reaches 255, so values are 0, 51, 102, 153, 204, 255 (6 values, 5 steps).
  2. Step 2: Understand analogWrite effect

    Each loop sets LED brightness to the current value, increasing brightness in steps with 100ms delay.
  3. Final Answer:

    The LED brightness will increase in 5 steps from off to full brightness. -> Option D
  4. Quick Check:

    Brightness steps with delay = gradual brightness increase [OK]
Hint: Loop increments PWM value to fade LED brightness up [OK]
Common Mistakes:
  • Counting 6 steps instead of 5 increments
  • Thinking LED blinks on/off instead of fading
  • Assuming syntax error due to loop
4. Identify the error in this Arduino code that tries to fade an LED on pin 10:
for (int i = 0; i < 256; i++) {
  analogWrite(10, i);
  delay(50);
}
analogWrite(10, 256);
medium
A. The for loop should use <= 256 instead of < 256.
B. The value 256 is invalid for analogWrite; max is 255.
C. Pin 10 cannot be used with analogWrite on Arduino.
D. delay() cannot be used inside a for loop.

Solution

  1. Step 1: Check analogWrite value limits

    analogWrite accepts values from 0 to 255. Using 256 is out of range and invalid.
  2. Step 2: Verify other code parts

    The for loop correctly uses i from 0 to 255. Pin 10 supports PWM on most Arduino boards. delay() is allowed inside loops.
  3. Final Answer:

    The value 256 is invalid for analogWrite; max is 255. -> Option B
  4. Quick Check:

    analogWrite max value = 255, 256 causes error [OK]
Hint: PWM values max at 255; never use 256 [OK]
Common Mistakes:
  • Using values above 255 for analogWrite
  • Thinking delay() is not allowed in loops
  • Assuming pin 10 can't do PWM
5. You want to create a smooth breathing LED effect using PWM on pin 3. Which code snippet correctly achieves this effect?
hard
A. for (int b = 0; b <= 255; b++) { analogWrite(3, b); delay(10); } for (int b = 255; b >= 0; b--) { analogWrite(3, b); delay(10); }
B. analogWrite(3, 255); delay(1000); analogWrite(3, 0); delay(1000);
C. for (int b = 0; b < 256; b += 50) { analogWrite(3, b); delay(100); }
D. digitalWrite(3, HIGH); delay(500); digitalWrite(3, LOW); delay(500);

Solution

  1. Step 1: Understand breathing LED effect

    A breathing effect smoothly increases brightness from 0 to max, then back down to 0 repeatedly.
  2. Step 2: Analyze each option

    for (int b = 0; b <= 255; b++) { analogWrite(3, b); delay(10); } for (int b = 255; b >= 0; b--) { analogWrite(3, b); delay(10); } uses two loops: one increasing PWM from 0 to 255, then decreasing back to 0 with small delays for smoothness. analogWrite(3, 255); delay(1000); analogWrite(3, 0); delay(1000); just turns LED fully on and off abruptly. for (int b = 0; b < 256; b += 50) { analogWrite(3, b); delay(100); } increases brightness in large steps, not smooth. digitalWrite(3, HIGH); delay(500); digitalWrite(3, LOW); delay(500); uses digitalWrite, which only turns LED fully on or off.
  3. Final Answer:

    for (int b = 0; b <= 255; b++) { analogWrite(3, b); delay(10); } for (int b = 255; b >= 0; b--) { analogWrite(3, b); delay(10); } -> Option A
  4. Quick Check:

    Smooth increase and decrease PWM = breathing LED [OK]
Hint: Use increasing then decreasing PWM values for breathing effect [OK]
Common Mistakes:
  • Using digitalWrite instead of analogWrite for brightness
  • Skipping the decreasing brightness loop
  • Using large PWM steps causing jerky effect