Bench NotesUno handbook

Variable-Duty LED Blinking

A potentiometer sets how much of each one-second cycle an LED stays on, from 20% to 80%.

BeginnerLEDResistorPotentiometerSerial

Overview

The LED blinks once per second, every second. Turning the potentiometer changes how that second is split: the LED stays on for 200 ms at one end of the dial and 800 ms at the other, and is off for the rest. The raw potentiometer reading streams to the Serial Monitor so you can watch the input while you turn it.

You will need

  • Arduino Uno R3 (or compatible) and USB cable
  • 10 kΩ potentiometer with legs on a 5 mm pitch
  • LED (any colour) and a 220–330 Ω resistor, or an LED module with a built-in resistor

Wiring

Uno pinConnects toNotes
A0Potentiometer outer legSketch sets A0 as an OUTPUT, LOW. This is the pot's 0 V end.
A2Potentiometer wiper (middle leg)Analog input. Reads 0–1023.
A4Potentiometer other outer legSketch sets A4 as an OUTPUT, HIGH. This is the pot's 5 V end.
11LED anode (long leg) through a 220–330 Ω resistorThe blinking output.
12LED cathode (short leg, flat side)Sketch sets pin 12 as an OUTPUT, LOW, so it acts as the LED's ground.
Wiring for Variable-Duty LED BlinkingArduino Uno A0 (driven LOW) to 10 kΩ potentiometer outer leg 1; Arduino Uno A2 (input) to 10 kΩ potentiometer wiper (middle); Arduino Uno A4 (driven HIGH) to 10 kΩ potentiometer outer leg 2; Arduino Uno D11 to LED anode (+, long leg) through 330 Ω; Arduino Uno D12 (driven LOW) to LED cathode (−, short leg).Connections (each line is one wire)Arduino Uno10 kΩ potentiometerLEDA0 (driven LOW)outer leg 1A2 (input)wiper (middle)A4 (driven HIGH)outer leg 2D11anode (+, long leg)330 ΩD12 (driven LOW)cathode (−, short leg)
Original wiring diagram. Each line is one jumper wire; boxes on a line are series resistors.

How the pins are arranged

Read before wiring

This project powers the potentiometer from analog pins instead of the 5V and GND sockets. A 10 kΩ potentiometer has three legs about 5 mm apart, which matches every second socket on the Uno's analog header. Push its outer legs into A0 and A4 and its wiper into A2. The sketch sets A0 as a LOW output (0 V) and A4 as a HIGH output (5 V), so the pot sees a full 0–5 V span and the wiper on A2 reads anywhere from 0 to 1023.

This works because a 10 kΩ pot draws only about 0.5 mA, far below what a pin can supply. Do not use this trick with a pot under about 5 kΩ, and keep in mind that A4 is also the I²C data line (SDA): the pot and an I²C device cannot share it. See Using analog pins as digital outputs for the details.

The LED goes between pin 11 and pin 12, which are neighbours on the header. Pin 12 is set LOW so it serves as the LED's ground; pin 11 does the blinking. Never push a bare LED straight into these sockets. Without a resistor the LED draws far more than the 20 mA a pin should supply and can damage both the LED and the microcontroller. Put a 220–330 Ω resistor in series with either leg, or use an LED that has a resistor built in. The long leg (anode) goes toward pin 11.

Diagrams

Header placementA 10 kilohm potentiometer with its outer legs in A0 and A4 and its wiper in A2. The sketch drives A0 LOW and A4 HIGH so the pot gets 0 and 5 volts. An LED with a series resistor between pin 11 (anode) and pin 12 (cathode); pin 12 is held LOW so it acts as ground.Arduino Uno R3 — parts plugged straight into the headerGND13121110A0A1A2A3A410 kΩ potentiometerA0 = LOW (0 V), A2 = wiper, A4 = HIGH (5 V)LED with series resistor: anode to 11, cathode to 12
Where the potentiometer and LED sit on the Uno header.
On/off time per secondPot at 0: on 200 ms, then off 800 ms; Pot at middle: on 500 ms, then off 500 ms; Pot at 1023: on 800 ms, then off 200 ms.Pot at 0ON 200 msoff 800 msPot at middleON 500 msoff 500 msPot at 1023ON 800 msoff 200 ms0 ms1000 ms
Every cycle is 1000 ms long. Only the split between on and off changes.

The sketch

adjustable_led_duty_cycle.ino
int potentiometerReading;
int onDurationMs;

void setup() {
  pinMode(A2, INPUT);
  pinMode(A0, OUTPUT);
  pinMode(A4, OUTPUT);
  digitalWrite(A0, LOW);
  digitalWrite(A4, HIGH);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);
  Serial.begin(9600);
}

void loop() {
  potentiometerReading = analogRead(A2);
  onDurationMs = map(potentiometerReading, 0, 1023, 200, 800);
  Serial.println(potentiometerReading);
  digitalWrite(11, HIGH);
  delay(onDurationMs);
  digitalWrite(11, LOW);
  delay(1000 - onDurationMs);
}

How the code works

map(potentiometerReading, 0, 1023, 200, 800) rescales the 0–1023 reading linearly to 200–800 ms. Because the off time is 1000 - onDurationMs, the total is always 1000 ms. The pot is read once per cycle, so a change takes effect at the start of the next blink.

Review result: no changes were needed. We checked the pin setup, the mapping range, and that the on and off delays always add to one second.

What you should observe

  • The LED blinks at a steady rate of one flash per second.
  • Turning the knob toward the A0 end shortens the flash (down to 200 ms); toward the A4 end lengthens it (up to 800 ms).
  • Open the Serial Monitor at 9600 baud: one number from 0 to 1023 appears per blink.