Topic: Controlling a relay with Ruby
Date:  2016 MAR 21
We’re going to control something mains powered with a computer! In the case of my old, low-feature EPROM eraser, this means that if a Bad Thing happens, the UV lamp could turn on which is potentially damaging to eyes and skin! Always unplug whatever you’re controlling before working on it, DO NOT DEPEND ON THE SOFTWARE/COMPUTER TO DO THE RIGHT THING!
I’ve got a cheap old EPROM eraser which simply turns on when you plug it in, and turns off when you unplug it (no safety interlocks, even!). Erasing EPROMs usually involves filling it up, plugging it in, and setting a kitchen timer or something for 20 minutes. The trouble is, you have to remember to unplug it when the timer goes off, which involves stopping what you’re currently doing or forgetting about the EPROMs and overerasing them into uselessness. I’ve already built a number of optoisolated relay switched outlets for various projects, but didn’t currently have a timer to control them. Enter Ruby!
(View on GitHub for direct download)
Being as the relay controller was an optoisolated current loop, I just connected it to the RTS handshake pin on a USB -> RS-232 adapter. The RTS pin is typically used to signal to a connected device that the computer is ready to send some data. It, along with the other output handshake signals, can be used as a 1-bit output port under software control. The serialport gem allows us to control the handshake lines from Ruby with ease:
I added in a quick progress bar using the ruby-progressbar gem. Its documentation covers both basic and advanced usage.
The control of the relay is wrapped in a
begin-rescue-ensure-end structure, which captures
SIGTERM. This makes sure that we turn the relay off if someone bashes CTRL+C or uses
For wiring, simply use pins 4 and 7 (RTS and GND) on a DB-25, or pins 7 and 5 on a DB-9. You can stick a LED directly across them (they’re current limited) to test. Since the RS-232 port reverses polarity between mark and space, you can reverse the connections to invert the action on the LED. Use a two-color, two-pin LED and try toggling the RTS pin if you’re unsure.