Basic electronics - clocked on/off timer
July 20, 2019 10:39 AM   Subscribe

This is proving too hard to Google. I want to select a time in minutes and seconds, have a thing do a thing for that long, then stop.

It seems like all I can find on Google is (a) a 555 timer and select the precise values of resistor and capacitor to make the time right. (b) program an arduino to do it. There's nothing in between? I find that hard to believe.

So I've got a new interest in analog electronics and DIY simple digital logic controllers. Coincidentally, at work I just had occasion to dig up an ancient counter-scaler and use it for... reasons that are not important to the question. This thing is really old. Actually I have a few, one has a nixie tube display, and I previously had one just like it except it didn't even have that, only a matrix of light bulbs indicating the output digit. Which is to say, I'm betting this does not involve a software-programmed microprocessor of any kind.

Anyway, I started looking at the thing (I'm not allowed to take it apart) and thinking, hey, that would make a neat project. What it does: count pulses. That's it. Starts at zero, counts for an amount of time, tells you how many happened. Here's the trick though - you select each time how many minutes and seconds you want it to count for, then it stops counting by itself, leaving the total on the display.

How? Note that this isn't so much a practical question, more of a learning fundamentals one, so videos and forum posts like "buy the LM27XRQ chip and connect pin 1 to..." don't (fully) answer the question. I want to know how it works.
posted by ctmf to Technology (17 answers total) 3 users marked this as a favorite
 
Take a look at this post from a few days ago. Watching the video tutorial on how to make a VGA card from 7400 series logic gates will show you what's involved in what you want to do.

Putting together logic to do what you need should be fairly straightforward. Having access to an oscilloscope would help with debugging the logic.
posted by monotreme at 11:04 AM on July 20, 2019 [1 favorite]


i can imagine a few ways it might work, one being a mechanical egg timer mechanism that completes a circuit when the timer reaches zero. no fancy electronics required.
posted by zippy at 11:05 AM on July 20, 2019


(for the timer part)
posted by zippy at 11:08 AM on July 20, 2019


It's possible to do this without a microcontroller, but it would be a lot more work. I'm not sure what your level of knowledge is, so I'll explain starting from the basics.

Your fundamental building blocks are transistors, from which you can build the usual assortment of logic gates (AND, OR, NOT, etc.) You can use these logic gates to build combinational logic: a circuit that computes an output as a function of its inputs, but otherwise has no "memory" or "control" ability of its own. For example, you could build a circuit that takes a binary number and adds 1 to it, or a circuit that takes two binary values and selects one of them based on the output

You can also build sequential logic using things like latches and flip-flops (which are themselves built out of logic gates) to create circuits with memory. This is crucial for non-trivial systems because it allows you to build a system whose behavior changes over time. For example, in your application one part of the state would be the current value of the counter, and another part would be a bit controlling whether the timer is currently running or not.

To build something like what you describe, you'll need a mixture of combinational and sequential logic. A high-level overview might look something like:
  • Construct combinational logic circuits to add and subtract 1 from binary-coded decimal values of sufficient size, including carrying overflows from one digit to the next. (Binary-coded decimal is a bit messier than plain binary, but converting from binary to decimal involves division by 10 which is a huge pain, so this will make things a lot simpler overall.)
  • Use flip-flops to create registers for "number of seconds remaining" and "current counter value", and an extra bit to determine whether the counter is "running" or not.
  • The timer register's clock source comes from a 1Hz signal while the counter is running, or directly from button presses if not. On each clock tick, the register's "next" value is computed using combinational logic as a function of various inputs, including its "previous" value:
    • If the timer is running, or if the "decrement" button was pressed, the new value is the current value minus 1.
    • If the "increment" button was pressed, the new value is the current value plus 1.
    • If the "reset" button was pressed, the new value is zero.
  • Similarly, the counter register's input is set up to be incremented, kept constant or reset based on the state of the system, but this register can be clocked directly from the input signal (possibly after running it through a comparator to get a nice clean square wave).
  • The "running" bit is likewise controlled by "start" and "stop" buttons.
  • To display the timer and counter values, you need a circuit for each digit to convert a BCD digit to the corresponding pattern of signals to turn LED segments on or off.
Counting "minutes and seconds" is not much different from counting seconds, except that your logic for incrementing and decrementing has to be a bit different (e.g. the ten-second digit has to overflow when it reaches 6 instead of 10).

This is basically how you could build a frequency counter using 1970's technology, except with a lot more work done to optimize the number of components, handle various unusual combinations of inputs properly, and make everything respond as fast as possible. These days, unless you had particularly demanding requirements it would be vastly cheaper to do the same thing with a microcontroller or an FPGA.

If this kind of thing interests you, you might want to consider checking out the nand2tetris course, which explains from the bottom up how to from logic gates to complex digital systems,
posted by teraflop at 11:09 AM on July 20, 2019 [4 favorites]


There are a bunch of ways that you could do this. You're correct, a microcontroller is not, strictly speaking, required. You can do this with pure old-school TTL if you want to, and you're correct that there are devices out there like that. (I used to have an old HP timer/counter and it was definitely all 5V TTL inside, with a bazillion discrete LEDs for output. It was neat.)

You need (IMO, to do it right) more than just a 555, though; you should really use some sort of counter, rather than just using a huge capacitor to make the 555's one-shot delay suitably long. Here's a circuit that might serve as a starting point. It uses a 555 as a pulse generator and a 4020B binary counter to count the pulses. In the most basic configuration, you can only set it to trigger on 2^n multiples of the clock frequency; to make it settable to any value you'd need some switches or other method of setting a binary value, and then compare the two via logic gates (or whatever) such that the output goes high when they finally match. This is probably the canonical method of building a digital timer—at least, it's the way I did it back when I was in undergrad circuits class.

There are other methods, e.g. this is an interesting use of 555s that I've not seen done before, which results in zero power consumption when the timer is not running. I question how precise it would be, but assuming you're okay with toying with capacitor values it might be fine for your purposes. I don't really like it, or anything that depends on the precise value of relatively large capacitors like that, but to each their own.

There's probably an IC out there that with a minimum of outboard components will do exactly what you want, just because it's so common, although I'm not familiar exactly with one. (The closest I can find in a quick search are watchdog timer ICs, but they only go up to about 1.6s so not really suitable.) Probably the cheapest solution is going to be to get a dollar-store kitchen timer and tear it apart, rather than buying ICs from Digikey. (It would also be interesting to get a few and tear them down and see how this problem is solved at scale commercially. I bet there's some pretty brutal hacks in the cheap ones. Surprisingly I can't find many kitchen timer dissections in a quick search.)

Personally, I'd program an Arduino to do this. It's not really that much overkill by modern standards, and an Arduino-compatible microcontroller is hardly expensive—it's probably cheaper than buying a special-purpose IC, assuming you can even find one. And you can even do neat stuff like use rotary encoders to set the duration, if you want. The code to do that is out there, so you're really just cookbooking it. My guess is that with an arduino and some rotary encoders and a clock source, you'll end up with a lower parts count than you would with virtually any other method, and that lots of modern kitchen timers have microcontrollers internally anyway.
posted by Kadin2048 at 11:15 AM on July 20, 2019 [2 favorites]


In the industrial world there are time delay relays that can be programmed to do all kinds of tasks like this. Turn things on for a precise amount of time, close a contact every N seconds, etc etc etc. They can switch line voltage so you don't need a lot of extra wiring to make it work. They're in everything around you...you just don't normally see them.

They're pricier than, say, an Arduino setup, but they're a lot safer to work with if you are a novice to doing these things.
posted by JoeZydeco at 11:18 AM on July 20, 2019 [3 favorites]


Response by poster: monotreme: that channel is actually mostly responsible for my interest! The breadboard computer was amazing.

I was thinking in my fuzzy block-diagram, isolate the problem way:
A clock signal (calibrating to real-time a separate problem)
A binary counter, resettable to zero, counting up every clock cycle
A binary setpoint register, for comparing the counter to (converting desired min/sec to binary counts for the user a separate interesting issue)
An output go/stop signal

So the user would put in the desired count time and press the start button. The start button would reset the counter to zero and enable the output signal. When the counter matched the setpoint it would disable the output. So like a set/reset latch that sets when the start button is pressed, and resets when the counter matches the setpoint.

And I guess the comparison part could be basically Ben Eater's ALU, subtracting one from the other, and triggering on zero.

Although 8 bit resolution might not be enough. I'm starting to see why this thing I have is such a large, rack-mount size tank of a machine, because I bet there's no ICs in there.
posted by ctmf at 11:43 AM on July 20, 2019


Response by poster: Oh, and in case my terminology was confusing, the original machine counts random events for a certain amount of time, so that's completely separate from the clock counter used to control it.
posted by ctmf at 12:02 PM on July 20, 2019


I think your "block diagram" is about right.

Here's a project involving building a clock entirely out of discrete transistors. Not because you'd want to (though it's cool!) but because it provides some nice block diagrams of how the circuit is built up from components.

For the counter, something like the 4020B is a 14-stage counter, so it can count up to 16,384; the highest "place" output can also be run into the input of another counter to count even higher. So two of them linked together would let you count up to 2^28, which is 8.5 years when clocked at 1 Hz, or still a pretty decent length of time if you clock at 10Hz (which lets you use smaller caps on the 555 pulse generator). It has a reset pin that can be connected directly to a momentary pushbutton to clear everything.

Though teraflop's point is well-taken that you may be better off using BCD than binary. If you want to go that route you might want to look at the 74LS90. It's designed specifically as a BCD / modulo-10 counter, and what's even better you can attach its outputs via a 74LS47 directly to a 7-segment display input (this is described in that article). Like the 4020B, you can also attach multiple counters together in a "cascade", one for each digit. As long as you are happy working in base-10 BCD, you could build a timer with a reset switch pretty straightforwardly, one 7490 per digit.

As an aside, even though you said that calibrating the clock to real-time is a separate problem, one solution if your project will be plugged into the wall is to use the 60Hz powerline frequency (or 50Hz depending on where you are) as a clock reference. It's super accurate compared to anything you'd do yourself, and basically requires using an AC wall wart instead of a DC one (moving the AC-to-DC stuff into your project) and using a Schmitt trigger to square up the sinusoidal powerline waveform. The discrete-transistor clock project does this, as IIRC do many commercial digital clocks from the 70s and 80s that plug into the wall.
posted by Kadin2048 at 2:07 PM on July 20, 2019 [1 favorite]


(Professional engineer-type person here. I get paid to do this sort of stuff.)

The 555 timer is not a good choice for a project like this because it is not very accurate. In particular, its accuracy gets much worse the longer the period (lower the frequency) you ask it to produce, because that requires large resistors and large capacitors. Large resistances have small currents flowing through them, and the smaller the current you care about, the more trouble you will have with unintended leakage currents caused by things like the finite resistance of a circuit board. Large capacitances are... just plain ugly. There is really just no way to get an accurate large-value capacitor.

It is much easier to make things work well by starting with a high-frequency clock and dividing it down; that is how all accurate modern electronic clocks work (except for those synchronized to AC mains and, probably, some exotic stuff somewhere). Your high frequency clock source could be a 555, or better a crystal oscillator (they're really simple to implement), or best of all something using atomic physics (Rb, Cs oscillators). Those last ones are expensive but can be very, very accurate -- accurate enough to define the second itself.

In the medium-old days you would do the division with discrete TTL logic divider/counter chips, usually decade counters or binary counters. In the old-old days, you would have had to implement those counters yourself with discrete transistors. Nowadays the easiest way to do clock division is with a microcontroller, hence the recommendation for an Arduino. (An FPGA is way more capable and *way* more fun to program than a microcontroller -- I wish I got to do more FPGA work -- but much harder to get started using. They are also a great deal less efficient for small projects because they often have a large overhead to get the chip to do anything at all.)

There are dedicated chips for this sort of work, as Kadin2048 suggests. The best ones I know of are LTC ADI's TimerBlox parts. They are quite capable and would handle this job nicely, but they're ridiculously expensive for what you get. Each one costs a couple of dollars -- dollars! -- when you can get an entire Arduino-class microcontroller for about thirty cents. Since a real project will usually have something else useful that the micro can do, which one to pick is kind of a no-brainer.
posted by GSV The Structure of Our Preferred Counterfactuals at 2:15 PM on July 20, 2019 [2 favorites]


Don Lancaster's free to download TTL Cookbook has a chapter on timers/clocks.
posted by Sophont at 3:57 PM on July 20, 2019 [2 favorites]


To answer the other aspect of your question: As GSV (etc) alludes to, the reason you're not seeing anything online in between a 555 (not really the best tool for this job) and an Arduino is that the price/performance/ease of use specs for microcontrollers have moved so far in 20 years that for a huge number of applications it's silly to not use a microcontroller, especially in the early/prototype/DIY stages. For a bill of materials of about a buck, an open source toolchain, and about 20 minutes of programming, I can have something that counts arbitrary incoming pulses up to roughly a megahertz. Maybe more like 10MHz if I'm clever at using all the hardware. That's ridiculous! I literally spent 10 years teaching people how to do it the way GSV and Kadin2048 describe and it's hard to imagine a scenario where I'd want to open up that toolbox again, if only because making design changes is so much easier in software.

So what you're left with is that the people who do need to do it with strict digital logic aren't really hanging out in many internet forums chatting about it -- they're at the far end of performance needs and are probably professional engineers working for eg Nvidia, so they're either working off internal company design docs or they're getting their info directly from data sheets and application notes. The closest you're likely to find in the DIY/forum community is the FPGA crowd.

This is not to discourage you from doing it 1980-style! It could be a ton of fun and certainly will teach you a lot more about digital electronics than programing an AVR would. This is just to fill in the historical context for why you're not going to find forum posts about this as easily.
posted by range at 7:24 PM on July 20, 2019 [1 favorite]


I was thinking in my fuzzy block-diagram, isolate the problem way:
A clock signal (calibrating to real-time a separate problem)
A binary counter, resettable to zero, counting up every clock cycle
A binary setpoint register, for comparing the counter to (converting desired min/sec to binary counts for the user a separate interesting issue)
An output go/stop signal


Back in the 1980's I built a darkroom enlarger timer and a process timer entirely out of CMOS logic. They were built on 6"x4" Veroboard; the enlarger timer took two, the process timer four boards. Part of the process timer would be what you're looking for, as it counted down from a preset value in minutes and seconds (the enlarger timer counted down only in seconds from 999 max). It basically consisted of four presettable up/down counters (4510), each with a 4511 7-segment decoder and display. Presetting them to the time required and counting down is pretty easy, only the 10s counter needs a bit of additional logic to be preset to 5 on every minute decrement; I found that in a 4510 application note. You OR the zero output of all four counters and there's your stop/go. Presetting the time can be done using BCD thumbwheel switches (mine used a keypad and shift registers, but that gets a bit more involved; also, the second PCB was almost entirely for the keypad and the display). If you need longer times than 99 minutes you just add an extra counter stage. Time base was a 32768 Hz crystal, divided to yield 1 Hz pulses.
posted by Stoneshop at 11:52 PM on July 20, 2019 [1 favorite]


A darkroom timer meets your above-the-jump requirements, if thing1 needs 120v power. I use an old one as a kitchen timer. But I think you're looking for a differently-implemented solution.
posted by achrise at 5:28 AM on July 22, 2019


Coming very late to this: I'd modify your block diagram design slightly by having it count up from a set starting point until it overflows and wraps around to zero, rather than starting from zero and counting up to a detected finishing count.

The simplest counter it's possible to make is a ripple counter, where each flip-flop gets clocked by the falling edge of the output from the next less significant flip-flop. But that means that the ripple counter's flip-flops are not all clocked synchronously, which means that decoding their outputs is hard. While a ripple of clocks is in the process of propagating upward from the least significant bit, the outputs to the right of the ripple will reflect the old counter value at the same time those to the left of it still reflect the new one, and working around that can take enough logic to negate the simplicity of the counter itself.

But if the only thing you care about is detecting a falling edge on the most significant output bit of a ripple counter, which will occur only when the counter as a whole overflows, that's easy. And preloading a set of flip-flops to defined starting values takes about the same amount of hardware as resetting them would, so that's easy too.
posted by flabdablet at 11:31 PM on April 24, 2020 [1 favorite]


(a) a 555 timer and select the precise values of resistor and capacitor to make the time right. (b) program an arduino to do it. There's nothing in between? I find that hard to believe.

There are many, many things in between on a conceptual complexity vs accuracy basis, but I am unaware of anything between the build cost of a 555-based solution and a low-end microcontroller-based solution.

The class of microcontroller responsible for the irritating beeping tunes that emanate from novelty birthday cards would be more than capable of handling this use case in a way that's more accurate, less power-hungry, physically smaller and cheaper than a 555 hooked up to a large capacitor.

I think of the place of microcontrollers in the realm of electronic design as essentially parallel to that of human beings in biology. Both have the same One Weird Trick - language - that confers endless and rapid adaptability. There is very little need to design task-specific hardware any more, given the ready availability of universal machines that you can simply instruct how to do the job you want done.
posted by flabdablet at 3:01 PM on April 26, 2020


Just to hammer that point home: the cheapest microcontroller I could find at element14 is a PIC16F1933-I/SP. This device is way more capable than the minimum you'd need for a simple interval timer, and element14 will sell you one for AU$0.25.

They charge AU$0.36 for a 555.
posted by flabdablet at 3:20 PM on April 26, 2020


« Older Help requesting reports from court case   |   Safe Trampoline recommendations? Newer »
This thread is closed to new comments.