Introduction: How to Use a Potentiometer to Create a Variable Speed "Knight Rider" With LED's

For my first Instructable I wanted to share the idea that got me into the Arduino in the first place. I wanted to create a Knight Rider effect for my Jeep. After going through the turorials and scouring forums I managed to get help from a fellow Redditor (u/ripred3) It is his code that I based this project off of.

What you will need:

1- Arduino (I am using an Uno)
2- at least 1 breadboard (I am using 2 as it keeps things easier and neater)
3- jumper wires
4- LED's (I am using 10, but this application can utilize up to 15)
5- Resistors (this will vary depending on the type of LED you are using. for this aplication I am using 220 ohm)
6- Potentiometer (I am using a 10k Ohm POT)

Step 1: Let's Wire Up the POT!

To set up the POT:

I used a Green, Orange, and Blue jumper.

The way a potentionmeter works is this:
There are 3 poles (Left, Center, Right). The center connection is a wiper that moves from one end to the other, giving it a range from very low resistance (around 1-50 ohms depending on the tolerance and quality of the pot) to around 5K or 10K ohms when measured between the center 'wiper' and the left or right connection.

On one of the boards place the pins of your POT into rows 1, 3, and 5 (Left, Right, and Center)
- The left pin will go to the Gnd (Blue)
- The Center pin will be connected to Analog 0 (A0) (Orange)
- The Right pin will go to the 5v power (Green)

Step 2: Now for the LED's!

This is pretty straight forward. to save time and sanity I have the negative (short pole) of the LED hooked into the power rail of the breadboard with the positive (long pole) into individual rails. I then gap a resistor over the middle of the breadboard where I have my jumper wires (in this application I used all white).

The board I am using is a small breadboard with 30 rows. I spaced my LED's out evenly starting at row 1 and leaving 2 empty rows between each. Obviously you can set this part up however you want.

You will run a jumper from each row, in order, to the digital pins on the Arduino starting with D2 (D0 and D1 are generally left open for debugging and writing to the serial monitor)

Now depending on how many LED's you are using, you may end up plugging into some of the Analog pins. This will be explained in the code.

Run one jumper wire from the power rail (negative for the LED's) into the Gnd on the Arduino board

Step 3: And Now for the Code...

Again a very special thank you to Reddit user ripred3 for the code here. 

// We set up a global array of integers to hold pin numbers. 
// We can then loop through these easier at runtime.
// Notice we use some of the analog pins as binary outputs
// but we skip the one we'll use for input (A0 for reading the pot).
// The pins named that don't start with an 'A' are the digital-only pins.
// We skip 0 and 1 since it is common to leave those free because they are
// the serial transmit and receive pins on the ATMEGA328 chip and are useful
// to use for debugging output to a serial monitor, but I digress:
int led_pins[15] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

void setup() {
  pinMode(A0, INPUT);  // set up the pin for reading the potentiometer

  // now we have to set up our LED pins as OUTPUTs:
  int i;
  for (i = 0; i < 15; i++) {
    pinMode(led_pins[i], OUTPUT);

    // Default each LED to off. This assumes the anodes of the LEDs are connected
    // to the output pins (with a 330 ohm resistor in between for current limiting)
    // and the other side of all of the LEDs (the cathodes) are connected to ground.
    digitalWrite(led_pins[i], LOW);

void loop() {
  // we set up a variable to index into our array of pin numbers.  We'll default it to 0 (the first
  // LED) and let its range go from 0 to 14 inclusive (the fifteenth value in our array).
  int index = 0;

  // We'll also need a variable to tell us which direction we are 'bouncing' the LEDs.
  // We'll start with 1 and use -1 to go the other direction:
  int direction = 1;

  while (1) {
    int value = analogRead(A0);  // get the position from 0 to 1023

    // We'll use this value as the number of milliseconds to delay between advancing to between LED's.
    // There are 1000 milliseconds in a second so this gives us a good range of delay from 1/1000 to a
    // little more than 1 second.

    // But first we need to turn ON the 'current' LED (the one indexed by 'index'):
    digitalWrite(led_pins[index], HIGH);  // turn the LED pin HIGH, '1', +5v, all the same thing...

    // Now delay for a time relative to the potentiometer.  The 'delay()' function waits the number
    // of milliseconds we give it before it returns so this works perfect:

    // Turn OFF the current LED before advancing:
    digitalWrite(led_pins[index], LOW);

    // we advance our index number so on the next pass we're talking about the next LED:
    index += direction;

    // We inspect our index to see if we're about to go past either end.  If we have reached either
    // end we will simply multiply the direction by -1 each time we reach one end or the other and this
    // will toggle it from 1 and -1, getting it ready for the next pass:
    if ((index == 0) || (index == 9)) {
      direction *= -1;

And there you have it. You should now be able to control your moving LED's with the potentiometer.


turbiny (author)2015-10-03

How should i do this without arduino ( i have uno and nano but i wanna put it in a car) for about 3 rows of 15-20 leds?
Thats about 60 leds i wanna use it as s blinker in the rear to point to the side i'm turning

wilgubeast (author)2013-12-06

Any chance you have a video of it in action?

About This Instructable




More by aridings:How to use a Potentiometer to create a variable speed "Knight Rider" with LED's
Add instructable to: