Make: More Electronics (2014)
Chapter 26. Experiment 26: Running Rings
A ring counter is a type of counter that has “decoded outputs.” This means that it activates one pin at a time, beginning with the pin that represents 0 and continuing up to a value that is limited by the number of pins. After that, the cycle automatically repeats.
By comparison, a binary counter has “coded outputs.” In other words, the high and low states on its output pins represent a number in binary code.
The ring counter that I will be using here is the 74HC4017, which can also be described as a decade counter, meaning that it can count up to 10 (actually, from 0 through 9). I plan to apply it in two different reflex-testing games: one that flashes LEDs sequentially, and another that flashes them randomly.
Demonstrating a ring counter is very easy. You just attach ten LEDs to its ten outputs and drive it with a slow-running timer. In my test, I found that the classic bipolar version of a 555 timer created such a noisy signal, the ring counter often interpreted it as two pulses instead of one. Probably I could have resolved the issue by adding a smoothing capacitor, but I felt more confident about using a less-noisy component: the 7555 timer. This has the same pinouts as the old 555 and the same timing characteristics but has a clean output that is properly compatible with logic chips.
Warning: Timer Incompatibilities
Be careful to store your 7555 timers separately from your old-style 555 timers. They look the same and they seem to function identically, but the output from some versions of a 7555 has a more limited current-sourcing capability. You can use it to drive other chips, or a single LED, but don’t try to use it with a relay.
Also, while the 7555 gives you the benefit of a cleaner output, it can be fussier about its input. When driving its input pin through a coupling capacitor, for instance, the value of the capacitor can be important. A spike in the voltage can cause the timer to end its current cycle prematurely. You won’t find this mentioned in the datasheet, but I have seen it happen.
Annoying Pin Sequence
The demo schematic is shown in Figure 26-1. As in the previous experiment, I am using yellow circles to represent LEDs, because there isn’t space for the proper symbols. Add a series resistor if your LEDs do not have resistors built in. You will only need one series resistor, because only one LED will be illuminated at any time.
Note that Pin 8 of the ring counter does not connect through an LED, but is tied directly to negative ground, because it’s a power pin on the chip.
Figure 26-1. A simple demonstration of the 74HC4017 decade counter with a slow sequence of pulses applied to its clock input pin while the enable and reset pins are held low.
If you put this circuit together and apply power, you’ll see the LEDs flashing one at a time as the counter responds to pulses from the timer. Unfortunately, the pin values of the 74HC4017 are scrambled, and their sequence is even more erratic than the pin values of the 74HC4514 decoder chip in Experiment 25. Figure 26-2 shows how the values are assigned to the chip.
Figure 26-2. Pinouts of a 74HC4017 counter. This is a ring counter, meaning that its outputs are decoded—they are powered one at a time instead of in a binary-coded combination. Because it has ten outputs, this is also classified as a decade counter.
You could shift the LEDs farther down the breadboard and unscramble the value sequence with jumper wires, as I suggested with the decoder chip in Experiment 25. However, for the application that I have in mind, I’ll need all available breadboard space for more chips. Really a second breadboard is necessary to mount the LEDs. You can then run wires across to them from the counter, as shown in Figure 26-3. The connections are shown in color so that you can follow them more easily.
Figure 26-3. Unscrambling the output pin sequence of a 74HC4017 ring counter to drive LEDs that will light up in numerical order. The colored connections can be jumper wires or a ribbon cable.
You can use flexible jumper wires, as in the test circuit shown in Figure 26-4. Use as many colors as possible to make it easier to trace connections from one board to the other.
Figure 26-4. Jumpers can be used to unscramble the counter outputs and power a line of LEDs that will light up in numerical order. The components at the top of the breadboard in this photograph will run a demo of the counter, described below.
However, in my experience, the little plugs at the ends of jumper wires don’t always make a firm and reliable connection. A better solution is to use a ribbon cable soldered to headers. This is shown in Figure 26-5.
Figure 26-5. A piece of ribbon cable with header pins soldered to each end. The conductors are swapped around to create connections shown in the previous figure.
Maybe you have never encountered the term “headers” before. Okay, I’ll pause for a moment to explain this, because the usage is confusing.
Quick Facts About Headers
The term “header” is ambiguous. It often refers to a series of “header pins” embedded in a strip of thin plastic, with shorter terminals sticking out at the other side. This is what you see in Figure 26-5. The plastic can be snapped off to give you the number of pins that you need, and they can push securely into a breadboard (provided you are careful to buy header pins with 0.1” spacing). You can solder the wires of a ribbon cable onto the terminals, resequencing them at the same time.
For permanent installation using a perforated board, you must mount “header sockets” on the board. The header pins will then plug into the sockets. Confusingly, both the pins and the sockets can be referred to as “headers” in parts catalogues.
A strip of header pins and two strips of header sockets are shown in Figure 26-6.
Figure 26-6. Header pins and header sockets are sold in long strips that can be snapped off to provide you with the number of pins and sockets that you require. Both the pins and the sockets may be referred to as “headers.” Various configurations are available, such as the angled pins in the strip at top-right.
Quick Facts About Ring Counters
Before I go any further with the test circuit, I’ll just sum up the most important facts about ring counters:
§ The 74HC4017 is a decade counter with ten outputs that are activated in sequence, one at a time.
§ This is also known as a ring counter.
§ Ring counters often have eight, ten, or sixteen outputs.
§ A counter with binary-coded outputs will represent 0 by holding all outputs low. A ring counter includes an output line to represent value 0, and this output will be high when the counter is at 0.
§ A ring counter repeats its output sequence indefinitely, so long as the clock input keeps running.
§ The 74HC4017 is normally used with its reset pin held low and its enable pin held low. The carry pin is an output, which can be left unconnected. The clock pin responds to a low-to-high transition; this is known as the rising edge of the clock signal.
§ Because the carry output changes from low to high when the counter transitions from a 9-value output back to a 0-value output, the carry pin of one counter can be connected with the clock input of a second counter, enabling the display of values up to 99. More counters can be added.
§ When the reset pin is high, it forces the counter back to a 0 output.
§ Some ring counters are not decade counters, and some decade counters are not ring counters. A ring counter always has single decoded outputs that are activated one at a time, but there may be fewer or more than ten of them. A decade counter always counts from 0 through 9, but it may have binary outputs or single decoded outputs. The binary outputs of a decade counter are referred to as “binary-coded decimal,” or “BCD,” in datasheets.
Making a Game of It
In Las Vegas, you can play a game where LEDs flash in sequence around a giant circle, and you have to press a button at exactly the moment when the LED nearest to you lights up. You are allowed a limited number of button presses before you have to pay to try again.
We can make a simple version of this game, but so long as it’s on a breadboard, a circle of LEDs will be difficult to create. I’ll compromise by using a vertical column of LEDs, where the player has to press a button at the exact moment when the LED at the bottom is illuminated. You can rebuild the circuit with a ring-shaped output if you have the time to do so.
To prevent a player from winning simply by holding the button down continuously, I can add another timer in one-shot mode. A timer will normally retrigger itself while its input is held low, but if the pushbutton connects with the timer input through a coupling capacitor, the capacitor will only pass the initial transition from high to low (just like the one-shot timer in the chronophotonic lamp switcher, back in Chapter 7). Also, if the capacitor has the right value, it will ignore any little transient spikes caused by “contact bounce” when the button is pressed. (I went into some detail about “contact bounce” in Make: Electronics).
However, the pushbutton must have two poles (DPST or DPDT type) so that the capacitor is connected to the positive side of the power supply when the pushbutton is not being pressed. Otherwise, the capacitor will have no charge to discharge when the button is pressed.
Figure 26-7 shows the game concept in block-diagram mode.
Figure 26-7. This block diagram shows the simple logic of the ring-counter game.
You can see in the block diagram that when a player is successful, an AND gate detects that the button press has synchronized with the ring counter. The AND gate triggers (guess what) another ring counter, which keeps track of the score using its own set of ten green LEDs.
To end the game, I’ve used a third ring counter, using ten red LEDs. This one advances every time the button is pressed, regardless of whether the player wins. When it has gone from 0 to 9, the 9-value output connects with the clock-enable pins on all three counters, freezing them by changing their enable states from low to high.
The only way to play another game is by pressing a reset button, shown at the bottom of the diagram. This sets all three of the counters back to 0 by applying a high signal to their reset pins. The reset signal overrides the high state on the clock enable pin.
As you can see, you’ll need thirty LEDs to set up this game. Fortunately, LEDs are cheap these days (around two cents apiece from China), and because a ring counter only lights one LED at a time, each column of ten LEDs can share a single series resistor. You don’t need to spend the extra money for LEDs with resistors built in.
The schematic for the game is shown in Figure 26-8. Because there was insufficient space, in this schematic I omitted the LEDs completely. The yellow, red, and green numbers in each of the counters tell you that these pins should be connected to LEDs of these colors, in numerical sequence. The colors match those that I used in Figure 26-7.
Also to save space in the schematic, I offset the chips diagonally. On a breadboard, naturally you have to line them up, one above the other. I think you’ll find that they fit.
If you build the game using the previous test circuit as a starting point, remember to disconnect pins 13 and 15 of the first ring counter from negative ground. I tied them to ground originally to disable the reset function and to keep the chip in its enabled mode. In the full circuit, those functions are now controlled to start a new game and to freeze the game after ten turns, respectively.
Photographs of the dual-breadboard circuit are in Figure 26-9 and Figure 26-10.
Figure 26-8. Schematic for the ring-counter game. Numerals inside the three counter chips indicate that those pins should be connected with LEDs of the same color, arrayed in numerical order.
Figure 26-9. The lefthand section of the ring-counter game.
Figure 26-10. The righthand section of the game, with jumper wires that run across to the other section.
You can use the trimmer potentiometer to control the game speed. After you play the game for a while, you may wish to replace the trimmer with two or three fixed resistors with DIP switches, for preset levels of difficulty.
I tested the game with three different pushbutton switches, and found that it worked well with a 0.033µF capacitor on the input to the one-shot timer. Remember, I mentioned that the 7555 can be a little fussy about this. If you find that the timer is sometimes unresponsible, or abbreviates its output pulse, try a higher or lower value capacitor, or try a different pushbutton switch.
The duration of the pulse from the one-shot timer is critical. If the pulse is too long, the player will be able to win by pressing the button a moment before the bottom LED lights up, because the duration of the pulse will overlap with the “on” state of the LED. The 100K resistor and 1µF capacitor will create a pulse lasting about 1/10 of a second. To make the game more difficult, substitute a 47K or 22K resistor, which will create a pulse lasting about 1/20 or 1/40 of a second, respectively.
Make Even More
I’m thinking that the game would be more interesting if the string of LEDs varies its flashing speed unpredictably. That sounds difficult to do, but really, it isn’t. In Experiment 22, I already showed how you can connect the outputs from a pair of timers through an XOR gate so that their signals move in and out of phase, creating unpredictable audio effects.
All you have to do is slow the timers in that experiment by a factor of about 1,000 and take the output from the XOR gate as the clock input for the reflex-testing ring counter, instead of using the astable timer that I included originally.
There won’t be room to do this all on one breadboard, but you can put your pseudorandom cycle generator on a separate board and run a signal wire between them (with additional power and ground connections). Adjust the two trimmers until the lights flash in a stop-and-start way that pleases you. Are they really random? No, but they can seem to be random, which is all we need. Sooner or later they will tend to repeat the same sequence, but if you get the timers running just slightly out of phase, this can take a long time to happen.
The breadboarded version of this circuit is shown in Figure 26-11. The circuit is derived from Figure 22-1, except that the XORed timers are now connected with a decade counter.
Figure 26-11. The idea first mentioned in Experiment 22, of XORing the outputs of two timers that are slightly out of phase, is applied here to create a seemingly random fluctuation of the counter controlling ten LEDs.
And a Microcontroller?
The main challenge for replicating this game with a microcontroller is that you need thirty outputs to drive all those LEDs. Or do you? Really, of course, you only need the ten yellow LEDs to make the game playable; the score and the number of tries can be displayed on an LCD screen. And if even the ten LEDs exceed your available number of output pins, you can flash the LEDs by driving them through a binary-to-decimal decoder chip, which only requires four binary inputs. Of course, that means you have to send numbers to the decoder in binary code.
Checking the user-input button with a microcontroller can be done with an interrupt. But you may want a routine that tells the microcontroller to ignore the button if it is being pressed before the 0-value LED lights up. A premature button press would tend to indicate cheating.
Most microcontrollers that run a high-level language such as C or a version of BASIC will have a pseudorandom number generator built in. That eliminates any need for XOR’d timers. And you can adjust the speed of the game by attaching a trimmer potentiometer to an input pin that has an analog-digital converter.
So, yes, it can be done, and you’ll certainly reduce the chip count. Oddly enough, though, I think that making the game work would be difficult with a microcontroller, because writing and debugging the program would be nontrivial. The reason is that you’ll be compressing all the functions of various chips into one chunk of code. It has to flash the LEDs in a pseudorandom way, and while each LED is lit, it has to go check the user-input button and the reset button—and it has to stop the game if the number of tries reaches the maximum—and it also has to update the LCD display and the game-speed trimmer input, which must be decoded to change the value of a variable that is used in conjunction with an internal clock. Some of these tasks can be handled by interrupts, but then you have to write code to handle the interrupts when they occur.
Sometimes, it’s easier to wire chips together. And in any case, I like having all of those thirty colorful LEDs as the game display.