need help building an up/down counter circuit

Discussion in 'The Projects Forum' started by papops, Mar 21, 2016.

  1. papops

    Thread Starter New Member

    Mar 21, 2016
    Hi All,

    I'm trying to build an up/down counter circuit using a sequential counter. Ideally, it would count 0 to 10 and stop, then count down to 0 and stop again, then repeat the up count. The intent of the circuit is drive a series of LED's to come on sequentially and stay on to the end of the up count, then turn off sequentially on the down count.

    This isn't a homework project but it is a hobby project. any ideas will be greatly appreciated.

  2. k7elp60

    Active Member

    Nov 4, 2008
    There are a number of up/down counters that will count up to 10 and then count back down to 0. As I recall they all have binary out put, so you would need a decoder circuit if you want 1,2,3,4,5,6,7,8,9,10. Tell is more of the LED's, like is there one to be turned on with each count? How would like to power the unit, and do you want to use a push button switch to activate the counting?
    GopherT likes this.
  3. GopherT

    AAC Fanatic!

    Nov 23, 2012
    @papops (Walt)

    Please tell us how you want the up/down counting to occur. Will the circuit pulse automatically to tick the counts up or will there be some other trigger (external to the circuit discussed so far) or will a single push button cause each press to increment the counter.

    Also, once 10 is reached, do you want a numeral 10 to be displayed or a series of dots/LEDs (representing 0 (no dots) and one dot for each to 10). Or what ever else you can describe

    When 10 is reached, does any action have to be taken to make the counter reverse? Or just additional ticks of clock, button or external pulse train?

    Finally, you have 11 total positions right now, 0, 1 ... 10. Is that correct?

    Sorry, it looks like I rambled on and repeated some of @k7elp60 questions.
  4. papops

    Thread Starter New Member

    Mar 21, 2016

    GopherT and K7elp60,
    Thanks for the reply. This circuit needs be autonomous, in other words, turn on the power and it starts running. it will drive 10 LED's. Each LED needs to be turned on in sequence and stay on until the #10 LED has been turned on, then each LED needs to turn off in sequence until they are all off. Then, the sequence starts all over again. The counter chip gets it's clock pulses from a 555 timer. There are numerous up/down counter chips but they all turn on one output at a time. I need to have each of the outputs stay on after being turned on and stay on until it is turned off again in sequence. I'm currently using a CD4017 up/down counter which has 10 outputs BUT they turn off when the next one turns on. I'm trying to come up with some kind of diode matrix to do what I need but I'm not having much luck. I'm using a 4013 flip/flop triggered by the #10 and #0 count outputs to reverse the sense of the up/down count sequence. That part seems to work O.K. but of course, I need a way to keep the LED's on until they are turned off during the down count.

    I hope that has made what I'm trying to do a little more clear. Again, thanks for your interest.

  5. Picbuster


    Dec 2, 2013
    Forget the good old 555 timer its to limited. use a PIC12f675 or a bigger one.
    All type of counts stop, up down, flash at different output pins is possible and build in no time. programming is simple costs are low and hardware is limited to a few components. Flexibility/applications are limited by our brains.
    I willing to give you basic schematic and a simple program to start from.
    Let me know.
  6. Dodgydave

    Distinguished Member

    Jun 22, 2012
    Sounds like a knight rider display, the cd4017 only counts up,not down,

    If its a knight rider display, use a lm3914 and 555 timer ramp generator.
  7. AnalogKid

    Distinguished Member

    Aug 1, 2013
    Sounds like a bar graph display. There is a great chip for this, but it takes an analog voltage. Since you have the clock, counter, and up/down reverser all worked out (excellent!), then one approach that sounds complicated but isn't too bad is to whip up a digital to analog converter (D/A) to take your increasing/decreasing binary count and turn it into an analog voltage, and use that to drive a chip that was designed from the ground up to do exactly what you want, the LM3914 linear bar/dot display driver.

    The 4017 is a great part, but it never will do what you want and there is no other part that is the same thing plus up/down (unless you want to learn how to program a PIC). There are other circuit tricks to turn a decoded output group into a bar graph, but with 10 steps you would need a 24 V supply and level translating driver transistors and and and. Another approach is to use a left/right shift register to produce the outputs, and have your counter and reverser drive it.

    A significant question is whether or not you want an all-off state when the display is reversing at the low end. For example, does the number of LEDs that are on go like this:

    3, 2, 1, 2, 3 ...


    3, 2, 1, none, 1, 2, 3 ...

    Big difference.

  8. Bernard

    AAC Fanatic!

    Aug 7, 2008
    555, 4515 4 line binary to 1 of 16, 4516 binary up-dn counter, 4071 OR, & ?
    Old preliminary stacker drawing to convert sequential pulses to bar graph style. Stacker 00000.png
    The 4516 has output low, so 4514 might be better with output high.
    If 74AC32 was used for ORing it has enough drive to light LEDs with 5V or 6V supply.
    10 stage decoder, BCD up-dn counter also available.
    Last edited: Mar 23, 2016
  9. k7elp60

    Active Member

    Nov 4, 2008
    I agree with Picbuster by using a PIC, But I did some research and if you use the 4017 and and 5ea 4013's connected as D flipflops between the 4017 outputs and your LED's, . Connect the LED's to the Q output, with the set and clear connected to ground. When the output of the 4017 goes high the D flip-flops with change state and the LED will come on. When that same output goes high again the LED will turn off. This will be a one way counter so for example if #1 LED turns on/ it will be the first one to turn off. I have lots of parts, and I will be glad to give part if you need some.
  10. papops

    Thread Starter New Member

    Mar 21, 2016
    I think it's time I started using Pic or Arduino to do some of these projects but it's been a long, long time since I did any kind of programming. I used to know Basic and a few brands of PLC logic programming, but again, that was so long ago it's like starting all over again. But I'm willing to give it a shot. So if you can help point me in the right direction, I'd appreciate it.
  11. AnalogKid

    Distinguished Member

    Aug 1, 2013
    Programing a PIC is nothing like the systems you are familiar with. Even PIC Basic is a very real learning curve an experienced Basic programmer. This sounds like an excellent get-your-feet-wet project if that's what you're after, but it will take much longer than wiring up a few chips on perf board.

  12. Picbuster


    Dec 2, 2013
    Ok I will compile some stuff for you.
    Schematic and simple program will speed it's way to you soon.
  13. Picbuster


    Dec 2, 2013
    A simple program. I did avoid all the pointer stuff. next step get the free mplab with xc8 compiler from microchip.
    This program will work in most of the 12f and 16F chips (move pic16f690 or higher when more outputs are needed)
    Let me know if you face a problem. I will be pleased to help you.

    Code (C):
    2. #include <htc.h>
    3. #include <pic12f675.h>
    4. #include <xc.h>
    5. #define _XTAL_FREQ 4000000  // Needed for delay_ms macro
    7. //#pragma CONFIG(FOSC_INTRCIO & CP_OFF & MCLRE_ON & WDTE_OFF); << depending from your wishes see manual
    9. #define Output GPIO5
    10. #define P3_On  GPIO4
    11. #define Led  GPIO2  // pin5
    12. #define On  1
    13. #define Off  0
    14. //========= end of pic specific
    15. int Wanted_value;
    16. int cnt;
    17. int Flash;
    18. unsigned int result;
    21. //=========================== interrupts ====================
    22. static void interrupt
    23. peripheral_interrupt(void)
    25. /*
    26. //================== when adc under interupt is needed========
    27. //================== set the int  (  ADIE=1;
    28. // ========== ask in main for ana do ==>  ADCON0bits.GO = 1;
    29. // == or  led a timer do it for you
    30. // == doing so will provide you always an value
    32. if (ADIF)
    33.   {
    34.   ADIF=0;
    35. //-------------------- read voltage  -------------------------
    36.    while (ADCON0bits.GO_DONE);
    37.    result = ADRESH;
    38.    result <<=8;  // shift highbyte left
    39.    result |= ADRESL;  // and do low
    40. //-------------------------------------
    42.   }
    43. */
    46. {
    48. //======================== timer =================================
    49. if (TMR0IF)
    50.    TMR0IF=0;
    51. cnt++;
    53.    Flash++;
    54.    if (Flash < 800){Led=Off;}
    55.    if (Flash > 1000 + (3000 * !Output))
    56. {
    57.    Flash=0;
    58.    Led = On;  //Led= on
    59.    }
    61.   else
    62.   {
    63.   Led=On;
    64.   }
    65. //------------------------------------------------------------------
    66. if (cnt >6000){cnt=0;}
    67. }
    68. //================  end of interrupt ========================
    72. void main()
    73. {
    74. //--------------------  settings -----------------------------------
    75.    OPTION_REG = 0b10000000;
    76. ADCON0 =0b10000001; // bit 7 right justfied bit 1 conversion is operating
    77. ANSEL = 0b010001;  // to select analog input GPIo0
    79. VRCON = 0x00;
    80. CMCON = 0x07;
    81. //==========  ADIE=1;  // activate interrupt used when adc working under int
    82. TMR0IE=1;
    83. GIE=1;
    85. TRISIO0 = 1;  // set for input
    86. TRISIO1 = 0;  // set for output
    87. TRISIO2 = 0;
    88. TRISIO4 = 0;
    89. TRISIO5 = 0;
    90. // ============================== start
    91. GPIO1=0;
    92. cnt=1;  // wait
    93.     while(cnt){}
    94.   Output=0; // flash fast
    96. //======================= main loop =====================
    97.   while(1)
    98. {
    100. //======  @ 5Vreference and 10bits  1024 = 5/1024 = 0,00488 V/bit input-->>  (204.8) 204 bits approx per volt
    102. //-------------------- read Batt voltage  -------------------------
    103. ADCON0bits.GO = 1;
    104.    while (ADCON0bits.GO_DONE);
    105.    result = ADRESH;
    106.    result <<=8;  // shift highbyte left
    107.    result |= ADRESL;  // and do low
    109. //-----------------------------------------------------------------
    111.   if(result >=Wanted_value)  //
    112.   {
    113.   Output=1; // flash slow  when off it goes faster see  if (Flash > 1000 + (3000 * !Output))
    114.   // hence 1000 + 3000 * Output
    119.   // your prog here.
    126.   }
    128. }
    129. }
    Moderators note: used code tags for C
  14. papops

    Thread Starter New Member

    Mar 21, 2016
    I appreciate the help and as I said before, this is the way I want to go in the future. But as AnalogKid stated, this is going to take a long time to get comfortable with. I'm basically starting from scratch - so let's begin at the beginning - what would you suggest in the way of hardware to begin with - just something to make some lights blink while getting used the "feel" of this kind of programming. I've downloaded the Picaxe users manual and have started reading - this ain't exactly your grandpa's BASIC (the language I cut my teeth on).

    BTW - do you think it would be more appropriate for me to be doing this on a Picaxe forum?

  15. Picbuster


    Dec 2, 2013
    If you look at the old basic and you did some programming in it, you have a base to start from.
    Most of programming mechanism is similar but the expressions are different.
    in basic for a =0 to 100 : do something :next a
    in C (int a=0; a<101;a++) { do something}
    The dimension in a is not important integer, float or double but in C it is.
    What I try to say is C is not really difficult you can avoid the complex things and start simple.

    picaxe looks great however it will, as far as I could see, not directly interact with the hardware and the programming language looks like a special. This could put you later into a relearning curve.

    Working example given is simple, showing hardware and how the interrupt timer works, producing a flashing light all at low cost pic+lm7805 + 'R's and cap 5 $/euro. The pickit3 (working for most of the pic's models and is about 30$/euro software compiler is free.
    Ending up with an investment risk of 40 $/euro.
    I don't know the price of a picaxe starter kit not able to compare.
    There is a manual call 'C for dummies'
    note: choice C not C++ most of the microprocessor compilers using C apart from some exceptions.
    Look also at
    Load also the Microchip program examples for the selected chip.
    Read all the stuff related to variables, loops, add, subtract, if, for loops and timers. give no attention yet to subroutines, printf, sprint, arrays, pointers or I/O.
    Make a simple program set one port High.
    (don't worry about reloading your pic (spec's >100000 times 100 times a day at least 1000 days to go)

    Keep me informed whatever technical direction you take to crack your (temporally) problem.
    (My goal is to motivate people to enter the beautiful microprocessor world allowing them to convert their brainwaves into hard and software.)