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 Ω
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.
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%)
}
}
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
}
Logic:
map()
.Method 1: Explanation
Method 2: Explanation