Arduino LED Bar Graph with a 4017 Counter and Potentiometer

In this tutorial I will show you how to drive a LED bar graph using an arduino and a potentiometer. This is fairly easy to do and a nice tutorial about it already exists on the arduino website. This tutorial will be different in that we will use a 4017 counter to save pins from the microcontroller. Additionally, I will demonstrate the concept of “persistence of vision”.

See below a video of the circuit in action.

If you are able to reproduce this project, have any question, correction, or suggestion, please, let me know by leaving a comment.

Hardware Required

  • Arduino board
  • (1) Rotary Potentiometer
  • (1) 4017 Decade Counter
  • Breadboard
  • Some wires

Schematic

Arduino LED Bar Graph Driven with a 4017 Counter and a Potentiometer

(Click the image to enlarge)

This is how it works:

The 10 LEDs are wired to the outputs of the 4017 counter. There is no need to protect them with current limiting resistors because the maximum current per pin is 10 mA. The arduino controls the 4017 counter using just 2 digital pins: digital pin 2 is connected to the counter’s clock pin, and the digital pin 3 is connected to the counter’s reset pin. The arduino runs a program that reads the analog input from the potentiometer (returning a value between 0 and 1023). The value read is mapped to the range 0-9 and is used to decide the number of LEDs to turn on.

Using the counter we face the limitation of not being able to turn on more than one LED at the same time. To solve this, we quickly turn on/off the LEDs in succession, and thanks to the ”persistence of vision” effect, all the LEDs appear to be on at the same time.

Below is the picture of the circuit in real life.

Arduino LED Bar Graph with a 4017 Counter and Potentiometer

(Click the image to enlarge)

CODE

/*
 * LED Bar Graph controlled with an external potentiometer.
 *
 * A 4017 counter is used to save pins on the arduino.
 * The technique of "Persistence of vision" is used to turn on
 * many LEDs at the same time.
 *
 * Developed by Leonel Machava
 * http://codentronix.com
 *
 * This code is release under the "MIT License" available at
 * http://www.opensource.org/licenses/mit-license.php
 */

/* Digital pin connected to the counter's clock pin */
int clockPin = 2;

/* Digital pin connected to the counter's reset pin */
int resetPin = 3;

/* Analog pin connected to the potentiometer wiper */
int potPin = 0;

void setup() {
  pinMode(clockPin,OUTPUT);
  pinMode(resetPin,OUTPUT);
  reset();
}

void loop() {
  /* Read the analog value from the potentiometer. */
  int potValue = analogRead(potPin);

  /* Map the value to the range 0-9. */
  int n = potValue * 10 / 1024;

  /* Turn ON/OFF quickly the first n LEDs. The n LEDs
     will appear to be ON at the same time due to the
     "persistence of vision" effect. */
  for( int i = 0; i < n; i++ ) {
    clock();
  }

  reset();
}

/*
 * Sends a clock pulse to the counter making it advance.
 */
void clock() {
  digitalWrite(clockPin,HIGH);
  delay(1);
  digitalWrite(clockPin,LOW);
}

/*
 * Resets the counter making it start counting from zero.
 */
void reset() {
  digitalWrite(resetPin,HIGH);
  delay(1);
  digitalWrite(resetPin,LOW);
}

Click here to download the source code (arduino_led_bar_graph_4017_pot.pde / 1.38 Kb)

If you like this article please share it by clicking one of the buttons below, follow me on twitter, or register on my RSS to receive article updates. Thanks!

Leave a comment ?

14 Comments.

  1. AlexHolsgrove

    I’ve built the circuit, but the LED bar-graph seems to dim when each additional segment is lit. Using an Arduino Uno (AtMega328) and the HEF4017BP decade counter (Phillips)

    • Hello Alex.
      In theory the led bargraph shouldn’t dim when each additional segment is lit because they are not lit at the same time. They are multiplexed and due to the persistence of vision effect “they seem” to be lit at the same time.

      Double check your connections are correct. Please, let me know about your result.

  2. OK, that works, but if you had 20 LEDs could you string together 2 4017 counters and make it work as one longer bar graph for more precision? Or would 10 be the limit? And if 20 is possible, where’s the limit? Just wondering, this brought up some fascinating possiblities.

    • The circuit is not limited to 10 LEDs. You can string 2 or more 4017 counters. Just make sure you know how to cascade 2 or more 4017 counters.

  3. How does it go in reverse in this video ?

    I am trying with standard leds, and cant get reverse with my code:

    void loop() {

    int n = random(10);

    for (int i=-1; i<n; i++){
    clock();
    }

    reset();
    }

    I am programmer by the way, but electronics noob, is it possible that reverse aint working because something isn't connected ok ?

    • Sorry for my late reply.
      It is not easy to do that programatically using the constructed circuit. Why: (1) persistence of vision will get in your way. (2) the code you have provided does not help much to acomplish that.
      My code works because it reads the value of the pot in realtime and turns on the LEDs accordingly. So if at a given time the pot reads 8 then the first 8 LEDs will be lit. Later if the pot is gradually turned to 4 you will see the bargraph going in reverse ending up with 4 LEDs lit.

  4. How long did it take before this burnt out? You need the current limiting resistors. No question.

  5. Arduino LED Bar Graph with a 4017 Counter and Potentiometer | codeNtronix

  6. Hi, where can I buy this led bar graph?

  7. Unquestionably consider tyat that you stated. Your favourite
    reason appeared to be on the internet the easiest thing to be mindful of.
    I say to you, I definitely get irked while other
    folks consider concerns that they plainly don’t recognise about.
    You managed to hit the nail pon the top and outlined out the
    entire thing without having side-effects , other
    folks could take a signal. Will probbly be again to gett more.
    Thanks

  8. is implementing – Foreclosure Diaries, a documentary regarding the financial disaster and possesses blogged on foreclosure related issues for American Banker game stefen wisniewski oakland raiders road jersey 61
    nike mens nfl white the sites in most cases
    keep quote for you personally and lock the quote in for a couple of days.

  9. Very good article. I will be facing some of these issues as well..

  10. Right now it appears like Movable Type is the preferred
    blogging platform available right now. (from what I’ve read) Is
    that what you are using on your blog?

Reply to Epifanio ¬
Cancel reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Trackbacks and Pingbacks: