How does logic work? The magic of the CD4017 decimal counter

All information about this project is available in a special website section.
We thank you for your attention!
No items found.
Click image to zoom

Hi there! In the previous post, we talked about binary counters. They count like this: 0000b, 0001b, 0010b, 0011b, 0100b, 0110b, 0111b, 1000b, and so on. But what if we have an LED for each different number or digit, for example, 0 to 9? Or what if we have some devices that should be turned on in a specific sequence?

This is where a decimal counter, or decoder counter, comes in handy, an excellent example of which is the CD4017. It is especially loved by enthusiasts of lighting effects and other gimmicky gadgets because this chip allows one to achieve excellent results using simple means.

We could use a 4-bit binary counter, a 4:16 decoder, and a logic board that would reset the counter after it has counted to 10, which is 1010b. But first, let's estimate both the complexity and the cost.

The post on counters describes the layout of this 4-bit binary. It consists of four flip-flops, according to the number of digits.

Next, we need the counter to be reset after counting to b1010. A double-input AND element, receiving signals from the first and third digits, will be enough.

We count the digits starting from zero, as a digit number represents the power to the base: 2 to the zero power is equal to 1, 2 to the first power is equal to 2, to the second power is 4, and to the third power is 8.

As you might remember, we've used the two AND logic blocks of the CD4081 chip in the electronic clock circuitry. So that when the seconds counter reached 60, the number b0110 appeared in the MSB, the seconds were reset, and one was added to the minutes count. The same deals with minute counters and hour increments.

And when the most significant bit of the clock is 2 (b0010), and the least important bit is 4 (b0100), the hour counter is reset. Here, too, one 2AND gate is enough.

You may be asking why the CD4518 chip does not need to be reset when it has been counted to 10. — Because it is a binary decimal counter that counts not from 0 to 15 (b1111) but from 0 to 9 (b1001). Upon reaching the number 10 (b1010), a reset circuit is already built into it.

So, we figured out the counters and their reset. Now we need a decoder. For simplicity's sake, let's take a three-bit decoder with three inputs and eight outputs, 3:8, SN74HC138N.

As one can see, three inputs have used three buffers and three inverters, one for each input. And one three-input AND element for every one of the eight outputs. We do not consider the output buffers and the output shutdown circuit.

But there's another, more budget-friendly way. Those were binary counters, and now we have a diagram of the internal structure of the CD4017B decimal counter.

There are not just 4 but 5 synchronous D-flip-flops in this circuit. Each of the ten outputs requires only one double-input AND element with inverting inputs! And additionally, we'll need just one triple-input and one dual-input NOR element.

How did such savings come about‽ Let's look at counting steps.

A high voltage level must be applied to both inputs of the 2AND element for a logical one to appear at the output. Since the inputs of our logic elements are inverting, they require not two ones but two zeros.

The initial state of ABCDE flip-flops is b00000. For ease of understanding, we will write the digits not from right to left but normally from left to right. We have zeros at the non-inverting outputs of flip-flops A and B. That is the logical one at the output of AND block number zero. It is the only one connected to the two non-inverting outputs of the flip-flops. The rest are hooked up to at least one inverting output.

The first clock pulse will transmit a one from ¬E ("not E" or !E, if you fancy) to A. B will receive a zero from A, D from C, and E from D will also get zeros.

For trigger C to receive a one, both inputs of 2NOR must receive zeros. B = 1, and zero must be at the output of the 3NOR element. The last condition will be met if at least one input of 3NOR receives a logical one.

When B = 1, then ¬B = 0, so it is unclear why 3NOR was used and not 2NOR. This may be due to the silicon topology.

So, we get the following formulas for the transition of trigger states: B = A, C = B & (A | C), D = C, A = ¬E, and E = D.

The initial state is ABCDE = 00000. The output "0" formula is Q0 = ¬E & ¬A. When A = 0, E = 0, Q0 equals 1. All other outputs will be 0.

After the first clock pulse, the trigger state is ABCDE = b10000. Q1 = ¬E & A = 1.
2nd pulse ABCDE = b11000. Q2 = B & ¬C = 1.
3rd pulse ABCDE = b11100. Q3 = C & ¬D = 1.
4th pulse ABCDE = b11110. Q4 = D & ¬E = 1.
5th pulse ABCDE = b11111. Q5 = A & E = 1.
6th pulse ABCDE = b01111. Q6 = ¬A & B = 1.
7th pulse ABCDE = b00111. Q7 = ¬B & C = 1.
8th pulse ABCDE = b00011. Q8 = ¬C & D = 1.
9th pulse ABCDE = b00001. Q9 = ¬D & E = 1.
On the 10th pulse, ABCDE = b00000, and the cycle begins again.

As you can see, our counter is a shift register, which is first filled with ones and then with zeros. The lighting effects machine from the post about the shift register does exactly the same thing, only with four rather than five binary digits.

This BCD code was developed in 1953 by the renowned American computer inventor Robert Royce Johnson.

Each subsequent Johnson code combination differs from the previous one by just one digit. This is great for computer technology since the simultaneous switching of many logic elements causes a surge in the current consumed by the microcircuit, meaning power supply interference that can potentially lead to failures.

Now, let's see what fascinating things can be assembled on the CD4017.

In this circuit, the counter is clocked by an oscillator on the NE555 integrated timer. Using the tuning resistor VR1, one can regulate the oscillation frequency and, accordingly, the speed of the light moving along the line. This way, one can imitate, for example, raindrops.

The following diagram is almost the same, but the timing capacitor of the oscillator C1 is charged not through a resistor but through the DC generator on transistor Q1.

The charge current of capacitor C1 and, accordingly, the oscillator frequency are determined by the voltage at the base of the transistor. And it reaches the supply voltage of +5V when the SW1 button is pressed. After the button is released, the voltage decreases as capacitor C2 is discharged by the base current and through resistor R1 back down to zero.

So, when you press the button, the counter starts running at a fairly high frequency, then it starts to decrease when the button is released. The light runs in circles more and more slowly and finally stops in one of ten places. The result is a wheel of fortune. It is almost impossible to guess where the light will stop.

And here, we've got precisely the same circuit equipped with a diode decoder, resulting in an electronic dice.

When the level is high at Q0, LEDs D1, D10, D11, D12, and D13 light up, corresponding to the number 5.

When the level is high at Q1, D10 and D11 light up, and the number is 2.
When the level is high at Q2, D1, D10, and D11 light up, and the number is 3.
When the level is high at Q3, D10, D11, D12, and D13 light up, and the number is 4.
When the level is high at Q4, D10, D11, D12, D13, D14, and D15 light up, and the number is 6.
When the level is high at Q5, only D1 lights up; the number is 1.

Suppose instead of a generator on the NE555, you clock the counter from a microphone with a basic transistor amplifier. In that case, you will get a party light that reacts to sounds and vibrations.

Finally, suppose you put together a matrix of 9 by 9 LEDs, where the vertical columns are lit by one CD4017 counter and the horizontal rows by the second one. In that case, you will get truly magical lighting with endless varieties of patterns.

By adjusting the frequencies of two clock generators or switching the column counter using switch S4 to one-tenth of the row frequency, you can make the picture move quickly, slowly, or even freeze in different directions. That's the fidget toy we were dreaming of!

Now you see how many amazing effects can be built on the good ol' CD4017 and NE555!

Products in this post

No items found.

Sign up for my newsletter

Kevin Gibbs

Hi! I'm Kevin! I am a very curious engineer :))
I'm the website founder and author of many posts.

I invite you to follow exciting experiments, research, and challenges.
Let's go on to new knowledge and adventures!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.