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 www.lctech-inc.com to play around with, and at $10 AUD each it's not worth my time to come up with a custom solution.

Electronics
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.

Electronics
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.

Electronics
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-1.3.1.3/1-1.3.1.3:1.0/ttyUSB0/tty/ttyUSB0

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-1.3.1.3:1.0 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.

Electronics
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.

Electronics
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.

Sunday, July 12, 2015

Raspberry Pi USB Webcam Testing

This post will unfortunately be very short.  I was playing around with a webcam connected to a Raspberry Pi, and just as I was starting to make headway I got sick.  I was using a Logitech C525 to take still images.

webcam
Logitech C525


I got far enough into the project to realize that the camera didn't have enough resolution for what I wanted to do.  So I need to look at other options.

Just to document what I've done I'll include the commands I was using here.  fswebcam was used to take the still images and uvcdynctrl is used to configure the camera, but it's poorly documented and I'm still trying to figure out how to use it.  You may need to install packages to use these commands.


fswebcam  -r 1920x1080 -s brightness=70% -s gain=50% -S 10 test.jpg
uvcdynctrl -L a.txt
uvcdynctrl -s "Focus (absolute)" 220


I promise I'll do better next week.

Wednesday, July 1, 2015

Large LED Panel Light Testing

I've recently wanted to do more videos and take better pictures for the blog, but I've been limited by my lighting.  At the moment most of my photography is done under 2 LED tube lights that illuminate the whole room.  They appear bright, but in reality there isn't enough light for photography, and although they're not as bad as point sources, I get long straight reflections on shiny objects in my photos.  What's needed is a large, bright, distributed light source that I can place above my desk.  It should remove most reflections, and also reduce shadows when working.  I've previously played around with a small LED light panel and it seemed a larger version would be perfect for the job.  After looking at all the options available on AliExpress (there are a lot),  I finally choose a single large 80W panel.

LED Light Panel
80 Watt LED Panel
This thing is massive.  It's 600mm x 1200mm x 8mm, the size of a standard ceiling tile you'd see in any Dilbert style office.  According to the specs it's an 80W panel with an output of 8000 lumens.  That gives a luminous efficacy of 100 lumens per Watt, which is a pretty standard value for white LEDs.  It has a reasonably high colour rendering index of above 80 to ensure faithful colour reproduction.  The colour temperature is somewhere around 4000K, which is the colour I'm most comfortable working under.

Electrical Specs
LED Panel Specifications
I was a little nervous ordering something so large and expensive from China.  When picking up the panel from the post office it became obvious my concerns were valid.  It was shipped in a box with the power supplies and they were allowed to freely move around.  It seems at some point a weight was placed on one end while the panel was resting on the power supplies and it was bent on the long edge by about an inch.  No problem though, I was able to easily bend it straight.

LED panel light
Meter ruler placed along the edge of the panel
.
LED panel light
Maximum deviation of the panel from straight
What's the point of buying something if I don't pull it apart?  After removing the back plate the construction method became obvious.  There are two white LED strips on the long edges of the panel, each containing around 200 x 200mW LEDs.  The panel uses the same method to get light from the edges to the face of the panel as the smaller light described in a previous post about LED panels.  There are two large thin sheets of foam to keep the plastic layers firmly pressed together with a return wire running on top of them.

LED panel light
LED panel with back removed face down
.
LED panel light
LED panel with back removed lit up
.
Pattern on back layer to disperse light
The frame is flimsy aluminium that's spot welded in the corners.  From what I can gather each part isn't overly strong but when assembled they make a fairly rigid product.  As usual in these lights, the wiring isn't great.  There isn't any significant strain relief and it looks like constant flexing of the cables could cause the solder joint to fail.
LED panel light
Welded corner and wiring
I wasn't willing to completely disassemble it as I think it'd be easy to damage if I wasn't concentrating.  I was however able to see the LED strip sandwiched in between the frame and the optical plastics.

LED Strip
LED strip
The light comes with two power supplies, which is disappointing, I would've rather paid less and bought separate power supplies as I don't intend to use these.  They're a no frills standard LED driver.  They take a multi voltage mains input and can drive an LED string at 30 to 40 Volts at 900 mA.  The astute among you may have noticed that's only a maximum of 36W.  So it's likely they're slightly underdriving the panel, which isn't a bad thing.

power supply
40W - LED driver module
I balanced the panel between the backs of two chairs to take some test shots.  I think they turned out OK.  There aren't too many severe reflections and the light seems bright and evenly distributed.  Even with the crappy point and shoot camera I use, the colours seem vibrant.

Pencil Case
Case that holds my metal files
.
Eelctroncis
Raspberry Pi
.
Electronics
DC - DC converter module
It wouldn't be right If I didn't use this panel light to pretend to be a doctor and look at some X-rays.  Luckily I had some laying around from few years ago.

X-ray
Coronal slices of my sinuses
Look at that photo.  Ain't I a handsome devil?

X-ray
The inside of my head
Overall I'm happy with the product.  Yeah, it could have been packed better but it's not a major problem and you forget about that as soon as you see it working.  It really is quite impressive to turn this thing on have it basically blind you.  The next step is to make some sort of frame to mount the light above my desk.  I think welding may have to be involved.