LED Dimmer With Potentiometer
by cagoldstein in Circuits > LEDs
12399 Views, 13 Favorites, 0 Comments
LED Dimmer With Potentiometer
This is an instructable that teaches you how to use a potentiometer to dim an LED.
Materials
- Arduino
- Computer
- Breadboard
- LED
- 5 Male wires
- Potentiometer
- USB cable
Getting Started
Plug Arduino into computer with USB cable.
Wiring
After the Arduino is plugged into the computer with the USB cord, we will take the first wire and put one end into ground and the other into j1. Then you will put the second wire from A0 to j3. Then You will put the third wire from 5v to j5.
After that you will put the fourth wire from D9 to j15. Then the fifth and last wire from ground to j17.
Potentiometer and LED Setup
Put the knob facing away from the wires. Plug it into f1 f3 and f5. Then take the LED put the longer leg into f15 and the shorter one in f17.
The Code
These are the variables which tell the computer what specific words mean:
int potPin = A0; This tells the computer that the middle part of the potentiometer, which we are calling potPin, is plugged into A0
int readValue; This tells the computer that whenever we say readValue it means to read the potentiometer
This is the Void Setup which only happens once to set up for the rest of the code:
void setup() { This is just telling you that this is the beginning of the Void Setup
pinMode(9, OUTPUT); This is setting up the light so it can be turned on later
pinMode(potPin, INPUT); This sets up the potentiometer so we can use it later
The next part is the void loop which runs over and over again until you stop it.
void loop() {
readValue = analogRead(potPin); This tells the computer to read the potentiometer whenever we say readValue.
readValue = map(readValue,0,1023,0,255); This converts the numbers from the potentiometer which is from 0-1023, to the numbers for the LED which is from 0-255.
analogWrite(9,readValue); This tells the computer to light up the LED at the brightness the potentiometer is telling it to.
}
This is the whole code by itself:
int potPin = A0;
int readValue = 0;
void setup() {
pinMode(9, OUTPUT);
pinMode(potPin, INPUT);}
void loop() {
readValue = analogRead(potPin);
readValue = map(readValue,0,1023,0,255);
analogWrite(9, readValue);}