This is a crazy project, but fun and educational. Perhaps it's a good STEM project. Relays are big and noisy. Building a relay based binary counter is a fun way to show kids (and perhaps some adults) how digital electronics work!
ContextWhy are we interested in flip flops or a binary counter? These are the basic building blocks of digital electronics. Every microprocessor or microcontroller is made up of registers where numbers are stored and mathematically processed. The flip flop is the basic element in a register - it stores 1 bit, and our binary counter is basically a computer register. In operating our counter, we are incrementing the value stored in that register with each count.
In a microprocessor the flipflops are made from microscopic transistors, but they do the same things as our relay flip flops. A typical microprocessor has dozens of registers and thousands of flip flops. This project may provide some insights into the most basic of digital electronic operations.
For me, this project is nostalgic, because I first build a binary counter from relays in 1960, before microcontrollers or personal computers even existed! I was a 16 year old student in high school. My original project was a 10 stage counter made from 20 surplus relays that someone gave me. It was fun and interesting to the other kids because it made a terrible racket when it was running. I ended up donating it to my high school physics class.
This project also has a historical component. Computers were never built from relays - they are too slow. Max clock rate is about 20 Hz. But when I build my first binary counter from relays, computer flip flops were still made from two vacuum tubes! Aside from the slow speed, relay flip flops looked pretty good!
We are going to jazz up the project a bit in 2021 from what I did in 1960, using an Arduino to control our counter and display the count, but the idea is still the same - use two DPDT (double pole, double throw) relays to build a flip flop, and then use five of our relay flip flops to build a binary counter.
The Relay Flip FlopSo where to start. First, let's look at how we build a flip flop with relays. The simplest way is a set / reset flip flop or digital latch.
In the image above, relay B is on, A is off. Mechanically close relay A and B turns off, latching A closed, Mechanically close B and A turns off, latching B on.
But we need something a little more complex for our binary counter. We need a toggle flip flop. A toggle flip flop has a single input. Give it one pulse and it latches. Give it another pulse and it unlatches. It essentially divides by two.
As we said already, a toggle flop flop in relays is a little more complex than the latch. It requires two DPDT relays and is wired up as shown below.
But how does it work? It is much easier to see how it works by drawing it a little differently.
The drawing above shows how the relays are configured when first powered up. Neither relay in on. Push the button and a path closes to energize relay B. Energizing relay B puts our two relay coils in series, but our push button is also shunting current around (or shorting out) relay coil A, so for now, relay B is on and relay A is off. But releasing our button removes the shunt around A and both relays are now on. But notice that the situation with our push button is now reversed. Pushing it again shunts and opens relay B, breaking the connection between the two relay coils. Now releasing our push button removes power to coil A and now both relays are off, which is the situation we started with.
So push and release the button to latch both relays. Push and release again and both relays unlatch. Our toggle flip flop divides by two. That's exactly what we need to build a binary counter.
Above is the 12 volt relay we are using. I chose this relay because it is a relatively inexpensive surplus relay - $2.40 on Amazon, $1.50 on eBay if you can wait a month to get it from China.
Wiring two of these relays in our toggle flip flop configuration looks like this:
In the photo below, you can see how the two relays are wired up to produce the configuration above. The red wire is +24 volts. The black is ground. The yellow and green wires are our input. We need 24 volts because some of the time our relay coils are in series and they need to close. That doesn't work with our 12 volt relays operating at 12 volts. ( I tried 15 volts and that didn't work either.)
Now let's see our flip flop in action.
In the video above, we are using a Arduino Uno and a relay module as the input to our flip flop. Thanks to the LEDs built into our relays, you can actually see the flip flop go through the four steps we described above as it latches and de-latches.
The Arduino isn't doing much yet, and to be honest, this project doesn't really focus on the Arduino. But later, when we have our counter built, we will use it to display the output of our counter in decimal.
A note regarding power: Our relay coils when properly powered at 12 volts draw 75ma or 0.9 watts. When powered at 24 volts they draw twice as much current and dissipate 3.6 watts. They work fine like that, but get warm over time and use a fair amount of energy. I suggest using them like this for short demonstrations of a few minutes, not for continuous use.
The Binary CounterSo now we can put four more relay flip flops together and build a bank of flip flops wired up as a counter, with the output of each stage becoming the input for the next. Below is our bank of flip flops wired together. The first stage is currently on the right, but when we flip it over, our counter will go from left to right. All the wiring has been done with 22 gauge bare tinned copper wire.
So now let's hook up our bank of flip flops to the Arduino and our relay module. Together they form a 6 stage counter, with the relay module as the first stage and our flip flops as the next 5. Our counter can count from 0 to 63 before starting over.
But there's a problem! Where is our nice counter display that goes from 0 to 63? It's actually there, but not in the form we wanted. The red LED on the relay module and the lower green lights on our relay flip flops actually display the counter we are looking for, but it is inverted, meaning an on LED is a 0 and an off LED is a 1. The lower green light tells us the lower relay is on. The reason our count is inverted is because the very first input pulse to our counter turns on all the lower relays in our counter - they are all on; the lower green lights are all on; And at that point our counter is at 0!
I didn't have this problem with my original relay based binary counter back in 1960. I actually used 3PDT relays with an extra pole or switch to use to display results. Since we didn't have LEDs back then, I actually used small neon bulbs hooked to the 3rd pole switches to display the count.
Displaying Our Binary CountDouble pole relays are all we need to build our flip flops, but all the poles on both relays are in use. So to display the output of our counter, we need to look around from the right signal. As we already said, the status of the lower relay is a good signal, but it is inverted. So to properly display the output of our counter, we need to build a little board with some inverters. To invert the signal and drive an LED, we need a transistor and two resistors.
Above is what we need on each stage of our counter to invert the status of the lower relay and display the count. An added bonus of this is that our LED actually supplies a digital output that we can send back to Arduino to display the output of our counter in decimal. The LED is at 0.7 v. or LOW when off (the forward drop of the transistor's base-emitter junction) and at about 3.2 v. or HIGH when it is on (we use Blue LEDs for their high forward drop). More on sending our count to the Arduino in a minute.
So above we have a board wired with 6 of our inverters and blue LEDs. One minor point to notice is that base resistor on the very first (leftmost) stage is 15K ohms instead of 100K. That's because its a 5 volts signal coming directly from the Arduino, unlike the others coming from the high side of a 12/24 v relay.
And below, we have it in action. I've rotated the board 180 degrees in this video, so that now our binary counter reads from right to left (least significant bit on the right), the same way we usually display numbers.
Above is a video of the counter working with our inverters and blue LEDs in place to display our binary count. We are counting here at 250 msec. per count, so we count from 0 to 63 and start over again every 15 sec.
Converting the Output Back to DecimalFirst, before we continue, let's make one addition to our counter. I occasionally saw my counter skip a beat. It seemed to be the first flip flop failing to latch. Thinking about the cause, I concluded that the relay module contacts may be bouncing. All mechanical switches bounce a little on contact, adding in digital "noise". So that could be the cause. I added a 0.01 mfd capacitor directly across the output of the relay module to suppress any noise. I was pleasantly surprised when it totally fixed the problem!
So now, let's go one step farther than I was able to do back in 1960. Let's use the Arduino to read our binary counter and display the result as a decimal number. Of course, we could just add up the pulses we sent to the counter in the first place and get the correct number, but it is more fun to read our binary counter and convert the inputs back into a decimal number.
We bring the anodes of our LEDs to pins 5-10 of the Arduino, which we put in digital read mode. (Pin 5 goes to the least significant LED, 10 to the most significant, etc.) To collect our binary data into a single digital number, we read each pin and shift its contents left by its significance, add all six up, and we have our number stored (still in binary) in a single integer variable. (See software for more details.)
Now all that's left is to display it. For that, I'm using a 1 inch OLED display using the ss_oled library. The display uses an i2c interface at pins A4 and A5. I won't go into detail here about using the display, but what I am doing here is basically similar to the examples in the library - nothing complicated.
One note on software is that we have to wait several milliseconds after incrementing our count before reading it from the display. That is because it takes time for the relays to open and close and there is additional time required for changes to propagate down our chain of flip flops. I am actually waiting a full 50 msec. before reading the counter! BTW - That propagation delay down a chain of flip flops is one of the major issues limiting the speed of real computers. Microcontroller designers go through all kinds of tricks to avoid it and maximize the clock rate of modern processors!
But you might wonder how fast we can make our counter go if we don't worry about the display and propagation delays. Here is the answer.
It is still not that fast - it takes about 4 seconds to count to 64. That's about 16 Hz. At some point, it doesn't matter how fast you send pulses into the counter. The relays only open and close so fast, so at some point, they start ignoring extra pulses! For the display to work (again because of propagation delays down our chain of flip flops), we need a slower speed yet, to about 1/2 our max speed.
SoftwareWhat you see in the video with the OLED display (2nd one up from here) is the Arduino sketch provided with this tutorial. It is set currently to 4 counts per second, but you can speed it up or slow it down with the #define statement at the beginning. It can be used for all of the different stages of the project shown in this tutorial. The ss_oled library used in this sketch can be downloaded directly from the Manage Library tool. The stdlib.h library should already be installed.
Final ThoughtsSo that's our project! Perhaps a good learning experience. Perhaps gives us a little more insight and appreciation for all that goes on in the digital hardware we all take for granted today. And as I said in the beginning, maybe a good STEM project for a group of young digital electronics enthusiasts!
My original project back in 1960 was easier in some ways that this project was. I think I was using 24 volt AC relays - I don't remember dealing with a DC power supply. I think a 24 volt transformer powered the whole thing, meaning it could close two 24 volt relays in series, so I didn't have to operate my relays at twice their recommended voltage like I had to this time. And I had an extra set of contacts on my relays. No extra circuitry required to display the status of the counter - a neon bulb and resistor did the trick, powered directly from the AC line. And of course, I didn't have an Arduino or anything like it to convert my binary number back to decimal.
In this current version of our counter, we used the Arduino to generate the input pulses to our counter. You might wonder how that was accomplished back in 1960. There is another relay circuit that we won't go into here, but it was a way to create a multi-vibrator circuit using a couple of relays, along with a potentiometer and capacitor - a relatively simple circuit. It provided an adjustable rate series of pulses for my original counter.
You might also wonder where the idea for a relay based toggle flip flop originally came from. I found it somewhere, but like all of us that are old enough to remember life before the Internet, I struggle to remember how we got along without the Internet! I have no idea how or where I found it!
Comments