How To Fading LED With Arduino Using POT

Required Hardware

For users who do not have Hackerspace Karachi Arduino KiT v1.0

  • Arduino Board
  • LED
  • Resistor 220 Ohms
  • Potentiometer (Any value)
  • Hook-up Wires

Circuit

Schematic

Code

                    

1. /*
2. Fading LED WITH ARDUINO USING POT
3.
4. This example shows how to fade an LED using the Potentiometer.
5.
6. The circuit:
7. – LED
8. Anode connected to digital pin 4.
9. Cathode connected to Ground.
10. – POT
11. Anode connected to 5v.
12. Signal/Viper connected to analogue pin A2.
13. Cathode connected to Ground.
14.
15. http://hackerspacekarachi.org/
16. */
17.
18. #define ledPin 4 // LED connected to digital pin 4
19. #define potPin A2 // POT connected to analog pin A2
20.
21. void setup() {
22. // nothing happens in setup
23. }
24.
25. void loop() {
26.
27. int potValue = analogRead(potPin); // read analogue value from pot(0 ~
1023)
28.
29. int fadevalue = map(potValue,0, 1023, 0, 255 ); // maps the value from 0~1023 to
0~255
30.
31. analogWrite(ledPin, fadeValue); // sets the value (range from 0 to
255)
32.
33. delay(30); // wait for 30 milliseconds
34. }

1. /*