CodeHub
HomeUploadContact
Back to Hub

Potentiometer controlled led

April 18, 2026 Watch Tutorial

Wiring Schematic

Potentiometer controlled led wiring diagram

Hardware Required

  • Arduino Board
    Buy on Amazon

Source Code

Arduino / C++
1// Define the potentiometer pin
2const int potPin = A0;
3
4// Define your LED pins (All PWM compatible on the Mega!)
5const int ledPins[] = {2, 3, 4, 5, 6};
6const int numLeds = 5;
7
8void setup() {
9  // Set all LED pins as OUTPUT
10  for (int i = 0; i < numLeds; i++) {
11    pinMode(ledPins[i], OUTPUT);
12  }
13}
14
15void loop() {
16  // Read the analog value from the potentiometer (0 to 1023)
17  int potValue = analogRead(potPin);
18
19  // Map the pot value to the number of LEDs (0 to 5)
20  int ledLevel = map(potValue, 0, 1023, 0, numLeds);
21
22  // Loop through and set the brightness
23  for (int i = 0; i < numLeds; i++) {
24    
25    // If the LED should be turned ON based on the pot's position
26    if (i < ledLevel) {
27      
28      // If it is specifically Pin 6, set to max brightness (255)
29      if (ledPins[i] == 6) {
30        analogWrite(ledPins[i], 255); 
31      } 
32      // For the other pins (2, 3, 4, 5), set brightness to 150
33      else {
34        analogWrite(ledPins[i], 150); 
35      }
36      
37    } 
38    // If the LED should be turned OFF
39    else {
40      analogWrite(ledPins[i], 0); 
41    }
42  }
43}
Breadboard
Buy on Amazon
  • 5 led
    Buy on Amazon
  • 5 330ohm resistor
    Buy on Amazon
  • Jumper wires
    Buy on Amazon
  • 10k ohm potentiometer
    Buy on Amazon
  • USB Cable
    Buy on Amazon