C# controlled lights via USB port

At work, we thought it would be cool if we could turn on siren lights when a server goes down.

Step #1 to make this happen was to be able to control simple electronics from custom code. I love C# and the USB port is what is common now so I researched and found the FT245RL “USB to FIFO” chip. This chip made it really easy for me. It plays well with drivers known by Windows.

I bought the FT245RL from SparkFun on a breakout circuit for ease of soldering. For details about chip, take a look at this DataSheet.

When I plugged in the chip to the USB port, Windows 7 immediately recognized it and installed the drivers for it. You can use a couple of drivers the default is the simpler one to use where it turns the USB port into a virtual serial (COM) port.

Writing to serial ports is really standard so this is the code that outputs a byte through the chip:

private static void WriteByte(byte byteToWrite)
    using (SerialPort vcp = new SerialPort())
        vcp.BaudRate = 9600;
        vcp.DataBits = 8;
        vcp.StopBits = StopBits.One;
        vcp.Parity = Parity.None;
        vcp.PortName = "COM5";


        vcp.Write(new byte[] { byteToWrite }, 0, 1);




8 thoughts on “C# controlled lights via USB port

  1. do you have any code for how i could do this using a PIC ? .. I have posed this question on two stack exchange sites ..
    > http://stackoverflow.com/questions/11372112/how-to-control-an-led-using-c-sharp-over-the-usb/11372136#11372136

    > http://electronics.stackexchange.com/questions/35184/c-c-c-programming-for-interfacing-usb-to-pic-microcontroller#comment70017_35184

    so , far your post is the most helpful thing i’ve found but I’m still not any closer to finding a way to do this .. would you be able to help me out ? .. my email address is [kasisnu@gmail.com] ..

    • Hi Kasisnu,

      I don’t have the code to use a PIC. I’m not sure if there’s a translator between C# and the PIC assembly language. I know there is one for Java but I haven’t dealt with PIC stuff for over 10 years.

      But if you really just want a byte of outputs from the usb port the PIC is an overkill. The PIC would make sense if you want the chip to do some thinking. On my example above all the thinking is done by the computer controlling the USB port.

    • Hi, my code is really just what you see here with System.Threading.Sleep(3000) in the middle of each WriteByte call.

      I think the problem is in the wiring of the circuit. Have you looked at the FT245RL data sheet?

      • Hi, I’m using a 4-Port controller similar to yours (this one https://www.sparkfun.com/products/9669) via USB.

        I wish to use an existing C# application to trigger one of the relays on and so forth.

        My question is…How would I go about doing this? I don’t know how to connect the relay to C# in visual studio.

        Of course, there’s hyperterminal where I CAN control basic commands. Please help me understand how on earth I could connect the controller to C#.

        Thank you my friend!

  2. seth257Seth: It sounds like you can use the WriteByte function described on the post. Note that the port name will probably be diferent on your computer. To get this port name, just plug in your device and use the name that shows up on “you just plugged in a device” windows notification baloon.

    Or open the device manager and find the device that shows up and disappears when you plug and unplug the USB cord.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s