LED Dimmer With Potentiometer

2,521

9

9

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);}

Share

    Recommendations

    • Big and Small Contest

      Big and Small Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • PCB Contest

      PCB Contest

    9 Discussions

    0
    None
    BenSides

    11 months ago

    GArbagw

    0
    None
    tliguori330

    11 months ago

    cool way to combine more than one skill. i can use this in class

    0
    None
    Caoliner

    12 months ago

    I heard google is about to buy u guys out

    0
    None
    Caoliner

    12 months ago

    Incredible project. It consists of all the correct lines of code, and it is a work of art. Their teacher shouldn't even give them 100 he/she should give them a approximately 217.34

    0
    None
    crazypj

    12 months ago

    Is the computer or Arduino running pot and LED? Seems a complicated way tp dim LED, BUT, the explanations of what and why are great

    1 reply
    0
    None
    cagoldsteincrazypj

    Reply 12 months ago

    The computer is sending the code to the Arduino witch then controls the potentiometer and LED

    0
    None
    Swansong

    12 months ago

    Great first instructable! Thanks for sharing :)