Atmel power control for a Lionel train

RS232, RS422, RS485, Oh My!

Serial communication can come in many electrical forms: RS232, RS422, and RS485 are just a few. Each RS defines different voltages and physical line configurations to represent ones and zeros, but all of the timing is the same.

Electrically, though, they are not compatible, and in fact all of these would damage an Arduino or microcontroller if connected directly. All of the voltage levels defined by these specifications are outside of the tolerance levels for a microcontroller.

An Arduino programmer [3] is a special form of USB-to-serial adapter that adjusts the voltage for you. When it's time to program an Arduino, you might plug in a programmer that looks like a serial port to the computer but limits its voltage to 5V or 3.3V (Figure 8). Because the timing is the same, it all works out.

Figure 8: An Arduino programmer. Even though the microcontroller isn't an Arduino, its voltage levels are compatible, so this allows it to talk to the computer.

Speaking Serial

Now that I have a relay control board that's listening for serial commands, I designed the communications protocol itself. Whenever I design communications between devices, I start with a unique character. Punctuation works well, especially brackets, curly braces, angle brackets, or parentheses because they come in pairs.

The relay board only has one command: Assign a block. This is followed by two parameters: the block to assign and what to assign it to.

Therefore, to form the command, I start with a curly brace ({). When the Atmel receives this, it will reset all of the serial variables and get ready for a new command.

Next, I send a B or assign a Block; then, I send the block I want to assign. This is a single character from 1 to 9 or A, B, C for blocks 10, 11, and 12. A 0, 1, or 2 is next to specify which throttle I want. Finally, I end the command with a curly brace (}). When the Atmel sees it, it processes the command.

In the future I can add more commands that mirror a block, clear a block, or initiate an emergency stop by following this same format and adding functions in processSerial() (lines 41-64).

Buy this article as PDF

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

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content