Working with a Potentiometer and an LED

Use of a potentiometer to control brightness of an LED.

May 22, 2019

35852 views

8 respects

Components and supplies

1

Breadboard (generic)

1

LED (generic)

1

Single Turn Potentiometer- 10k ohms

1

Resistor 1k ohm

1

Jumper wires (generic)

1

Arduino UNO

Apps and platforms

1

Arduino IDE

Project description

Code

Code

arduino

Downloadable files

Schematic Diagram

Schematic Diagram

Circuit Diagram

Circuit Diagram

Breadboard Diagram

Breadboard Diagram

Comments

Only logged in users can leave comments

Anonymous user

4 years ago

Amazing! Could you provide insight as to why you have the 'inputVal/4' in line 12? Also, I changed the denominator from'4' to '10' which made the LED intensity change much more gradually. However, when I changed it from '4' to '2', the LED goes from zero to full brightness, then goes back to zero and again to full brightness in one single turn of the potentiometer. Any ideas, why?

Anonymous user

2 years ago

**tl;dr:** it's because each PWM unit is approximately eqaul to 4 analog units. Hi! You might already know the answer but for others who are also curious like us, it's because `analogRead()` will return a value from 0-1023 while each PWM digital pin where the LED is connected (marked by ~ on the Arduino board) has only a range of 0-255. So to get the corresponding PWM value from the result of `analogRead()`, we need to map the values of analogRead() to the values of PWM. From the tutorials I have seen, this is done by using `PWM_LED_VALUE = map(analogRead(PIN_POT), 0, 1023, 0, 255)` which results to the PWM value that we need for our LED. The expression is basically read as `target_PWM_value = map(current_analog_value, analog_range_low, analog_range_high, PWM_range_low, PWM_range_high)`. AND NOW, why did OP use `inpuVal/4` instead of the mapping? **It's because the PWM values obtained from the mapping is approximately equal to 4 analog units (1023/255 = 4.01176).** I think this is a good Arduino hack although others might find it somewhat lazy. Now, when you changed the denominator to 10, this basically meant that for each PWM unit, there will be 10 analog units, so if the value you get is from `analogRead()` is either 0,1,2,3...or 9, this will just be interpreted as 1 PWM unit which explains the gradual intensity of the LED light. Furthermore, you will be only to get a maximum of 102 PWM units from this denominator since 1023/10 = ~102, which means you will not be able to make the LED fully bright since the max PWM value is 255. When you made the denominator to 2, this meant that for each PWM unit, there will be 2 analog units. Since 510 analog units is already the max 255 PWM units, when you received 511 and 512 from `analogRead()`, the value will be mapped to 0 PWM unit again. This explains the turn on-turn off behavior of the LED light. I hope this has addressed some of your curiosity. Thank you for reading, and have a good day! :-)