DIY ELECTRONICSTR

← All projects

LED Brightness with a Potentiometer

Beginner20 min

Set an LED's brightness by turning a potentiometer's knob: with the knob at one end the LED is off, and it gets brighter as you turn towards the other end. The value read and the brightness given to the LED are also written to the Serial Monitor. It teaches reading an analog input with analogRead, turning one range into another with map, and using PWM with analogWrite. No extra library needed.

Build it step by step in the app

What you need

Wiring steps

  1. 1

    Connect the power rails (5V and GND)

    Connect the power rails (5V and GND)

    Plug a black jumper wire into one of the GND pins on the Arduino's power header and connect its other end to the breadboard's bottom blue (−) rail. Plug a red jumper wire into the 5V pin on the same header and connect its other end to the bottom red (+) rail. The potentiometer is powered from these two rails, and the LED's resistor goes into the (−) rail.

    • Arduino GND → the bottom blue (−) rail (black wire)
    • Arduino 5V → the bottom red (+) rail (red wire)
  2. 2

    Add the LED and its resistor

    Add the LED and its resistor

    Plug the red LED's long leg (+) into column 11 in the bottom half of the breadboard and its short leg (−) into column 10. Put one end of the 220 Ω resistor in column 10 and the other end in the bottom (−) rail. Then connect the Arduino's pin ~9 to column 11 with a blue jumper wire (LED_PIN 9). The ~ in front of a pin number means that pin can do PWM; to set the brightness, the LED must be on such a pin (3, 5, 6, 9, 10 and 11 on the Uno).

    • red LED long leg (+) → column 11, bottom half
    • red LED short leg (−) → column 10, bottom half
    • 220 Ω resistor (leg 1) → column 10, bottom half
    • 220 Ω resistor (leg 2) → the bottom blue (−) rail
    • Arduino pin 9 → column 11, bottom half (blue wire)
  3. 3

    Add and wire the potentiometer

    Add and wire the potentiometer

    Plug the potentiometer's three legs into columns 4, 5 and 6 in the bottom half. Connect column 4 to the bottom (+) rail with a short red wire and column 6 to the bottom (−) rail with a short black wire. Then connect the Arduino's A0 pin to the middle leg's column, column 5, with a yellow jumper wire (POT_PIN A0). The two outer legs give the potentiometer 0 V and 5 V; as you turn the knob, the middle leg gives a voltage between the two, which the Arduino reads as a number from 0 to 1023.

    • potentiometer left leg → column 4, bottom half
    • potentiometer middle leg → column 5, bottom half
    • potentiometer right leg → column 6, bottom half
    • column 4, bottom half → the bottom red (+) rail (red wire)
    • column 6, bottom half → the bottom blue (−) rail (black wire)
    • Arduino A0 → column 5, bottom half (yellow wire)

Arduino sketch

// Middle leg of the potentiometer: A0
const int POT_PIN = A0;
// LED on ~9: analogWrite needs a PWM pin
const int LED_PIN = 9;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // From 0 (one end) to 1023 (the other)
  int potValue = analogRead(POT_PIN);

  // Turn 0–1023 into PWM's 0–255
  int brightness = map(potValue, 0, 1023, 0, 255);
  analogWrite(LED_PIN, brightness);

  Serial.print("Potentiometer: ");
  Serial.print(potValue);
  Serial.print("  Brightness: ");
  Serial.println(brightness);
  delay(200);
}

What the Serial Monitor shows

SERIAL MONITOR · 9600 BAUD
Potentiometer: 0  Brightness: 0
Potentiometer: 256  Brightness: 63
Potentiometer: 512  Brightness: 127
Potentiometer: 768  Brightness: 191
Potentiometer: 1023  Brightness: 255

What you will see

With the knob at one end the LED is off; as you turn it towards the other end it slowly gets brighter, and at the very end it lights at full brightness. In the Serial Monitor the potentiometer value (0 to 1023) and the brightness (0 to 255) change with the knob. If the value never changes, check that the yellow wire is in the middle leg's column (5). If the value changes but the LED stays the same or never lights, check that the LED's wire is on pin ~9 and its long leg is in column 11.

In the app, tick off every wire, match the parts to your inventory and pick up where you left off.

Build it step by step in the app