Blog about Web Programming and Electronics
💬 👤
back Back to blog
Arduino & Electronics

Difference Between PWM and analogWrite

Arduino does not have true analog outputs. Instead, it uses PWM (Pulse Width Modulation), which creates a “fake” analog signal by switching a digital pin HIGH and LOW very quickly.

1. What Is PWM?

PWM is a digital signal that rapidly toggles between HIGH and LOW. The key parameter is the duty cycle – the percentage of time the signal stays HIGH.

  • 0% duty cycle → LED off
  • 50% duty cycle → LED half brightness
  • 100% duty cycle → LED full brightness

2. What Is analogWrite?

analogWrite() sends a value between 0 and 255 to a PWM pin.

analogWrite(9, 0);    // 0% - off
analogWrite(9, 128);  // ~50% - medium brightness
analogWrite(9, 255);  // 100% - full brightness

Arduino converts the 0–255 value into the correct duty cycle.

3. When Do We Use PWM?

  • LED dimming
  • DC motor speed control
  • servo motor control (indirect)
  • power control for heaters, fans, pumps

Summary

Concept Description
PWM Fast HIGH/LOW switching to simulate analog output
analogWrite() Function that sets duty cycle (0–255)
Use cases LEDs, motors, fans, pumps
back Back to blog