Skip to main content
Translate content

To read content in another language, use your browser’s Translate page option (right-click the page or open the browser menu). Choose your preferred language; if offered, enable automatic translation. The site language menu changes navigation, not the original article. Interactive activities may remain in their original language.

roboticsFundamentalsEstimated time: 30 minUpdated December 3, 2026

Arduino Digital Outputs: Interactive LED and PWM Experiments

Control digital states, PWM duty cycle and sensor thresholds on the UNO R3

Explore how the UNO R3 controls an LED using digital states, pulse-width modulation and a sensor decision. Seven native activities connect sketches, calculations and safe wiring.

Arduino UNO R3 beside an LED and a conceptual PWM pulse train showing digital output control.

Learning objectives

  • Configure and predict digital outputs
  • Trace a blinking sketch
  • Apply a strict threshold
  • Select a safe LED resistor

An Arduino can switch an LED on, switch it off, or make it appear dimmer without changing the supply voltage. This lesson builds on basic input and output concepts and focuses on controlling outputs on the Arduino UNO R3. You can complete every interactive experiment without hardware.

What you will learn

  • Configure a digital output and predict HIGH and LOW states.
  • Trace a sketch from setup() to loop().
  • Calculate PWM duty cycle and ideal average voltage.
  • Apply a strict sensor threshold to control an LED.
  • Choose a safe series resistor for an external LED.

Prerequisites: basic Arduino sketch structure, variables and simple if statements. All examples assume a nominal 5 V Arduino UNO R3, not a different board.

1. Digital output: two states

A digital output is either HIGH or LOW. On the UNO R3, HIGH is near 5 V and LOW near 0 V under suitable load. The built-in LED (LED_BUILTIN, pin 13) already has a current-limiting circuit. Use it for the first experiment without adding wires.

In setup(), configure the pin once as OUTPUT. digitalWrite() then selects the state. The state remains until changed. A digital output is a signal, not a power supply for motors or other high-current devices.

Worked example: a steady LED

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
  // The LED stays on.
}

After setup(), the built-in LED is on. An empty loop() does not turn it off. Changing HIGH to LOW turns it off.

Experiment 1 — Switch the virtual LED

Predict the state before pressing each button. This model represents the output state; it does not upload a sketch to a board.

Press HIGH, then LOW. Observe the LED state. Reset returns the model to OFF.

Pin 13

LED OFF

2. Follow the program in time

A blinking LED alternates between HIGH and LOW. delay(500) pauses the sketch for about 500 milliseconds; it does not change the pin state by itself.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}

One cycle lasts approximately 1 second: 500 ms on plus 500 ms off. The ideal blink frequency is 1 Hz. This slow blink illustrates timing, not the hardware PWM frequency.

Experiment 2 — Trace one blink cycle

Use Next and Previous to follow the sketch. Reset returns to the first step.

Step 1 / 6

setup(): configure pin 13 as OUTPUT.

3. PWM: control the proportion of ON time

Pulse-width modulation (PWM) rapidly switches a digital signal between HIGH and LOW. The duty cycle is the fraction of a period spent HIGH. Increasing the duty cycle generally makes an LED appear brighter, but perceived brightness is not exactly proportional to duty cycle.

On UNO R3, analogWrite() supports hardware PWM on pins 3, 5, 6, 9, 10 and 11. Its normal input range is 0 to 255. It does not generate a steady analog voltage on those pins.

analogWrite valueApproximate duty cycleIdeal average voltage (5 V HIGH)
00%0 V
6425.1%1.25 V
12850.2%2.51 V
19174.9%3.75 V
255100%5 V

Worked example: value 153

Duty cycle (%) = 100 × value / 255. For 153, the duty cycle is 100 × 153 / 255 = 60%. Assuming ideal HIGH = 5 V and LOW = 0 V, the time-average voltage is 5 × 0.60 = 3 V. The instantaneous voltage still switches between approximately 0 V and 5 V. This average is an ideal model, not a measured analog output.

Experiment 3 — Adjust PWM on pin 9

Try 0, 128 and 255. Compare the displayed duty cycle with the table. The progress indicator represents duty cycle, not measured light intensity.

Move the PWM slider between 0 and 255. Observe the duty cycle. Reset returns to 0%.

Pin 9

0

Duty cycle: 0%

Experiment 4 — Calculate the duty cycle

A PWM value of 102 is used. Enter the duty cycle as a percentage (number only).

Answer and explanation

100 × 102 / 255 = 40%. The ideal average voltage is 5 × 0.40 = 2 V.

4. From a sensor reading to an output decision

Imagine a light sensor scaled from 0 (dark) to 100 (bright). A lamp should turn on only when the reading is strictly below 40. In this virtual example the scale is normalized; it is not the raw analogRead() range of the UNO R3.

ReadingCondition: reading < 40LED state
25trueON
39trueON
40falseOFF
65falseOFF
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
  // Simulated reading, not a real sensor measurement.
  int light = 39;
  if (light < 40) {
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    digitalWrite(LED_BUILTIN, LOW);
  }
}

This sketch uses a fixed example value. Change 39 to 40 to predict the boundary behavior. The activity below lets you vary the normalized reading independently. For a real sensor, measure and calibrate its response before choosing a threshold.

Experiment 5 — Test the threshold

Try readings 39, 40 and 41. LED ON means reading is strictly below 40. Reset returns to reading 0 (LED ON).

0

LED OFF

5. Optional hardware experiment: dim an external LED

You need an UNO R3, USB cable, breadboard, red LED, 330-ohm resistor and jumper wires. Disconnect USB and other power before changing wiring. Do not connect motors directly to output pins.

  1. Connect digital pin 9 to one end of the 330-ohm resistor.
  2. Connect the other end of the resistor to the LED anode (usually the longer lead).
  3. Connect the LED cathode (usually the shorter lead, flat side of the package) to Arduino GND. Verify the polarity for your component.
  4. Check that the resistor is in series and that there is no direct short between pin 9 and GND.
  5. Connect USB, select Arduino UNO in the IDE, select the correct port and upload the sketch below.

For a typical red LED with forward voltage near 2 V, the estimated HIGH-state current with 330 ohms is (5 − 2) / 330 = 0.00909 A, about 9.1 mA. This is comfortably below the UNO R3 recommended 20 mA per I/O pin. Actual current depends on output voltage, LED and resistor tolerances. Check component ratings and board limits.

const int ledPin = 9;
void setup() {
  pinMode(ledPin, OUTPUT);
  analogWrite(ledPin, 64);
}
void loop() {
  // PWM continues without repeated calls.
}

Observe the LED. Change 64 to 128, then 255, and upload again. Compare relative brightness. If the LED does not light, disconnect power and check polarity, resistor connections and the selected pin. Do not remove the resistor to increase brightness.

6. Check your understanding

Experiment 6 — Choose the correct explanation

What does analogWrite(9, 128) normally do on an UNO R3?

Answer and explanation

analogWrite() on pin 9 sets PWM duty cycle. 128 / 255 is about 50.2%. The pin switches between HIGH and LOW; it is not a DAC.

Experiment 7 — Predict and reveal

Predict both outputs: light = 40 in the strict threshold model; analogWrite(9, 255) in the PWM model. Explain your reasoning before revealing the answer.

Answer and explanation

The threshold LED is OFF because 40 is not less than 40. PWM is 100% duty cycle, so the output is held HIGH. With an appropriate series resistor the external LED is on.

7. Independent practice

  1. Calculate duty cycle and ideal average voltage for PWM value 204.
  2. Predict the threshold LED state at readings 0, 40 and 100.
  3. Change the blinking sketch to stay on for 200 ms and off for 800 ms. Calculate its period and ON-time percentage.
Practice solutions
  1. 204 / 255 = 0.8, so 80% and 4 V ideal average.
  2. ON, OFF, OFF respectively.
  3. Use delay(200) after HIGH and delay(800) after LOW. Period = 1000 ms = 1 s; ON-time = 200 / 1000 = 20%.

Key takeaways

  • digitalWrite() controls a digital output state.
  • Hardware PWM controls duty cycle, not a steady analog voltage.
  • Strict inequalities matter at threshold boundaries.
  • Protect external LEDs with a suitable series resistor and respect current limits.
  • Virtual experiments model behavior; real hardware requires correct wiring and verification.

Definitions

Digital output

An output configured to drive a HIGH or LOW logic state.

PWM

A digital signal whose proportion of time HIGH can be controlled.

Duty cycle

Fraction of one period spent HIGH, usually expressed as a percentage.

Threshold

A boundary value used to select an output state.

Worked examples

PWM at 153

Find the duty cycle and ideal average voltage for value 153.

153/255 = 0.60; duty cycle 60%; ideal average 3 V.

Boundary test

Is the LED on at reading 40 if the rule is light < 40?

No. Equality does not satisfy a strict less-than condition.

Common mistakes to avoid

  • Using pin 13 for hardware PWM on UNO R3
  • Confusing a PWM average with steady analog voltage
  • Treating equality as strictly below a threshold
  • Connecting an external LED without a series resistor
  • Powering a motor from a digital output

Practice exercises

PWM calculation

Find duty cycle and ideal average voltage for value 204.

Hint: Divide by 255, then multiply by 100 or 5.

Show solution

80%; 4 V ideal average.

Threshold states

Predict output for 0, 40 and 100 when the condition is light < 40.

Hint: Equality is false.

Show solution

ON, OFF, OFF.

Blink timing

A sketch holds HIGH for 200 ms and LOW for 800 ms. Find the period and ON-time percentage.

Hint: Add both delays.

Show solution

1 s; 20%.

Key takeaways

  • HIGH and LOW represent digital states
  • PWM changes duty cycle, not a steady analog voltage
  • A strict threshold excludes equality
  • Use a series resistor and verify wiring before power

Explore more

Related lesson

Arduino for Beginners: Understanding Inputs and Outputs

Read the lesson