You can easly control the current running through your LED by adding a potentiometer as part of your circuit.
To control the LED with Arduino programming you attach the potentiometer to your analog in and let your Arduino program decide how much to dim the LED depending on the input you get from the potentiometer.
The input from analogRead returns a value between 0 and 1023. The analogWrite takes values between 0 and 255. The code below show you have to convert your analog in value to make your LED shine as bright as possible when the potentiometer is fully on.
-
int ledPin = 3;
-
int potentiomenterInput = 0;
-
-
void setup() {
-
pinMode(ledPin, OUTPUT);
-
}
-
-
-
void loop() {
-
-
potentiomenterInput = (analogRead(0)/4); // Divides input 0-1023 to resemble to 0-255
-
-
analogWrite(ledPin, potentiomenterInput);
-
// The delay can be change to get the desired dimming effect
-
delay(20);
-
}
LEDs cannot correctly be controlled this way and the Arduino isn’t doing anything here.
@MAS3 How do you mean that is is not possible to control the LED with this circuit?
A LED needs a certain voltage to work.
The intensity of a LED is to be controlled by adjusting its current, not its voltage, yet in your first sketch you are controlling voltage.
The 220 Ohm resistor will control current, the potentiometer just controls voltage.
You will see that your first sketch will work very poorly with al large dead zone in your potentiometer.
The second one however will work beautifull with a nice linear intensity of that LED.
(Our eyes are better served with a logaritmic intensity, but never mind that).
I can add a circuit where programming is involved. This example just shows how a potentiometer works.