Introduction: LED Dimmer With Potentiometer

This is an instructable that teaches you how to use a potentiometer to dim an LED.

Step 1: Materials

  1. Arduino
  2. Computer
  3. Breadboard
  4. LED
  5. 5 Male wires
  6. Potentiometer
  7. USB cable

Step 2: Getting Started

Plug Arduino into computer with USB cable.

Step 3: 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.

Step 4: 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.

Step 5: 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);}

LED Contest 2017

Participated in the
LED Contest 2017

Arduino Contest 2017

Participated in the
Arduino Contest 2017