Path: AVR_EN => Applications => Model railway crossing gate
Gate ATtiny24 Applications of
AVR Single chip controllers AT90S, ATtiny, ATmega and ATxmega
Servomotor controller with an ATtiny24

Model railroad crossing gate with an ATtiny24 and a servomotor

Download this page as zipped file (1.078 kB) Download this page as PDF (1,123 kB)

If you are this is a small and simple to build project that fits your needs. The whole software is documented here, so you can even understand how it works. If necessary you can inspect it instruction word by instruction word, no hidden things or intransparent secrets here. And it provides you with a very flexible and easy to adjust piece of lean, stand-alone hardware.

1 Properties

Here is a servomoter controller for model railroad crossing gates with the following properties:
Hardware Mounting Software Pictures/videos

1 Hardware

1.1 Schematic

Gate tn24 schematic The schematic is simple. The controller ATtiny24 controls it all. Three 10k trim potentiometers are attached to the ADC0, ADC1 and ADC2 pins. The red/green duo LED is attached to the port pins PB0 (anode red, cathode green) and PB1 (cathode red, anode green). The two push buttons are on the INT0 and PCINT7 input pins. The pulse-width-modulated signal for the servomotor is generated on output OC1A. If programming of the ATtiny24 shall be performed a 6-pin In-System-Programmer can be attached via the standard ISP6 plug.

1.2 Electrical functions

All electrical functions are performed by the micro-controller ATtiny24.

1.3 PWM signal for the servomotor

PWM signal duration The PWM signal for the servomotor looks like this: the pin is on for a certain period and off for the remaining duration to 20ms.

Active signal duration The active signal duration determines the position of the motor. It can be between 900 and 2,100s length. The short duration brings the motor to its rightmost position, the longest to its leftmost position. 1,500s is the middle position. The position of the trim potentiometer P1 varies the signal duration from 1,500 (trim = 0) down to 800s (trim = 270) for closing the gate. Trim potentiometer P2 varies the signal duration between 1,500 (trim = 0) and 2,200s (trim = 270).

Speed control of the motor When opening or closing the gate the signal duration changes: when closing it gets shorter, when opening it gets longer. In both cases movement stops when the lower and upper limit is reached. The speed of the in- and decreases is determined by the trim potentiometer P3. On P3 = 0 increase and decrease lasts for seven seconds (350 PWM cycles). If P3 = 270 the whole movement lasts only one second (50 PWM cycles). Changes of P3 come only into effect when the gate is moving.

1.4 Variations of the hardware

If you need two LEDs (one for each side of the gates): just connect the second LED with its own current-limiting resistor to PB0 and PB1. Up to seven LEDs can be driven by these two outputs pins.

If you need only two, you can also connect them serially and reduce the resistor down to 100 Ω (with 5 V operating voltage). Or if you need four: connect two times two LEDs serially and with two resistors of 100 Ω to PB0 and PB1. Up to 14 LEDs can be driven in this way.

If, instead of two push buttons, you want to use only one switch that is switching to close or open the gate, use a one with a middle pin. Connect the middle pin to ground (GND) and the two other pins to the INT0 and PCINT7 input pins. That works fine, too.

Other extensions, such as receiving and decoding remote control signals on e.g. the PA3 input pin, require some additional software. Controller-internal hardware such as the 8 bit timer TC0, flash storage space, SRAM for storage or six upper registers, of which two register pairs can be used as pointers, and six lower registers are still available. Enough resources that allow for own extensions.
Top of page Hardware Mounting Software Pictures/videos

2 Mounting

Mounting comes in two versions, V2 and V3. The only difference between those concerns the pins for the servo motor: The latter is for plugging in standard servomotors directly. Both versions are 50-by-40 mm.

2.1 PCB Version 2

Gate tn24 pcb layout V2 Component placement on V2 Two bridges have to be soldered in this version.

2.2 PCB Version 3

Gate tn24 pcb layout V3 Component placement on V3 Three bridges have to be soldered in this version.

The final product looks like that:

PCB from above PCB from below

Top of page Hardware Mounting Software Pictures/videos

3 Software

The software comes in two versions, too. Version 1 is the older original source from 2004, translated to english. Version 2 is more recent, makes better use of the available resources in the ATtiny24 and is my favored. The functionality is nearly the same. Both source codes are in assembler.

3.1 Source code V1

The source code can be downloaded here and can be viewed in the browser here. The following describes how it works in detail.

3.1.1 Functioning of the software V1

See the extensive further comments in the source code in an editor for detailed descriptions of the functioning. On start up of the controller the current status of the trim potentiometer is read in, the 64 values on each channel are summed up (0..65,535) and the positions are converted as follows: The three values are written to registers.

On start up the internal hardware of the controller is set up:

3.1.2 Interrupts

All further execution is interrupt-controlled. All further actions are triggered via flags.

3.1.3 Flags

Following wake-up of the controller by interrupts (sleep mode idle) flags are consulted to check if handling is required. Following flag handling the controller is send to sleep again.

The following handling routines are triggered:

3.2 Version 2 of the source code

A new version V2 has been posted here, wich can be viewed in HTML format here. The new version has the following improved properties: The new version is much simpler in its structure, much easier to understand and considerably shorter than the previous version.

The source code includes a detailled description of the software's functioning. Open it in an editor of your choice to go deeper into that.

PWM signal This is the PWM signal generated on the OC1A port pin. This is rather exact.

4. Pictures and videos

Here is a picture after the servomotor reached its closed position and the red LED is permanently on.

Gate tn24 closed

And here the gate is in its open position and the green LED is on.

Gate tn24 opened

this video the gate moves slow from the open to the closed and back to the open position. In this video the movement is fast. If those videos do not play automatically in your browser, download them and start them locally in your favored video player.

Here two videos show a steam hammer model that uses the control device.

Top of page Hardware Mounting Software Pictures/videos

©2014-2018 by