Atmel power control for a Lionel train

Building the Board

Rather than have a custom PCB etched, I decided to build the relay controller on a Eurocard 3-hole island protoboard. This made the project cheaper, and I could pick up everything at local suppliers. The microcontroller is an Atmel ATmega164P [2] (also called an "AVR"). I had one on hand, and it has enough I/O to control the relays without having to use any expansion chips.

Each GPIO pin on the AVR drives one relay with a transistor between them to handle the current. Ports A and C are used only to drive the relays. Port B contains the programming pins, and Port D has the UART, so the last eight relays are split between the available pins in those two ports.

Once I had all of the components, I laid them out on the protoboard. When I was happy with the arrangement, I soldered all of the sockets and connectors in place. Next, I added all of the transistors. The Eurocard is exactly the right width for 12 transistors from top to bottom, with two rows to control all of the relays (Figure 6).

Figure 6: The board with connectors and one row of transistors in place. All of the connecting wiring is on the bottom of the board out of view.

The next step was to add the resistors between the AVR and the transistors. When working on a protoboard the components themselves can also serve as jumper wires! Finally, all of the remaining wire connections were made. Because I had plenty of I/O available, I didn't use any of the programming pins for other connections.

Microcontroller Firmware

Microcontrollers are single-task devices, that is, they only run one program. There's no operating system between the program and the hardware, so timing is very precise. The trade-off is that their computing power can be limited. An application like this, in which the goal is just to decode a serial command and turn on GPIO pins, is well within its reach.

Atmel AVRs and PIC programmers are two of the most popular brands of microcontrollers. Arduinos fall into this category as well; the commands you use in your sketch get translated to C commands to be compiled. On the chip itself an extra bootloader handles programming so that everything is transparent to you as the user.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content