The CD4017 is another useful IC for the maker, both for discrete projects and some microcontroller applications as well.
In the last few Classroom instalments, we’ve been looking at various Integrated Circuits (ICs) that may be of interest to makers, in a non-engineering level, ‘what you need to know to use it’ way. Most of these have focused on linear ICs, which are often overlooked in our digitally-focused environment. This month, however, we’re going to look at a digital IC from the days before microcontrollers were so commonplace. The CD4017 Decade Counter is a device which can count to ten based on a single input, and this has many more uses that it may, at first, appear to have. While some manufacturers have discontinued it, others are still making it and in any case, there is still plenty of supply around.
NAMES AND DATASHEETS
Before we look at the IC itself, we need to cover what it is called. At heart, the device is called the 4017, and the series it belongs to is usually called the ‘4000’ series or ‘CMOS 4000’ series. You may also encounter the series referred to as just the ‘CMOS series’. However, an internet search for the term ‘4017’ will bring up all sorts of results, many of them for the postcode in Queensland. Different manufacturers label the device different ways. For example, Texas Instruments adds the prefix ‘CD’ to the whole series, as does National Semiconductor, Renesas and Fairchild Semiconductor. ON Semiconductor have a surface mount version labelled ‘MC14017’, but no through-hole offering. All use various suffixes. ST Microelectronics have a ‘HCF4017B’, and NXP Philips have a ‘HEF4017B’. All of these devices are considered the same, however, sometimes the different suffixes and prefixes can indicate subtle differences, like supply voltage differences or current limits. Just to add more confusion, Nexperia have a 74HC4017, which is almost the same device but with 5V logic operation rather than the 15V to 18V limits of the CMOS versions, and an extra input and output.
Confusing? It was to us, too. Making it even harder is that some of these companies have been acquired by each other, and the brands may or may not still exist. Some of the companies no longer make the device but the datasheets from every company listed above are still around on the internet. They are mostly comparable and the differences are not likely to trouble the maker, except in the case of the lower voltage Nexperia device. What will make a difference is the user-friendliness of the datasheets. We found the Texas Instruments version to be a scanned copy of the old Harris Semiconductor one, while the Fairchild data sheet was fully digitally formatted and very clear to read. If you are looking for a datasheet, first try to find out who makes the device you have bought, and find that company’s publication. However, the differences are generally academic and you should be able to use any datasheet. We used Fairchild’s document for this article but had to dive into the Texas Instruments one a few times.
GETTING TO KNOW THE CD4017
The CD4017 is a CMOS IC, which comes with certain rules and expectations. TTL ICs generally work on the 0-5V range, but CMOS logic, while still digital, generally works from 0-15V or 0-18V. They are often more rugged than the TTL equivalents and can generally, but not always, handle more current at inputs and outputs where relevant. Electrostatic Discharge (ESD) survivability is a matter of debate among even experts, so it is safer to assume that they will still be static sensitive. The datasheets are not always helpful in clearing this up.
On the retail market, makers will most likely encounter the 16-pin DIP (Dual-Inline-Plastic) variety. A variety of SMD packages exist but are not readily available over the counter.
The first thing to note when looking at the pinout diagram is that the outputs are not reflected in the pin numbers. While they are sequential in terms of the chip’s internal logic, the way they connect to the pins is not sequential. Be careful when reading diagrams, and make sure that you know whether a circuit is referring to pin numbers or output numbers. Some diagrams clarify this by using ‘OUTn’ where ‘n’ is the output number. We have also seen ‘Qn’ used the same way, which is a bit clearer that ‘O1, O2’, and so on, but can be problematic as the terms ‘Q’ and ‘Q’ relate to flip-flop outputs and will be found in the logic diagrams for the IC. For the rest of this article, we will label our diagrams with ‘OUTn’ if we are referring to an output, and just a number if we are referring to a pin. Note that the first output is Output 0, not 1, and the last is Output 9, not 10.
That said, note that pins 1 to 7 of the IC are outputs, while pin 8 is the GND pin. This will often be labelled on datasheets as Vss. Pins 9 to 11 are the remaining outputs, followed by the ‘CARRY-OUT’. This output goes high when the last output goes low, and stays high for the next five counts, before going low again. More on this soon. Pin 13 is the Clock Enable pin, and it must be low for the counter to advance. Taking this pin high will cause the device to ignore the clock, and so can be used to pause the count. As soon as this pin is low again, counting will resume from where it left off. Pin 14 is the clock input, and pin 15 is the reset pin. A high signal on this pin will reset the count back to zero and counting will resume from the beginning once the reset pin is low again. Finally, pin 16 is the Vcc supply voltage, usually labelled Vdd on datasheets.
The CD4017 is based on a logic arrangement called a ‘Johnson counter’, and this one has five stages with two outputs each, hence the ten outputs and the name ‘decade counter’. If you want to know what’s going on inside, there are pretty good logic diagrams provided in the datasheets, but we promised not to get into that. What you need to know is that every time there is an appropriate signal at the input to the system, the counter advances one output. Only one output is on at any given time, one after another. They are not cumulative, which means they do not turn on and stay on until the count is complete, like a bar graph display would.
DRIVING, PAUSING AND RESETTING A CD4017
The input needs to be a pulse. The count is advanced when the clock signal rises from low to high and there is no other timing involved. This means the clock signal can be as long or short as you like, but cannot be a single state change. Counting only advances on the rising (low to high) edge of the signal, never the trailing (high to low) edge. In other words, a high clock signal has to go low and then high again to advance the counter. You could not, for example, connect a toggle switch and expect the counter to advance when you turned the switch on then again when it was switched off. You would need to toggle on, then off, before turning the switch on again to advance the count.
This is quite useful, as it allows the CD4017 to count pulses that may be variable in duration. Any device which has a separate timer and checks the input every so often will register multiple counts if the input is left high across several time periods. With the CD4017, pulses can be as short as 100 nanoseconds for 5V operation, dropping to 30ns on 15V operation. That’s quicker than most signals the maker is working with. The key is that there is no maximum, so if you were to, say, the number of times a limit switch on a basketball arcade game is activated, then you only get one count whether the ball goes straight through or if it gets stuck there.
So far, we have discussed switches providing the input. This is not always the case. In fact, it is probably not often the case. Many circuits found online for the CD4017 feature a pulse generator which advances the output. We have used this feature in two Kids’ Basics projects. In Star Struck, Issue 27, we made a starry sky by using the first five outputs of the CD4017 to drive LEDs which would provide a ‘twinkle’ effect to fibre optic stars on a painting. In this case, we took the sixth output, OUT5, to the reset pin. This way, as soon as the sixth output went high, the device reset instead of counting on the next few inputs that had no LEDs connected to them.
In this project, we wanted the circuit to just work on its own and light LEDs in sequence. This was done by using a 555 timer in astable mode, to produce a continuous stream of clock pulses fed straight to pin 14 of the CD4017. The frequency of this oscillator determines how fast the outputs count. This system is often used for visual effects and many educational circuits using the CD4017 feature either chaser lights or a scanner, of Knight Rider style, driven by a continuous clock pulse. While a 555 is common, any oscillator circuit can be used. Note that in these circuits, if the count proceeds all the way to ten, then reset is automatic and the reset pin can remain tied to ground. The reset pin is only used to manually reset or to shorten the count range as in Star Struck.
In Kids’ Basics Issue 31, we made a decision maker based on the CD4017. We did it with a 555 oscillating faster than the human eye can perceive, and a pushbutton was used to feed the clock pulse to pin 14 only when the decision was being made. In retrospect, this would have been a great opportunity to use the clock enable pin. We could have used a normally closed pushbutton to connect the Clock Enable pin 13 to the supply voltage, taking it low whenever the button was pressed and therefore enabling the count to proceed. Instead, we had a normally open pushbutton between the oscillator’s output and the counter’s clock input, only feeding the clock to the counter when the pushbutton was pressed closed.
Whether we went this way for a reason or simply didn’t think of using the Clock Enable is lost to the sieve of poor memory, but it does highlight the function of this pin. It is far more relevant if you want to interrupt a count with an electronic signal. You may have the counter working away in the background and want a circuit to pause it for a while. Without a person to press a switch, the Clock Enable pin represents an easy way to pause or enable the CD4017 electronically.
One challenge in using the CD4017 in display situations is that the output is limited to 10mA. This is ok for driving low-brightness, high-efficiency LEDs but it won’t drive much else. While that isn’t a challenge on its own, building a neat and compact circuit with the necessary transistors and resistors required to boost the output current is a challenge, especially when breadboards are involved.
We even considered a cousin of the CD4017, the CD4026 which is a decade counter with an output designed for direct connection to seven-segment displays. We thought this might simplify the circuit for a project we have planned but no, the output of that device is 10mA too, and still needs transistors to drive most available seven-segment displays.
On the subject of outputs, it is worth noting that there are high-efficiency devices available. We have sourced some from Element 14 which run on 2mA, although they have a low brightness. Others are around which have a reasonable brightness as far as panel indicators are concerned (close range, in other words), and still draw less than 10mA. Others will be acceptably bright if you limit them to 10mA when they may have a typical current of 20mA. If you are using LEDs without transistors, then because only one output is on at any given time, all LEDs can use one common resistor in series, between the LED and ground. This will simplify board layout in many cases.
It is possible to connect more than one CD4017 in two main formats: Cascade, and Ripple. Cascade counting is where one device flows to the next. Some outputs are needed to control circuit behaviour but most options give eight or nine outputs per device. So, if you wanted to make, say, a roulette wheel with thirty-seven LEDs, you would need five CD4017s (9+8+8+8+8, the last one truncated to 4). The following circuit shows how AND gates are used to achieve this.
The last output from each stage feeds one input of the AND gate, as well as the Clock Inhibit input for its own CD4017. That means that as the clock inhibit signal is high, the clock pulses no longer affect that stage, so it stays at the last count. The clock pulses are then fed to the second stage via the now-activated AND gate. When that stage has counted to its end, the last output repeats this process. Note that the OUT0 outputs are not used in subsequent stages as these are high by default until the clock advances the count. Most displays which cascade the counters do not want extra lit LEDs in the display. There are other ways of cascading CD4017s, but this one is the clearest and comes from the Texas Instruments datasheet.
While cascading can increase the count of the CD4017, ripple counting is a different story. In general terms, ripple counting is where the output of one device triggers or controls the next. This is often used in strings of flip-flops, but when applied to the CD4017, something very interesting happens. Using the Carry Out function of pin 12 to feed the Clock input of the next counter means that the next stage gets one full clock cycle (remember the Carry out is high for five counts and low for five) for every ten clocks the previous one receives.
This means that the CD4017 (and other decade counters) can be set up to count inputs in the same place value that we use in normal mathematical counting. In other words, the first one counts the ‘ones’ or ‘units’ column, then every time that counter is full, it increments the ‘tens’ column before starting again. It’s just the same as moving those little wooden blocks (MAB blocks) or tally marks around on your place value chart at school. They can be daisy-chained to count to any factor of ten. One counts to 10, two count to 100, three count to 1000, and so on. The only catch is that the outputs start with the first output high, remember there are only nine spots in each place value column anyway. In school, we put the value ‘0’ in as it has place value when there is nothing in that column. Building a counter for the same job would leave you with the choice of displaying an LED for ‘0’ values, which would be valid, or leaving it unconnected.
Having said that, a circuit doing this job is often not giving a visual display (you would likely use the seven-segment driving cousin, the CD4026, for that), but rather performing a division task. For example, if a signal within the CD4017’s minimum pulse time of 100ns was too fast to use in a circuit you wanted, a divider could be used to reduce it by factors of ten, one hundred, one thousand, or any number of dividers you connect in ripple mode. This way, only one pulse in every ten, one hundred, or so forth, would be fed to the rest of the circuit. The principle is often called a frequency divider.
Enough of the theory. Let’s build!
|Parts Required:||Jaycar||Altronics||Core Electronics|
|1 x Solderless Breadboard||PB8820||P1002||CE05102|
|1 x Breadboard Wire Links||PB8850||P1014A||CE05631|
|46 x Plug-to-Plug Jumper Wires %||WC6024||P1022||PRT-12795|
|2 x 1kΩ Resistor*#||RR0572||R7558||COM-05092|
|6 x 10kΩ Resistor*||RR0596||R7582||COM-05092|
|2 x 100nF MKT Capacitors||RM7125||R3025B||FIT0118|
|2 x CD4017 IC||ZC4017||Z4017||CE07310|
|2 x 1N4148 or 1N914 Small Signal Diodes *||ZR1100||Z0101||COM-08588|
|20 x LEDs||ZD0152||Z0800||COM-09856*|
|1 x Small Pushbutton||SP0710||S1060B||CE05249|
|2 x Momentary Switches of Choice||-||-||-|
* Quantity required, may only be sold in packs. % 40 for the LEDs, 3 for the reset switch, two to join the power rails on the two boards, and one to join pin 13 of each 4017. Does not include wires used for trigger switches. # We used 1kΩ for our LEDs on 12V. Yours may be different.
To demonstrate the CD4017, we’re going to build the basis for a little game. We are going to use two independent CD4017s with a switch each as the clock input, then use the Clock Inhibit pin to lock the game when a win is achieved. We can’t call it ‘first to ten’ because the counter starts on ‘0’, meaning only nine inputs are required to reach the end. ‘Ladder Climb’ represents the fact that the display would likely be ten LEDs in a vertical column for each CD4017, and the game ends when the ninth input is achieved.
We say ‘basis’ because we aren’t going to necessarily finish it. The switches we use to give the clock inputs to our CD4017s could be the levers in an arcade basketball game, or behind targets shot at with Nerf guns. They could be big pushbuttons that a ball is thrown at. Really, it will be up to you. Additionally, we’ll build on two solderless breadboards but you would likely want to make this permanent. To that end, we suggest the solderable prototyping boards that are a hole-for-hole copy of the traditional solderless breadboard.
We decided to mount the project in a thick cardboard gift box from a dollar shop (we keep a lot of these on hand for Kids’ Basics) but you might use a plastic enclosure. The method of attaching switches is open to choice as well. You may pass wires through the side of the box and attach to the board, but we chose to use spring-type speaker terminals mounted to the side of the box. This is so we can experiment with switch options and adapt the unit to different situations. If you already know what you’re going to do with yours, don’t worry about these terminals.
Before you start on the breadboard, you will need to attach wires to your LEDs. Sadly, this is necessary because the outputs are not in sequential arrangement, and because in most cases, you’ll want the display visible in a front panel. You may use plug-to-socket jumper wires (take a look at Kids’ Basics Issue 31 for how to do that), or you may cut one end off each wire and solder it. You may also use your own wire and solder on pins at the breadboard end. If you are using the solderable breadboard you won’t even need the pins. Make sure the legs of the LEDs are well-insulated, either with tape or heatshrink.
A NOTE ON LEDS: There used to be a standard for LEDs where the visible internal structure told you which side was the anode (+) and which was the cathode (-). Sadly, this standard is no longer upheld and plenty of LEDs are too opaque to see said structure. It is also possible to use the rim of the LED as the marker. It has a flat spot on the cathode (-) side. However, not all LEDs had a rim in the case. The most common identifier is that the cathode leg is shorter and the anode leg is longer. However, some LEDs have legs the same length! This is rare, however. Whatever the situation, examine your LEDs carefully to determine polarity, then either mark your wiring as you attach it or use colour-coded wiring. This will be important later. An LED tester, such as the AA0274 from Jaycar or Q2010 from Altronics are also very handy for testing LEDs.
Install a CD4017 on each breadboard. Then install the wire links according to the photos. By using one set of power rails for LED connections and not for power as such, we were able to keep wiring to a minimum. Add the resistor common to all LEDs, plus the one used to stop the clock input floating when the switch is not pressed. Add a capacitor where the switch will go later. So far you’ve done the same thing on two identical boards. Note that one wire link in this bunch on each board connects the last output to the Clock Inhibit input but we will make another connection here soon.
Add the LEDs by inserting all the cathodes into the rail at the side of the board. Make sure you use the same row that the common resistor is connected to. Attach the anodes one at a time into the relevant outputs of the 4017s, using masking tape as you go to label each LED with its output number for later connection to the front panel.
Now you need to connect two momentary switches, one for each board. These are the inputs. We also need a momentary pushbutton common to both as a reset. Decide how these will be mounted. The Reset button can probably just be wired, as we have, to three jumper wires with one end cut off, bared, and twisted through the terminals. The side with one wire goes to the power rail, while one wire from the twin connection goes to each reset pin. If your enclosure is bigger than jumper wires can cope with, you might have to solder pins to another source of wire as described for the LEDs.
Connecting the input switches depends on where you are mounting them and what type of switch they are. You could be using Normally Closed reed switches with a swinging target holding the magnet next to them (and therefore the switch open) until the target is hit by something. The terminology for Reed switches is reversed because ‘normal’ is defined as with the magnet present. You may be using limit switches in a basketball arcade game, giant pushbuttons to throw balls at, or light gates to pass an object through. Whatever the case, bring the wires from these through the sides of the display enclosure, solder pins to the wires and insert them in the boards where the pushbuttons are shown on the schematic and Fritzing. Or, you could use speaker terminals as we did for options later on.
Place the boards in the enclosure and put the lid on. Make a hole in the front panel for the reset switch, and twenty holes for the LEDs. using the labels you added earlier, insert the LEDs through the panel in order, from 0-9, for each side. Now all that is required is to connect power. We designed this to run from 6V via four AA batteries, however with a resistor change, you could run it from a 5V USB battery bank, a 12V battery and solar panel, or anything else 15V or under. Once powered, you can test your creation.
GAME PLAY AND WHERE TO FROM HERE
The object of the game is for you and a competitor to have a target each. Every time a target is hit, the counter for that side increments by the illuminated LED climbing the ladder. The first to light the highest LED wins, at which point no further scoring is possible for either player. The Reset pushbutton must now be pressed.
As far as expansion, we have discussed quite a few switch options above. You could also think about cascading the CD4017s, or even using the carry-out circuit to make a minute-to-win-it style game, where the object is to score as much as possible in a given time. As noted, this is hard without seven-segment displays, but it is still possible using rows of LEDs and read the display like a place value chart.
While the CD4017 is getting old and only does one job, it does it very well and without the issues of code and reliability that can affect microcontroller circuits. A circuit based on discrete components does not ‘hang’ or ‘freeze’ unless it is far more complex than anything you will ever see in DIYODE. If you have the job for it, then the CD4017 is still an IC of choice.