{"id":338,"date":"2018-08-11T17:59:30","date_gmt":"2018-08-11T16:59:30","guid":{"rendered":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/?p=338"},"modified":"2024-11-21T23:00:25","modified_gmt":"2024-11-21T23:00:25","slug":"usb-relay-boards","status":"publish","type":"post","link":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/?p=338","title":{"rendered":"USB Relay Boards"},"content":{"rendered":"<h4>FTDI USB Relay Board<\/h4>\n<p>A while back I wrote a <a href=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/?p=241\">post<\/a> about using an FTDI breakout board as a GPIO board from Python. Since then, I have discovered that there are USB relay boards based on this concept available from China. SainSmart seem to sell them, but they are available on ebay, Banggood and Aliexpress. The boards are interesting as they do not contain a microcontroller &#8211; just an FTDI chip and an ULN2803 array of Darlington pairs to drive the relay coils.<\/p>\n<div id=\"attachment_339\" style=\"width: 490px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2018\/08\/sainsmart8relay.jpg\"><img aria-describedby=\"caption-attachment-339\" loading=\"lazy\" class=\"size-full wp-image-339\" src=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2018\/08\/sainsmart8relay.jpg\" alt=\"Relay Board\" width=\"480\" height=\"480\" srcset=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2018\/08\/sainsmart8relay.jpg 480w, https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2018\/08\/sainsmart8relay-150x150.jpg 150w, https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2018\/08\/sainsmart8relay-300x300.jpg 300w\" sizes=\"(max-width: 480px) 100vw, 480px\" \/><\/a><p id=\"caption-attachment-339\" class=\"wp-caption-text\">SainSmart 8 Channel USB Relay Board<\/p><\/div>\n<p>Using the setup previously detailed in my other post, it is possible to control one of these boards from Linux. The process is exactly the same as using one of the little red USB UART adapter boards.<\/p>\n<p>To use one of these boards from Linux :<\/p>\n<ol>\n<li><a href=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/?p=241\">Follow the process to set up your computer as per my old post<\/a>.<\/li>\n<li>Plug the board into a 12v DC supply, and your computer&#8217;s USB.<\/li>\n<li>Run the following example (as root \/ with sudo if you haven&#8217;t set up permissions for your user to access the device).<\/li>\n<\/ol>\n<pre class=\"lang:python decode:true\" title=\"FTDI USB Relay Board\">#!\/usr\/bin\/env python\n\nimport os\nimport sys\nimport ftdi1 as ftdi\nimport time\n\nftdic = ftdi.new()\nif ftdic ==0:\n\tprint( 'new failed: %d', ret )\n\tos._exit( 1 )\n\n# try to list ftdi devices 0x6010 or 0x6001\nret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6010 )\nif ret &lt;= 0:\n    ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6001)\n\nif ret &lt; 0:\n    print( 'ftdi_usb_find_all failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )\n    os._exit( 1 )\nprint( 'Number of FTDI devices found: %d\\n' % ret )\ncurnode = devlist\ni = 0\nwhile( curnode != None ):\n    ret, manufacturer, description, serial = ftdi.usb_get_strings( ftdic, curnode.dev )\n    if ret &lt; 0:\n        print( 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )\n        os._exit( 1 )\n    print( 'Device #%d: manufacturer=\"%s\" description=\"%s\" serial=\"%s\"\\n' % ( i, manufacturer, description, serial ) )\n    curnode = curnode.next\n    i += 1\n\n# open usb\nret = ftdi.usb_open( ftdic, 0x0403, 0x6001 )\nif ret &lt; 0:\n    print( 'unable to open ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )\n    os._exit( 1 )\n\n\n# bitbang\nret = ftdi.set_bitmode( ftdic, 0xff, ftdi.BITMODE_BITBANG )\nif ret &lt; 0:\n    print( 'Cannot enable bitbang' )\n    os._exit( 1 )\n\nfor i in range( 8 ):\n    val = 2**i\n    #print( 'enabling bit #%d (0x%02x)' % (i, val) )\n    ftdi.write_data( ftdic, chr(val), 1 )\n    time.sleep ( 0.5 )\n\nftdi.write_data( ftdic, chr(0x00), 1)\ntime.sleep ( 0.2 )\n\nftdi.disable_bitbang( ftdic )\nprint( '' )\n\n\n\n# close usb\nret = ftdi.usb_close( ftdic )\nif ret &lt; 0:\n    print( 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )\n    os._exit( 1 )\n\nprint ('device closed')\nftdi.free( ftdic )\n<\/pre>\n<p>This code is lightly modified from the code found <a href=\"https:\/\/github.com\/chiefdome\/libftdi\/blob\/master\/examples\/python\/complete.py\">here<\/a>, without which I&#8217;d have struggled.<\/p>\n<h4>HID USB Relay Board<\/h4>\n<p>Similar boards exist which use an Atmel microcontroller interfacing with the computer through a software HID implementation. These boards are cheaper, but otherwise similar on the relay side and in appearance.<\/p>\n<div id=\"attachment_340\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2018\/08\/USBRelayRed.jpg\"><img aria-describedby=\"caption-attachment-340\" loading=\"lazy\" class=\"wp-image-340 size-medium\" src=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2018\/08\/USBRelayRed-300x300.jpg\" alt=\"Red USB Relay Board\" width=\"300\" height=\"300\" srcset=\"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2018\/08\/USBRelayRed-300x300.jpg 300w, https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2018\/08\/USBRelayRed-150x150.jpg 150w, https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2018\/08\/USBRelayRed.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-340\" class=\"wp-caption-text\">Red USB Relay Board<\/p><\/div>\n<p>These boards can be controlled from Linux by installing the &#8220;usbrelay&#8221; program from the repository (on Ubuntu and Debian at least) using &#8220;sudo apt-get install usbrelay&#8221;. Otherwise the developer has a github page for the project here : <a href=\"https:\/\/github.com\/darrylb123\/usbrelay\">https:\/\/github.com\/darrylb123\/usbrelay<\/a>. Once installed and with the device connected, simply typing &#8220;sudo usbrelay&#8221; (without quote marks) at your command line reports back on connected devices. One of my devices calls itself &#8220;VXCMF&#8221;, while another calls itself &#8220;6QMBS&#8221;. Where a device is called &#8220;VXCMF&#8221;, the first two relays can be turned on with the following command &#8220;sudo usbrelay VXCMF_1=1 VXCMF_2=1&#8221; and off again with &#8220;sudo usbrelay VXCMF_1=0 VXCMF_2=0&#8221;.<\/p>\n<p>If you want to use the boards without sudo, you&#8217;ll have to add it to \/etc\/udev\/rules.d. There are detailed instructions on the github linked above, but in short, if you&#8217;re using a Debian family OS, put the following in a file called &#8220;50-usbrelay.rules&#8221; in the folder \/etc\/udev\/rules.d :<\/p>\n\n\n<pre class=\"wp-block-code\"><code>SUBSYSTEM==\"usb\", ATTR{idVendor}==\"16c0\",ATTR{idProduct}==\"05df\", MODE=\"0666\"\nKERNEL==\"hidraw*\",  ATTRS{idVendor}==\"16c0\", ATTRS{idProduct}==\"05df\", MODE=\"0660\", GROUP=\"plugdev\", TAG+=\"systemd\" ENV{SYSTEMD_WANTS}=\"usbrelayd.service\"\nKERNEL==\"hidraw*\",  ATTRS{idVendor}==\"0519\", ATTRS{idProduct}==\"2018\", MODE=\"0660\", GROUP=\"plugdev\", SYMLINK+=\"usbrelay%b\"\n\nSUBSYSTEM==\"usb\", ATTR{idVendor}==\"5131\",ATTR{idProduct}==\"2007\", MODE=\"0666\"\nKERNEL==\"hidraw*\",  ATTRS{idVendor}==\"5131\", ATTRS{idProduct}==\"2007\", MODE=\"0660\", GROUP=\"plugdev\", SYMLINK+=\"usbrelay%b\"<\/code><\/pre>\n\n\n<p>Then make sure your user is in the plugdev group and restart. If you&#8217;re using Windows or macOS, there are other instructions in forks such as this one <a href=\"https:\/\/github.com\/corerd\/usbrelay\">https:\/\/github.com\/corerd\/usbrelay<\/a>, although it looks a little old.<\/p>\n<p>Both boards detailed in this post are available with various numbers of relays. The red boards are probably easier to use as it is simply a case of installing usbrelay. Hopefully the instructions here and in my previous post can help people get their boards running. I haven&#8217;t tested, but both should work fine with a Raspberry Pi and many other SBCs.<\/p>\n<p>Good luck!<\/p>","protected":false},"excerpt":{"rendered":"<p>FTDI USB Relay Board A while back I wrote a post about using an FTDI breakout board as a GPIO board from Python. Since then, I have discovered that there are USB relay boards based on this concept available from China. SainSmart seem to sell them, but they are available on ebay, Banggood and Aliexpress. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":283,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[13,42,12],"tags":[66,67,62,59,105,47,65,102,104,103],"jetpack_featured_media_url":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/wp-content\/uploads\/2018\/01\/IMG_20180114_140752.jpg","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7g9vY-5s","_links":{"self":[{"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/posts\/338"}],"collection":[{"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=338"}],"version-history":[{"count":6,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/posts\/338\/revisions"}],"predecessor-version":[{"id":2729,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/posts\/338\/revisions\/2729"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=\/wp\/v2\/media\/283"}],"wp:attachment":[{"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elephantandchicken.co.uk\/stuffandnonsense\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}