Fading or controlling led brightness using arduino uno and potentiometer/variable resistor is not a very hard task. Arduino predefined libraries made it very easy to fade led with arduino uno. Whats going behind the arduino code predefined commands/instructions at software and at hardware level is important to understand. Students normally start with the pre-written arduino code examples available with in the arduino ide and never try to understand what actually is happening behind the visible spectrum. So when they move to higher level of arduino programming they face hurdles in properly designing the circuit and implementing the code logic for the particular hardware. In this tutorial i will explain how to control brightness of a led using arduino and a potentiometer/variable resistor.
One can find the arduino analog fade and arduino serial in/out example in the arduino ide. The examples are more or less same to what i am going to discuss in this tutorial. I will go deeper and try to highlight every piece of information widely and as easily as i could.
Potentiometer is used in the circuits where we need a variable resistance to control current and voltage. Have you noticed that, the speaker that you have in your home, you move its knob in clockwise and anti clock wise direction to set the volume. Actually behind the knob their is a potentiometer, that is you are varying the resistance to set the volume. Like wise in many other home appliances potentiomenter is used for the same purpose(old TV’s , old radios etc).
What happens at the potentiometer side?
When we rotate the knob of the potentiometer we actually are increasing or decreasing the resistance. Remember potentiometer is a variable resistor nothing else. The range of the potentiometer resistance is written on the potentiometer or you can manually check the data sheet of the particular potentiometer to verify its resistance.
Now recall Ohms law that when ever resistance in a circuit increases the current decreases. Also current is directly proportional to voltage. So with the increase in voltage current increases and with decrease in voltage current decreases.
If we consider the above statement it is clear that we can directly connect the led with potentiometer and fade/dim/control its brightness by rotating the knob of the potentiometer. Then why we are trying to fade led with potentiometer using arduino? What are the pros and cons of fading led with potentiometer and arduino?
Why potentiometer with Arduino?
Led brightness control with potentiometer and Arduino
How the system works?
Arduino analog input pin is connected to output of potentiometer. So arduino ADC(analog to digital converter) analog pin is reading the output voltage by the potentiometer. Rotating the potentiometer knob varies the voltage output and arduino reads this variation. Arduino converts the input voltage to its analog pin in to digital form. The digital value ranges from 0 to 1023 volts. 0 represents 0 volts and 1023 represents 5 volts. Arduino ADC is 10 bit which means it cam sample input voltage and output it in between range of 0 to 1023 volts (2^10 = 1024). Arduino works on 5 volts so its ADC input voltage range is also between 0 to 5 volts. Arduino boards working on 3 volts input range for ADC is 0 to 3 volts.
Note: Applying greater voltage to arduino analog pins will damage your arduino board. So in our case the potentiometer voltage output must not increase 5 volts.
Coming to the schematic of the circuit. Apply 5 volt to 12 volt to the +Pin(anode) of potentiometer and connect -Pin(cathode) to ground. Connect the output pin of potentiometer to the Analog input Pin-A0 of the ardunio. Now connect the led +pin(anode) to the pin#9 of Ardunio. Pin#9 is used as an analog output pin. Few arduino uno pins can be used to output variable voltage and pin#9 is one of them. It outputs analog values in the form of pwm(pulse width modulated signal). Connect the other end of led to ground in series with a resistance. Resistance can be in range of 120 ohm to 4.7 k ohm. Make the circuit and upload the sketch in your ardunio uno.
Voltage Variation Fades Led
When you rotate the knob of the potentiometer resistance decreases and the current starts flowing. As the current increases the voltage increases and their is a change in voltage which is detected by the analog input A0 pin of the Arduino. We analyze this change in voltage in our sketch(code) and then output the change to Pin#9 to which our led is connected. Led fades when we constantly rotate the potentiometer knob in clockwise and anticlockwise direction.
Its necessary to divide the analog reading by 4 because analogWrite() function outputs analog values in between range from 0 to 255. Where 0 represents low and 255 represent high, and analogRead() inputs value from 0 to 1023. At last the analogWrite() function outputs the analog value. You will now see led fading/brightness varied if you rotate the knob of potentiometer.