Chapter 10

In the previous chapter, you used a potentiometer to generate smooth values between 0 and 1023, and then mapped them into useful ranges.

Now comes a natural question:

If Arduino outputs only HIGH or LOW, how does it control brightness or speed?

This chapter answers that with PWM


💡 The Problem PWM Solves

Digital output pins can only do this:

  • HIGH → 5V

  • LOW → 0V

But LEDs don’t just need ON or OFF.
They can be dim, bright, or anything in between.

So how do we fake an “analog output” using a digital pin?


⚡ What Is PWM?

PWM stands for Pulse Width Modulation.

Instead of outputting a steady voltage, Arduino:

  • switches the pin ON and OFF very fast

  • controls how long it stays ON vs OFF

This ratio is called the duty cycle.


🧠 Think of PWM Like This

Imagine flipping a switch very fast:

  • ON for long time → looks bright

  • ON for short time → looks dim

Your eyes (and motors) don’t see the rapid switching —
they see the average effect.

That’s

PWM.


📊 Duty Cycle Explained

  • 0% duty cycle → always OFF

  • 50% duty cycle → ON half the time

  • 100% duty cycle → always ON

Arduino expresses this using numbers:

  • 0 → 0% (OFF)
  • 128 → ~50%
  • 255 → 100% (FULL ON)

🔌 PWM Pins on Arduino Uno

Not all pins support PWM.

On Arduino Uno, PWM pins are marked with ~:

3, 5, 6, 9, 10, 11

Only these pins can use analogWrite().

Click here to learn about pin scheme of Arduino.


💻 Basic PWM Example (LED Fade)

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

void loop() { analogWrite(9, 128); // ~50% brightness }

<sp-lab title=“Arduino Basic PWM” subtitle=“Interactive simulation built in Tinkercad” link=“https://www.tinkercad.com/things/8wEFnZn7DDA-pwm-basic” button=“Open Simulation in Tinkercad” image=“/images/pathway-to-arduino/understanding-PWM-and-LED-fading/1.png”

Try playing with the voltage value and see different levels of brightness!

Even though the function is called analogWrite(),
it is still digital PWM, not true analog voltage.


🔄 Fading an LED Smoothly

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

void loop() { for (int i = 0; i <= 255; i++) { analogWrite(9, i); delay(10); }

for (int i = 255; i >= 0; i—) { analogWrite(9, i); delay(10); } }

<sp-lab title=“Arduino Fading LED PWM” subtitle=“Interactive simulation built in Tinkercad” link=“https://www.tinkercad.com/things/kTncBdyO7dY-pwm-fading-led” button=“Open Simulation in Tinkercad” image=“/images/pathway-to-arduino/understanding-PWM-and-LED-fading/1.png”

Try changing the delay to obtain different speeds of fading!

This gradually changes the duty cycle, making the LED fade in and out.


🔁 Potentiometer → PWM Control

Now combine what you learned earlier:

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

void loop() { int potValue = analogRead(A0); int pwmValue = map(potValue, 0, 1023, 0, 255); analogWrite(9, pwmValue); }

<sp-lab title=“Arduino PWM using Potentiometer Input” subtitle=“Interactive simulation built in Tinkercad” link=“https://www.tinkercad.com/things/gHHbhdqs8h7-using-pwm-with-potentiometer” button=“Open Simulation in Tinkercad” image=“/images/pathway-to-arduino/understanding-PWM-and-LED-fading/2.png”

Try rotating the potentiometer and observe how the LED brightness changes in real time.

Turning the potentiometer now directly controls LED brightness.


🎯Let's Recall Previous Chapters!!

What happens if a digital input pin is left floating? It can randomly read HIGH or LOW It always reads LOW It becomes OUTPUT Why do we use pull-up resistors? To increase current To prevent floating inputs To protect the microcontroller Which function is used to read analog inputs? serial.Print analogRead analogReader 1. It can randomly read HIGH or LOW
2. To prevent floating inputs
3. analogRead

Check these if you got them wrong:


⚠️ Important PWM Facts

  • PWM does not change voltage level

  • It changes time ON vs OFF

  • Average voltage is what devices respond to

  • Frequency is fixed by Arduino timers (for now)

Later, we’ll talk about PWM frequency, noise, and motors.


🧰 Common Beginner Mistakes

  • Using analogWrite() on non-PWM pins

  • Expecting smooth fade on wrong pin

  • Forgetting the resistor for LED

  • Thinking PWM is “real analog output”

PWM is a clever illusion, not true analog.


📌 What Comes Next

You’ve now completed the core I/O fundamentals:

  • Digital Output

  • Digital Input

  • Analog Input

  • PWM Output

Next, we fix a problem you’ve already seen but maybe ignored:

👉 Delays vs Non-Blocking Code (millis())