If you'd like a more traditional npm package to use in your nodejs projects, check out this repo
Hi. I recently purchased a Sainsmart 16-Channel USB Relay Board (the "Relay Board" from here on out). It wasn't until it was delivered and I went to try it out with a Rasperry Pi that I noticed that documentation for this product was rather sparse/nonexistent. After many failed attempts and much frustration, I was finally able to get it to work. Since I couldn't find any direct guides for how to do so, I figured I'd throw one together. So here we go.
If you wanna skip all the nonsense, just do the following:
sudo apt-get update
,sudo apt-get upgrade
- Download the driver -
sudo wget https://github.com/aperepel/raspberrypi-ch340-driver/releases/download/4.4.11-v7/ch34x.ko
- Update your pi -
sudo rpi-update
- Reboot your pi -
sudo reboot
- Check to make sure ch341.ko is installed -
ls /lib/modules/$(uname -r)/kernel/drivers/usb/serial
($(uname -r) should evaluate to something like "4.14.58-v7+") - Plug the Relay Board into your Pi
- Check to make sure ch341 (and usbserial) process is running -
lsmod
- Check to make sure the Relay Board has been recognized through USB -
ls /dev/tty*
(Look for 'ttyUSB0') - Add this repo -
npm install sainsmart-16-channel-usb-relay-ch341
(I'll get around to adding dependencies (just serialport) and all that) node 16ch_usb.js <relay # (1-16)> <state (on, off)>
to flip individual relays ornode 16ch_usb.js reset
to turn them all off
I had used the GPIO-controlled 16-channel board from Sainsmart in a few projects before. It's pretty well-made and reliable, but wiring it is such a pain and you can only really control about 26 relays or so with the GPIO pins of a Raspi Model B. This is normally more than enough, but I needed a bit more capacity to avoid using multiple Pi's.
So I was very excited when I noticed Sainsmart had added a USB-controlled relay board. Theoretically, it meant that I would be able to control 64 individual relays with one pi (4 USB slots x 16 relays each). So I ordered a few.
Sainsmart provided some materials on commands, and I found a github repo for a raspi ch340/341 driver, which allowed me to communicate commands to the Relay Board, eventually resulting in the helper file you see above.
After searching some forums to see if anyone had used these in a project before and was kind enough to include sample code, and failing to find anything, I emailed Sainsmart support. They provided this link: 16-Channel wiki
Out of two .exe files and a couple of diagrams, the only information that was actually useful was the command breakdown for turning relays on and off through USB serial commands. I plugged in the relay to my Pi again to see if I could figure out what drivers it might need. lsusb
showed a list of usb devices and the relay board showed up with this info: QinHeng Electronics HL-340 USB-Serial adapter
The "HL-340" part ended up pointing me to a ch340/341 driver and this repo. Getting that .ko file to install on my Pi was frustrating - the insmod
command was throwing an error, which was documented in the repo's issues tab. I read through the issues and eventually got to the following steps:
sudo apt-get update
,sudo apt-get upgrade
- Download the driver -
sudo wget https://github.com/aperepel/raspberrypi-ch340-driver/releases/download/4.4.11-v7/ch34x.ko
- Update your pi -
sudo rpi-update
- Reboot your pi -
sudo reboot
- Check to make sure ch341.ko is installed -
ls /lib/modules/$(uname -r)/kernel/drivers/usb/serial
($(uname -r) should evaluate to something like "4.14.58-v7+") - Plug the Relay Board into your Pi
- Check to make sure ch341 (and usbserial) process is running -
lsmod
- Check to make sure the Relay Board has been recognized through USB -
ls /dev/tty*
(Look for 'ttyUSB0') - Use the serialport library to interact with that port (i.e.
const port = new SerialPort('/dev/ttyUSB0')
) and/or use my code above to test your connection through the command line