Sainsmart 5V USB Relay Board C# Source code

Hopefully this may be useful to somebody…

A while back I purchased this relay board from Sainsmart, although the software provided does work the source code is not given. As I wanted to add the control into my own application this was not enough, slight false advertising but I wrote a program to demonstrate how they work anyhow.

The commands that the company sent to me were wrong, in two cases, they simply did not work. I did supply this code to them and they seemed to be thankful and asked if it was OK to put on their site, it has not yet materialised so for now I will leave it on here.

Any comments or questions just let me know, link below for the example project. (C#, VS2012).

Sainsmart Relay demo

22 responses to “Sainsmart 5V USB Relay Board C# Source code

  1. Excuse me, I have an Sain Smart 4xUSB relay 5V, but the leds never turned on, I am so frustrated with it, I’m using C# and the app pre-compilated, and no one case the leds turned on.

    For avoid any fail, I need to know how is the right connection, USB, AC, etc, may yo help me sending an diagram connection please.

    Please help me

  2. Hello,

    Out of the box the board will not be powered by USB.

    If you want to power by USB then you need to put a shorting link on the board, by putting the link in you allow the 5V to come through from the USB. Once you have powered the board you should hear the relays click ON/OFF quickly before staying off. I don’t have the board to hand but I’ll take a photo on Monday, give it a try if you get what I mean with the shorting link.

  3. Thanks for this! It worked beautifully with the 12V 4-port board. I’m using it in a pinball machine simulator to control some physical toys (fan and shaker).

  4. Thanks a lot for this, I really spent a long time looking around for documentation about the board and didn’t get the relation between the relays and the FTDI chip before reading your code !

  5. Thanks for the code! Is the code easily extendable to accommodate the 8 channel relay? I was wondering if I could just modify the RelaySwitch method in the relayBoard.cs file to handle the additional four channels? Thanks again!

  6. Hi Chris, thanks. I had a look at the 8 channel board on their site, they use the same FTDI chip… so you should just be able to add the extra enums and bit patterns. So relay 5 would be 0x10, 6 – 0x20, 7 – 0x40, 8 – 0x80.

    Don’t see why that shouldn’t work, but I don’t have an 8 channel board. Let me know how you get on 🙂

  7. Hey,

    thx for the app. I have the 8 channel board, first off, needless to say with your app you can control the first 4 relays without changing anything, and to extend it to 8 channel is also very easy, as described by you. I changed it in c#, with my close to none programming skills, so thx!

    • Just having a quick look it appears as though there are drivers available for Linux, you could use the source from my project as a reference. My project is for windows only however.

  8. hello!

    For some reason your link for the c# code is broken, any chance I could get it somehow? Trying to build a sample project this weekend with this. Thank you for the post.

  9. Worked right away! I did put a jumper on board to power it by USB. In the version i have (purchased 6/1/2017 board labled k138402BSJH 1982-USB4CH), the jumper is labeld R10. It consists of two thru holes to solder the jumper into, labeled VCC and 5V. Solder a wire into these two holes, and it works without putting power into the VCC terminal.

  10. Thanks! THis is awesome code, very cleanly written. Works great, but i did have to add one mod: if there are more than one device on the com ports, it doesn’t find it. The connect member assumes there is only one. So i added a loop. Now it works great.

    I downloaded the rar folder. I think the drivers for the ftdi insalled automatically. Not sure, becuase i did go and download them myself, and clicked the autoinstaller “Autoinstaller_WHQL_CDM2.12.12_Win 2K, XP, Vista, 7, 8, 8.1, 10, 32_64bit” once downloaded. THis link is provided in the Relay class: http://www.ftdichip.com/Drivers/D2XX.htm

    Here’s the connect method in its entirety. THe modified section is the while loop.

    public bool connect()
    {
    // Determine the number of FTDI devices connected to the machine
    ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
    // Check status
    if (ftStatus == FTDI.FT_STATUS.FT_OK)
    {
    //Console.WriteLine(“Number of FTDI devices: ” + ftdiDeviceCount.ToString());
    //Console.WriteLine(“”);
    }
    else
    {
    //Console.WriteLine(“Failed to get number of devices (error ” + ftStatus.ToString() + “)”);
    return false;
    }

    if (ftdiDeviceCount == 0)
    {
    //Console.WriteLine(“Relay board not found, please try again”);
    return false;
    }
    else
    {
    // Allocate storage for device info list
    FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];

    // Populate our device list
    ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);

    //Search Device List
    // Open first device in our list by serial number
    int i = 0;
    ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[i].SerialNumber);
    //If that wasn’t the device, search the rest of the list.
    while (ftStatus != FTDI.FT_STATUS.FT_OK & i++<ftdiDeviceCount)
    {
    ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[i].SerialNumber);
    }

    if (ftStatus != FTDI.FT_STATUS.FT_OK)
    {
    // Console.WriteLine("Error connecting to relay board");
    return false;
    }

    // Set Baud rate to 9600
    ftStatus = myFtdiDevice.SetBaudRate(9600);

    // Set FT245RL to synchronous bit-bang mode, used on sainsmart relay board
    myFtdiDevice.SetBitMode(0xFF, FTD2XX_NET.FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG);
    // Switch off all the relays
    myFtdiDevice.Write(startup, 1, ref bytesToSend);

    return true;
    }
    }//END connect

    • Modified: it seams that other devices can be openned with this code. So in debug i read the serial numbers in the list. In Visual Studio i stopped the code and looked at the ftdiDeviceList. By plugging in only the relay board, i can find the serial number. Then i hard coded that number in. I’m sure there is a better solution, but i don’t know much about this and how to detect a kind of device compared to others. This gets me through the night:

      int i = 0;
      ftStatus = FTDI.FT_STATUS.FT_DEVICE_NOT_OPENED;
      while (ftStatus != FTDI.FT_STATUS.FT_OK & i++<ftdiDeviceCount)
      {
      //check serial number
      if (ftdiDeviceList[i].SerialNumber =="AI050MXB")
      {
      ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[i].SerialNumber);
      }

      }

Leave a comment