Bird
Raised Fist0
Arduinoprogramming~10 mins

analogWrite() and PWM output 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 - analogWrite() and PWM output
Call analogWrite(pin, value)
Check pin supports PWM?
NoNo PWM output
Yes
Convert value (0-255) to PWM duty cycle
Set PWM signal on pin
Pin outputs PWM voltage
Loop or end
The function analogWrite() sets a PWM signal on a pin by converting a value (0-255) to a duty cycle, which controls the average voltage output.
Execution Sample
Arduino
void setup() {
  pinMode(9, OUTPUT);
}

void loop() {
  analogWrite(9, 128);
}
This code sets pin 9 as output and writes a PWM signal with 50% duty cycle (128 out of 255) continuously.
Execution Table
StepActionPinValuePWM Duty CycleOutput Voltage Approximation
1Call analogWrite9128128/255 ≈ 50%About 2.5V (if 5V supply)
2PWM signal generated912850%Pin rapidly switches between 0V and 5V, averaging 2.5V
3Signal continues until changed912850%Steady PWM output at 50% duty cycle
4analogWrite called again with 2559255100%Pin outputs steady 5V
5analogWrite called with 0900%Pin outputs steady 0V
💡 PWM signal runs continuously until analogWrite is called again or pin mode changes.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 5
pinundefined999
valueundefined1282550
PWM duty cycleundefined50%100%0%
Output voltage approx.undefined2.5V5V0V
Key Moments - 3 Insights
Why does analogWrite use values from 0 to 255 instead of 0 to 100?
analogWrite uses an 8-bit scale (0-255) because PWM hardware works with 8-bit resolution, giving finer control than 0-100%. See execution_table step 1 for value to duty cycle conversion.
Does analogWrite output a steady voltage?
No, analogWrite outputs a PWM signal that switches between 0V and 5V rapidly. The average voltage depends on duty cycle. See execution_table step 2 for PWM signal behavior.
What happens if you call analogWrite with 0 or 255?
0 means 0% duty cycle (always off, 0V), 255 means 100% duty cycle (always on, 5V). See execution_table steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the PWM duty cycle when analogWrite(9, 128) is called?
A100%
B128%
C50%
D0%
💡 Hint
Check the 'PWM Duty Cycle' column at step 1 in the execution_table.
At which step does the pin output a steady 5V voltage?
AStep 4
BStep 5
CStep 2
DStep 1
💡 Hint
Look for 100% duty cycle and 5V output in the execution_table.
If you change the value in analogWrite from 128 to 64, how does the PWM duty cycle change?
AIt increases to about 75%
BIt decreases to about 25%
CIt stays at 50%
DIt becomes 0%
💡 Hint
Duty cycle is value divided by 255; 64/255 is about 25%. See variable_tracker for duty cycle values.
Concept Snapshot
analogWrite(pin, value) sets PWM on pin.
Value 0-255 controls duty cycle (0-100%).
PWM switches pin voltage rapidly on/off.
Average voltage = duty cycle × supply voltage.
Used for dimming LEDs, motor speed control.
Full Transcript
The analogWrite() function in Arduino sets a PWM signal on a pin. It takes a pin number and a value from 0 to 255. This value controls the duty cycle of the PWM signal, which is how long the signal stays HIGH in each cycle. For example, 128 means about 50% duty cycle, so the pin is HIGH half the time and LOW half the time. This creates an average voltage between 0V and 5V. The PWM signal keeps running until analogWrite is called again or the pin mode changes. Values 0 and 255 mean always LOW (0V) and always HIGH (5V) respectively. This method is useful for controlling brightness of LEDs or speed of motors by changing the average power delivered.

Practice

(1/5)
1. What does the analogWrite() function do on an Arduino board?
easy
A. It sets a digital pin to HIGH or LOW.
B. It reads the voltage from an analog sensor.
C. It outputs a PWM signal to simulate an analog voltage on a digital pin.
D. It measures the frequency of a signal on a pin.

Solution

  1. Step 1: Understand the purpose of analogWrite()

    The analogWrite() function does not output a true analog voltage but uses PWM (Pulse Width Modulation) to simulate varying voltage levels on digital pins.
  2. Step 2: Compare options with function behavior

    It outputs a PWM signal to simulate an analog voltage on a digital pin. correctly describes this behavior. Options A, B, and D describe other functions or actions unrelated to analogWrite().
  3. Final Answer:

    It outputs a PWM signal to simulate an analog voltage on a digital pin. -> Option C
  4. Quick Check:

    analogWrite() = PWM output [OK]
Hint: Remember: analogWrite() controls brightness/speed via PWM [OK]
Common Mistakes:
  • Confusing analogWrite() with analogRead()
  • Thinking analogWrite() outputs true analog voltage
  • Assuming analogWrite() sets pin HIGH or LOW directly
2. Which of the following is the correct syntax to set pin 9 to half brightness using analogWrite()?
easy
A. analogWrite(9, 512);
B. analogWrite(9, 0);
C. analogWrite(9, 255);
D. analogWrite(9, 127);

Solution

  1. Step 1: Understand the value range for analogWrite()

    The analogWrite() function accepts values from 0 to 255, where 0 is off and 255 is full brightness.
  2. Step 2: Calculate half brightness value

    Half brightness is about half of 255, which is approximately 127. A uses 512 (out of range), B uses 0 (off), C uses 255 (full brightness), so D is correct.
  3. Final Answer:

    analogWrite(9, 127); -> Option D
  4. Quick Check:

    Half brightness ≈ 127 [OK]
Hint: Use values between 0-255; half is about 127 [OK]
Common Mistakes:
  • Using values above 255 (like 512)
  • Confusing digitalWrite() with analogWrite()
  • Using full brightness value instead of half
3. What will be the effect of the following code snippet on an LED connected to pin 6?
void setup() {
  pinMode(6, OUTPUT);
}

void loop() {
  analogWrite(6, 0);
  delay(1000);
  analogWrite(6, 255);
  delay(1000);
}
medium
A. The LED will blink on and off every second.
B. The LED will stay dimly lit.
C. The LED will stay fully on.
D. The LED will flicker rapidly.

Solution

  1. Step 1: Analyze analogWrite values and delays

    The code sets pin 6 to 0 (off) for 1 second, then to 255 (full brightness) for 1 second, repeatedly.
  2. Step 2: Understand LED behavior

    When the pin is 0, the LED is off; when 255, it is fully on. The delays cause the LED to stay in each state for 1 second, making it blink on and off every second.
  3. Final Answer:

    The LED will blink on and off every second. -> Option A
  4. Quick Check:

    0 and 255 with delays = blink [OK]
Hint: 0 means off, 255 means full on; delays cause blinking [OK]
Common Mistakes:
  • Thinking analogWrite(0) dims LED instead of off
  • Ignoring delay effects on LED timing
  • Assuming LED flickers rapidly without delay
4. Identify the error in this code snippet intended to fade an LED on pin 10:
void setup() {
  pinMode(10, OUTPUT);
}

void loop() {
  for (int i = 0; i <= 255; i++) {
    analogWrite(10, i);
    delay(10);
  }
  for (int i = 255; i >= 0; i--) {
    analogWrite(10, i);
    delay(10);
  }
}
medium
A. The code will work correctly and fade the LED in and out.
B. The for loop variable i should be declared outside the loop.
C. The pin 10 is not set as OUTPUT.
D. analogWrite() cannot be used with pin 10.

Solution

  1. Step 1: Check pinMode setup

    Pin 10 is correctly set as OUTPUT in setup().
  2. Step 2: Analyze the for loops and analogWrite usage

    The loops increase and then decrease the PWM value from 0 to 255 and back, with delays to create a smooth fade effect. This is a common and correct pattern.
  3. Final Answer:

    The code will work correctly and fade the LED in and out. -> Option A
  4. Quick Check:

    For loops with analogWrite create fade [OK]
Hint: For fading, increase then decrease PWM values smoothly [OK]
Common Mistakes:
  • Forgetting to set pinMode to OUTPUT
  • Thinking analogWrite can't be used on pin 10
  • Misunderstanding loop variable scope
5. You want to control the speed of a DC motor using PWM on pin 3. Which code snippet correctly sets the motor speed to 60% power?
hard
A. analogWrite(3, 60); // 60 is the percentage value
B. analogWrite(3, 153); // 60% of 255 is about 153
C. analogWrite(3, 0.6); // decimal value for 60%
D. analogWrite(3, 255); // full power always

Solution

  1. Step 1: Convert percentage to PWM value

    60% of the maximum PWM value 255 is 0.6 x 255 = 153.
  2. Step 2: Check the analogWrite parameter

    analogWrite() requires an integer between 0 and 255. analogWrite(3, 153); // 60% of 255 is about 153 uses 153, which is correct. analogWrite(3, 60); // 60 is the percentage value uses 60 which is too low, C uses a decimal which is invalid, and D sets full power.
  3. Final Answer:

    analogWrite(3, 153); // 60% of 255 is about 153 -> Option B
  4. Quick Check:

    60% x 255 = 153 [OK]
Hint: Multiply percentage by 255 for PWM value [OK]
Common Mistakes:
  • Passing percentage directly instead of scaled value
  • Using decimal numbers instead of integers
  • Always setting full power without scaling