Address
304 North Cardinal
St. Dorchester Center, MA 02124
Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM
Address
304 North Cardinal
St. Dorchester Center, MA 02124
Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM
In This tutorial, We are going to show how You can control The intensity or Brightness of LED Using Potentiometer/PWM
You will learn how to Dynamically make the LED fade-in/out when you rotate the potentiometer knob.
Steps to build the circuit:
Add the LED:
Add the Potentiometer:
In this application, what we want to do is simple: when we turn the knob up (for example turning clockwise), we want the LED brightness to increase.
The minimum knob position will correspond to the minimum brightness – LED turned off. And the maximum position will correspond to the maximum brightness – LED with full intensity, same as if you used digitalWrite() with HIGH.
You Can use This Code
/****************************************************************************************************
Description: Control Led Intensity using Potentiometer/PWM
Website: www.Iduino.co.in
E-mail: support@Iduino.co.in
Modified BY: IDUINO
****************************************************************************************************/
#define LED_PIN 11
#define POTENTIOMETER_PIN A1
void setup()
{
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
int potentiometerValue = analogRead(POTENTIOMETER_PIN);
int brightness = potentiometerValue / 4;
analogWrite(LED_PIN, brightness);
}
#define LED_PIN 11
#define POTENTIOMETER_PIN A1
“#define” is used to Pre-define the values in the program
LED_PIN Defined for digital Pin 11 in code
POTENTIOMETER_PIN Defined for Analog Pin A1
One for the LED pin, one for the potentiometer. This way, we can modify the circuit at any moment, all we’ll need to do in the code is to modify those 2 lines.
void setup()
{
pinMode(LED_PIN, OUTPUT);
}
void setup()
In the void setup(), we need to initialize the mode for the pins we want to use:
pinMode, LED_PIN Defined as output for providing output to your project
int potentiometerValue = analogRead(POTENTIOMETER_PIN);
We enter the void loop(), and the first thing we do is to read the potentiometer value.
To do that we use the analogRead() function, which takes one parameter: the pin number to read from. We store the value inside an integer variable. This value will be in the range 0-1023. In other words, this is a 10 bit number: 2^10 = 1024.
This number corresponds to the voltage we read on the analog pin. The higher the voltage, the higher this number. For 0V you get 0, and for 5V you get 1023.
int brightness = potentiometerValue / 4;
In order to control the LED brightness, we are going to use the analogWrite() function on the LED pin (later on). This analogWrite() function takes a byte value, or in other words, a number between 0 and 255.
So, before we use this function, we need to make sure the value is in the correct range. And as I really like simplicity, you can see that, roughly, 1024 is 255 multiplied by 4 (not exactly this value but this is a good enough approximate for what we need to do). So, if we want to put a value from the range 0-1023 to the range 0-255, we can just divide by 4.
Nothing fancy here, it’s very simple and it works.
int brightness = map(potentiometerValue, 0, 1023, 0, 255);
To do the same thing, you could also use the Arduino map() function which can put a number into a different range.
With this line and the 5 arguments in the function, what we’re saying is: take the potentiometer Value from the range 0-1023, to the range 0-255.
Using map() is great whenever you need to do operations like this, and it works for any integer range.
analogWrite(LED_PIN, brightness);
So now we have computed the brightness to apply to the LED. This brightness variable is an integer number between 0 and 255. Exactly what we need for the analogWrite() function.
analogWrite() takes 2 arguments: the pin to apply the voltage to, and then a byte number between 0-255.
Note: this is only going to work on PWM compatible pins. So if you see that the LED is just fully powered off/on when you run this program, this is probably because the digital pin you’ve chosen is not PWM compatible.
So, what’s going to happen here: the value 0-255 will be translated into a voltage. 0V for 0, 5V for 255, and proportional values in between.
After this instruction, the void loop() exits and is called again, so we directly apply the new potentiometer value to the LED brightness. The update frequency is very high here, so you when you turn the potentiometer knob, you will immediately see the LED brightness change.
In this tutorial you’ve learnt how to control an LED brightness with a potentiometer, using Arduino.
In the end, here’s a summary of what you need to do in the code: