Countdown timer with random intervals

After the random timer based on an Attiny24 I have build a larger version of the random countdown timer. The Attiny24 had a single channel and two potentiometers. In this countdown timer the number of outputs have been increased from 1 to 8. The potentiometers have been replaced by a 2×16 HD44780 LCD screen and a rotary encoder.

Multichanel random timer with LCD and rotary encoder

With this random countdown timer circuit you can turn on and off 8 outputs. The outputs are, while randomly turned on and off, still grouped together meaning that as a group that at the beginning and half way the countdown timer cycle they are all on or off at the same time.

With this circuit you can simulate people turning on and off internal house lighting in a street on a model railroad track. Read on and see it in action.

You can use the LCD screen and the rotary encoder to configure the countdown timer. Turn the rotary encoder 1 step clockwise and the lcd screen will diplay the “On time”.

Multichanel random timer configuration menu showing on timeThe “On time” is half of the duration of the countdown timer cycle. It will take up to a maximum of “On time” to enable all outputs. In this example the first half of the timer cycle is 30 seconds.Multichanel random timer configuration menu showing on time configuration changePress the rotary encoder knob one time and the pointer goes to the value. Turn counter clockwise to decrease the timer value. Here is it decreased to 28 seconds. Press the knob another time and you are back to browsing all the settings.

Multichanel random timer configuration menu showing on randomThe next parameter is the “On random” setting. A setting of 50% means that is will take up to a maximum of 50% of the “On time” to turn on the outputs of the countdown timer. With an “On time” of 30 seconds it means that anywhere between 0 seconds and 15 second the outputs are randomly turned on.

Multichanel random timer configuration menu showing off randomMultichanel random timer configuration menu showing off time

The “Off time” and “Off random” are similar to the “On” settings, but this time it will configure how long and when the outputs turn off. “On time” plus “Off time” together define the countdown timer cycle duration. 30 seconds plus 30 seconds is a total of 60 seconds for the countdown timer cycle.

Multichanel random timer configuration menu showing intert outputsThere two more settings. The “Invert outputs” is handy for driving different LEDS or a relay boards. When invert is “0”, the output is 5V when turned on. When “1”, the output is 0V. 0V is needed at the inputs of the relay board to turn on a relay. Multichanel random timer configuration menu showing random extra

The other setting “Random extra” can be used when you have two or more countdown timers working at the same time. If “Random extra” is the same on the two boards then right after power on the two boards appear to switch on and off exactly the same. While random, the randomness is preprogrammed (using my random number generator). When setting the value to something other than 0 then some random numbers skipped in the table. In this way they appear both being random. When you leave this setting the same, after a while, because of fabrication variances the internal clocks will be different and thus the countdown timer outputs.
single group of components that drive 1 relay

I have used a standard 8 channel 5V relay board that I found on eBay. These relays are driven by 8 outputs of the microcontroller. The relays are driven by a transistor that in turn is driven by the output of an optocoupler. The input of the optocoupler is connected to an output pin of this microcontroller. This is repeated 8 times for all 8 relays.

Besides the relay board there is a rotary encoder knob and a standard 16×2 HD44780 LCD screen to see the status information and to change the settings of the countdown timer.

multichannel random countdown timer schematicThe schematic is fairly simple. The connections on the microcontroller are chosen in such a way that they can easily connected to all components when mounted on a prototype board.
Diode D1 is a life saver. When you mistakenly swap polarity nothing happens and your circuit is protected. Connector JP2 in my version is a female header that will mate with the male header of the relay board.
The microcontroller running the countdown timer program can be any 28 pin Atmega48-like controller. In the version you see in the youtube movie I used an Attiny48. I think these are a bit under appreciated. The Attiny48 gives you a nice amount of IO pins for a small price. It is not as feature complete as an Atmega48, but if you do not need the missing peripherals then an Attiny48 is a fine choise. Also, because of the missing peripherals, you will get a little bit more program space because the number of interrupt vectors is also less.

I compiled the countdown timer program for a few pin compatible AVR micro controllers. Namely the Attiny48, Attiny88, Atmega48(p), Atmega88(p), Atmega168(p) and Atmega328(p). The larger microcontrollers (>4K flash) contain a bigger random number table. You can download all the hex files here: random timer lcd hex files EN.zip. I also have one translated into the Dutch language: random timer lcd hex files NL.zip. You can use the .elf files to program the flash, eeprom and fuses all at once. If you use avrdude then use the .hex, .eeprom and .fuses files. The .fuses file contain the fuse settings of for each microcontroller type. Open it and you will see something like this:

Contents of section .fuse:
 820000 62d5f9

This means you should program the low fuse to 0x62, the high fuse to 0xD5 and the extended fuse to 0xF9. Use the Engbedded AVR Fuse Calculator to get the command line arguments needed for avrdude.

It should be possible to use an Atmega8 controller with this countdown timer, but as I do not have one laying around I could not test it. If you want to try and change the source code to support the Atmega8 you can download it here: random timer lcd source.zip. What you need to do is edit timer.c and provide a 100ms timer tick generated by the Atmega8 timer.
I developed the countdown timer using the Atmel Studio 6.1 integrated development environment.

The HD44780 16×2 LCD is driven by a library made by Pascal Stang. It is used in 4 bit mode.

The rotary encoder algorithm is inspired by the one made by Ben Buxton. It uses a finite state machine that needs no de-bouncing of the rotary encoder inputs.

Have fun! Building one yourself? Or have a question? Post it in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *