Editorial Solution

First of all, let's do the hardware connection,

We need to connect the potentiometer to the Arduino ADC. We can use any of the potentiometers with values between 1kΩ and 10kΩ.

Similarly, we can connect the 5 LEDs properly to any 5 PWM pins. 

Considering LED drop voltage of ~2 volts and 10mA current, we can use a Resistor with a value

R = (Vcc - Vled) / I

   = (5-2)/0.01 

   = 300 Ω

Circuit connection


Firmware

As we have 5 LEDs connected, the ADC range (0–1023) is divided into 5 equal segments, each corresponding to a specific LED. 

ADC value Segmentation: 

Let’s do ADC value segmentation, as we have 5 leds= 1023/5 i.e. 204.

  • Segment 0 (ADC: 0–203):
    • LED0’s brightness(PWM values) changes from 0 to 255 linearly as the ADC value changes from 0 to 203.
    • LEDs 1, 2, 3, and 4 remain OFF during this segment.
  • Segment 1 (ADC: 204–407):
    • LED1’s brightness(PWM values) changes from 0 to 255 linearly as the ADC value changes from 204 to 407.
    • LED0 remains fully ON (PWM = 255), while LEDs 2, 3, and 4 remain OFF.
  • Segment 2 (ADC: 408–611):
    • LED2’s brightness(PWM values) changes from 0 to 255 linearly as the ADC value changes from 408 to 611.
    • LEDs 0 and 1 remain fully ON, while LEDs 3 and 4 remain OFF.
  • Segment 3 (ADC: 612–815):
    • LED3’s brightness(PWM values) changes from 0 to 255 linearly as the ADC value changes from 612 to 815.
    • LEDs 0, 1, and 2 remain fully ON, while LED 4 remains OFF.
  • Segment 4 (ADC: 816–1023):
    • LED4’s brightness(PWM values) linearly changes from 0 to 255 as the ADC value changes from 816 to 1023.
    • LEDs 0, 1, 2, and 3 remain fully ON.

Code(Methode 1)

const int ledPins[] = {11, 10, 9, 6, 3};  // Pins connected to LEDs (PWM pins)

void setup() {
  // Set all LED pins as outputs  
  for (int i = 0; i < 5; i++) {
    pinMode(ledPins[i], OUTPUT);
  }  
}

void loop() {
  int potValue = analogRead(A0); // Read the potentiometer ADC value (0–1023) connected to analog pin A0  
  int brightness; //To store Brightness value
  
  // Divide the potentiometer range into 5 segments, one for each LED
  if (potValue < 204) {
    // Segment 1: Potentiometer value 0–203
    analogWrite(ledPins[1], 0); // Turn LED 1 fully OFF
    analogWrite(ledPins[2], 0); // Turn LED 2 fully OFF
    analogWrite(ledPins[3], 0); // Turn LED 3 fully OFF
    analogWrite(ledPins[4], 0); // Turn LED 4 fully OFF
    
    brightness = map(potValue, 0, 203, 0, 255); // Map ADC value to PWM values
    
    analogWrite(ledPins[0], brightness); // Control LED 0 brightness 
  }
  else if (potValue < 408) {
    // Segment 2: Potentiometer value 204–407
    analogWrite(ledPins[2], 0); // Turn LED 2 fully OFF
    analogWrite(ledPins[3], 0); // Turn LED 3 fully OFF
    analogWrite(ledPins[4], 0); // Turn LED 4 fully OFF
    analogWrite(ledPins[0], 255); // Turn LED 0 fully ON
    
    brightness = map(potValue, 204, 407, 0, 255); // Map ADC value to PWM values
    
    analogWrite(ledPins[1], brightness); // Control LED 1 brightness (0% to 100%) 
  }
  else if (potValue < 612) {
    // Segment 3: Potentiometer value 408–611
    analogWrite(ledPins[3], 0); // Turn LED 3 fully OFF
    analogWrite(ledPins[4], 0); // Turn LED 4 fully OFF
    analogWrite(ledPins[0], 255); // Turn LED 0 fully ON
    analogWrite(ledPins[1], 255); // Turn LED 1 fully ON
    
    brightness = map(potValue, 408, 611, 0, 255); // Map ADC value to PWM values
    
    analogWrite(ledPins[2], brightness); // Control LED 2 brightness (0% to 100%)  
  }
  else if (potValue < 816) {
    // Segment 4: Potentiometer value 612–815
    analogWrite(ledPins[4], 0); // Turn LED 4 fully OFF
    analogWrite(ledPins[0], 255); // Turn LED 0 fully ON
    analogWrite(ledPins[1], 255); // Turn LED 1 fully ON
    analogWrite(ledPins[2], 255); // Turn LED 2 fully ON
    
    brightness = map(potValue, 612, 815, 0, 255); // Map ADC value to PWM values
    
    analogWrite(ledPins[3], brightness); // Control LED 3 brightness (0% to 100%) 
  }
  else {
    // Segment 5: Potentiometer value 816–1023
    analogWrite(ledPins[0], 255); // Turn LED 0 fully ON
    analogWrite(ledPins[1], 255); // Turn LED 1 fully ON
    analogWrite(ledPins[2], 255); // Turn LED 2 fully ON
    analogWrite(ledPins[3], 255); // Turn LED 3 fully ON
    
    brightness = map(potValue, 816, 1023, 0, 255); // Map ADC value to PWM values
    
    analogWrite(ledPins[4], brightness); // Control LED 4 brightness (0% to 100%) 
  }
}

Code(Methode 2)

const int ledPins[] = {11, 10, 9, 6, 3};  // Pins connected to LEDs (PWM pins)

void setup() {
  // Set all LED pins as outputs
  for (int i = 0; i < 5; i++) {
    pinMode(ledPins[i], OUTPUT);
  } 
}

void loop() {
  int potValue = analogRead(A0);          // Read potentiometer value (0–1023) 
  int segment = potValue / 204;           // Determine which segment the value falls into (0–4)
  int brightness = potValue % 204;        // Get the remainder for brightness mapping in the segment
 
  // Smooth transition for LED brightness 
  for (int i = 0; i < 5; i++) {
    if (i < segment) {
      analogWrite(ledPins[i], 255);       // Fully on LEDs in previous segments  
    } else if (i == segment) {
      analogWrite(ledPins[i], map(brightness, 0, 203, 0, 255));  // Gradually vary brightness of current LED  
    } else {
      analogWrite(ledPins[i], 0);        // Turn off LEDs in next segments  
    }
  }

  delay(10);	// Short delay to stabilize transitions
}

Code Explanation

Logic:

  • LEDs in earlier segments are fully ON.
  • The current segment's LED brightness is gradually adjusted using map().
  • LEDs in later segments are turned OFF explicitly.

Method 1: Explanation

  • Approach: The potentiometer range is divided into five fixed segments using if and else-if conditions.
  • Strengths: Simple to understand and implement.
  • Weaknesses: Code is repetitive and less scalable.
  • It is harder to extend for more LEDs or segments.

Method 2: Explanation

  • Approach: The potentiometer range is dynamically divided into segments using integer division and a loop.
  • Strengths: Compact, efficient, and scalable.
  • Weaknesses: Slightly more complex for beginners to follow.
  • Easy to extend for more LEDs or segments.

Output

 

Output Video

 

 

 

Submit Your Solution