Control Multiple LED Brightness/Dim with One Potentiometer

Hi. I am trying to dim multiple LED's using one potentiometer. I am first completing this in TinkerCad. I am able to turn the LED's on and off, but I seem to be missing something as far as controlling the amount of electricity is going to the LED's to control how bright it is. I am trying to keep the code simple, but any advice would be helpful. I am trying to not use the map function.

//Pin Definitions
const int RedPin1 = 4;//Red1 connected to pin 4
const int RedPin2 = 6;//Red2 connected to pin 6
const int GreenPin1 = 8;//Green1 connected to pin 8
const int GreenPin2 = 10;//Green2 connected to pin 10
int potPin = A0;     // Potentiometer pin
int inputValue = 0; // int to keep track of the values coming from the Potentiometer

void setup() {
  // LED pins 
pinMode (RedPin1, OUTPUT);
pinMode (RedPin2, OUTPUT);
pinMode (GreenPin1, OUTPUT);
pinMode (GreenPin2, OUTPUT);
pinMode (potPin, INPUT);
   
}

void loop() {
  // Program multiple LED Lights to Turn ON and OFF
inputValue = analogRead(potPin);    // gets value from sensor

digitalWrite (RedPin1, HIGH);
digitalWrite (RedPin2, HIGH);
digitalWrite (GreenPin1, HIGH);
digitalWrite (GreenPin2, HIGH);
delay (3000);

digitalWrite (RedPin1, LOW);
digitalWrite (RedPin2, LOW);
digitalWrite (GreenPin1, LOW);
digitalWrite (GreenPin2, LOW);
delay (1000);
  
 
}

To control the brightness of LEDs you must use analogWrite() instead of digitalWrite().

analogWrite() only works with the Arduino pins that have the ~ symbol next to their pin numbers.

So far, you have succeeded! But why do you not want to use that function?

Welcome to the forum

You are reding the pot then ignoring the value that you have read

when you read the pot into an int variable you will get a value between 0 and 1023. To control the brightness of the LEDs they must be connected to PWM pins and you must write a value between 0 and 255 using analogWrite().

To avoid the use of the map() function (why ?) just divide the value read from the pot by 4 and write it to the LEDs

1 Like

Thank you for the feedback. I wasn’t sure if the map function was need, but it appears that the map function must be included for the voltage divider and the analogWrite with the PWM pins. I appreciate the help.

Here is my working copy:

//Pin Definitions
const int RedPin1 = 3;//PWM Pin 3 - Red1 connected
const int RedPin2 = 5;//PWM Pin 5 - Red2 connected
const int GreenPin1 = 6;//PWM Pin 6 - Green1 connected
const int GreenPin2 = 10;//PWM Pin 10 - Green2 connected
int potPin = A0;     // Potentiometer pin
int inputValue = 0; // int to keep track of the values coming from the Potentiometer

void setup() {
  // LED pins 
pinMode (RedPin1, OUTPUT);
pinMode (RedPin2, OUTPUT);
pinMode (GreenPin1, OUTPUT);
pinMode (GreenPin2, OUTPUT);
//pinMode (potPin, INPUT);
   
}

void loop() {

  // Gets the Value from the Potentiometer 
inputValue = analogRead(potPin);  
  
// Map the potentiometer value to the LED brightness range (0-255)
  int brightness = map(inputValue, 0, 1023, 0, 255);
  

  
// Write brightness to each LED individually
analogWrite  (RedPin1, brightness);
analogWrite  (RedPin2, brightness);
analogWrite  (GreenPin1, brightness);
analogWrite  (GreenPin2, brightness);
  
 
}

No, use of the map() function is not necessary. Simply divide the value read from the pot by 4 instead, as I previously suggested

Also incorrect. There is nearly always more than one way to achieve a feature in your code.

For example

  // Map the potentiometer value to the LED brightness range (0-255)
  int brightness = inputValue / 4;

or

  // Map the potentiometer value to the LED brightness range (0-255)
  int brightness = inputValue >> 2;
1 Like