Thursday, July 23, 2015

Controlling a USB Relay with a Raspberry Pi

I love the Raspberry Pi.  It allows me to prototype ideas quickly and without too much effort, but one thing I despise is the GPIO.  Maybe despise is too harsh, but it's a clunky way to interface electronics to what is for all intents and purposes a striped down personal computer.  Don't get me wrong, the GPIO needs to be there and I've made use of it before, but if I want to quickly prototype something I have to spend time figuring out how to use it and then I've got jumper wires all over the place.  I much prefer to use devices that are connected to the USB port.  They can be easily reused on other computers and the final solution is much neater.

Recently I've been toying around with a project that needs to switch a load on and off.  It's a low voltage LED light so nothing too complicated, and I have no problem figuring out how to switch the load via a transistor connected to one of the GPIO pins, but everything gets messy and then I have to do up a small prototype board to mount the parts on.  You know what would come in handy?  A USB controlled relay.  Unsurprisingly someone has already thought of that, they're all over eBay and Aliexpress, some are controlled as USB HID devices and some use the good ol' USB to UART method.  I bought a couple of the USB to UART style devices from to play around with, and at $10 AUD each it's not worth my time to come up with a custom solution.

Top Side of PCB
There's not much to the module.  As you can see above it's what you'd expect.  A USB connector, a relay, a set of terminal, and a few electronic components.  When plugged in, you send commands to it over a virtual com port to turn the relay on and off.  There are two status LEDs, one to indicate the device has power and another to indicate the relay is active.  Couldn't be easier.

PCB Mounted Relay
The Songle relay used seems to be the de-facto standard in this area of the market.  It's a standard NC/NO relay that can be switched by applying 5 Volts to the coil.  The data sheet isn't too clear but requires 90 mA or less to actuate the relay.  The contact resistance of 100 milliOhm isn't the worst I've seen either.

Between coil & contact - 1500VAC 50/60HZ (1 minute)
Between contacts - 1000VAC 50/60HZ (1 minute)

The relay is sufficiently rated for mains voltage use and has adequate isolation, but I wouldn't use it.  I don't like running mains voltage through random crap I bought off eBay.  Besides that, even if the relay is rated for 10 Amps, I'd be surprised if the tracks on the PCB (hidden under the relay) are up to the job.  The connector definitely looks like it's not rated for that much current.  Based on that, this device will only be used for low voltage medium current applications.

CH340T USB to Serial Controller
As mentioned before, the relay is controlled by sending commands over a USB to serial converter.  In this case a CH340T IC is used.  The only data sheet I could find for this chip was in Chinese and wasn't very helpful.  The Raspberry Pi has the drivers by default and I've read that the latest versions of Windows do as well.  Disappointingly, the serial number of the devices aren't set or are unable to be read in Linux.  This has the side effect that you can't connect two of these relays to a computer and deterministically know which relay is which.  In Linux, if two devices are plugged in at once they enumerate at /dev/ttyUSB0 and /dev/ttyUSB1, but you can't be certain that they enumerate in the same order if you reboot.  I'd hoped that I'd be able to tell them apart by reading the serial number of the USB devices, but as they aren't set, no luck.  It doesn't really matter, I only need to use one of them, but if I did need to use 2 devices I could buy a two relay board.

Edit - Bingo.  I think I've found a way to do it by using the physical mapping of the ports.

pi@raspberrypi ~ $ ls -l /dev/ttyUSB0
crw-rw---T 1 root dialout 188, 0 Jul 22 19:46 /dev/ttyUSB0

pi@raspberrypi ~ $ ls -l /sys/dev/char/188:0
lrwxrwxrwx 1 root root 0 Jul 22 20:01 /sys/dev/char/188:0 -> ../../devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3.1/1-

pi@raspberrypi ~ $ lsusb -t
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=dwc_otg/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/5p, 480M
        |__ Port 1: Dev 3, If 0, Class=vend., Driver=smsc95xx, 480M
        |__ Port 3: Dev 5, If 0, Class=hub, Driver=hub/4p, 480M
            |__ Port 1: Dev 7, If 0, Class=hub, Driver=hub/4p, 480M
                |__ Port 3: Dev 20, If 0, Class=vend., Driver=ch341, 12M
                |__ Port 4: Dev 10, If 0, Class=stor., Driver=usb-storage, 480M
            |__ Port 3: Dev 8, If 0, Class=vend., Driver=r8712u, 480M
        |__ Port 5: Dev 9, If 0, Class=HID, Driver=usbhid, 1.5M

The 1- string describes the physical layout of the USB device.  The device is plugged into port three of the first hub which is plugged into port one of the second hub which is plugged into the Raspberry Pi on port 3.  I've highlighted the important bits to make it a bit clearer.  BTW The reason I use two hubs is because I have a USB 3.0 hub connected to the Pi and the USB 1.0 USB to serial converter needs to be connected to a USB 2.0 hub for the pi to see it.  It's a bug with the Pi.

Bottom Side of PCB
The quality of the board isn't too bad.  All the SMT parts that are automatically placed have nice clean solder joints, but the hand soldered through hole parts leave a lot to be desired.  There's a lot of flux residue on the board and the pads are too small causing the solder to ball up on the leads resulting in weak joints.  I have to give them points for routing out an isolation slot in the board though.

Operation is pretty self evident.  The SOT-23 transistor Q1 is used to apply current to the coil of the relay.  If you look at the top of the board there's also a reverse biased diode placed across the relay coil to stop back EMF spikes.

IC with Markings Scratch Off
The IC on the top handles the USB to serial conversion, but the serial commands still need to be interpreted and used to control the relay.  I assume the IC on the bottom side of the board with its markings removed is some sort of micro-controller to do this.

Controlling the relay is dead simple

You first need to configure the serial port at 9600 baud with a character size of 8 bits.

stty -F /dev/ttyUSB0 speed 9600 cs8

Then you send the command (in hex of course) A0 01 00 A1 to close the contacts

echo -n -e '\xA0\x01\x00\xA1' > /dev/ttyUSB0

Then you send the command A0 01 01 A2 to open the contacts.

echo -n -e '\xA0\x01\x01\xA2' > /dev/ttyUSB0

Tablet and Relay
Testing the Relay
I think these are great for prototyping.  Not only can I use this on a Raspberry Pi, I can use it on a desktop computer or possibly a phone or tablet (not tested).  It's also great for people that are coming from the software world who may not be comfortable interfacing with the GPIO port of the Pi.  They can plug it in and deal with what they know best (software), and within a few minutes control devices like fans, pumps, lights or whatever else you could imagine.


  1. well I have the eight relay model works fine windows 10, but I want it to work off an Arduino Yun or Raspi 1,2, any help would be greatly appreciated, I used to be pretty good at all this but alas I had a stroke n this has shall we say made it difficult, its hard to remember what you have forgot.

    1. Hi. I didn't get too far with my project but I might be able to help. I've never used an Arduino but have used a raspberry pi. I found a site that has an example python script to control the relays. If I were to write a script to do this it would probably look a lot like this. I'm not sure if you're familiar with Python, but it's not too hard to get started. Let me know how you go.

  2. This comment has been removed by the author.