USB – Sending Signals to USB Port Using C

cusb

I want to send a simple 5V signal to the usb port. The USB cabel's insulation will be removed and connected to a relay which is connected to an electric door system. I think I should write this code in C. Can somebody give me a hint?

Best Answer

You cannot do what you want with just connecting a relay to the USB port.

The USB is a bus, with a serial protocol running on it. It is necessary for you to interface with that bus using the proper protocols.

You will require a use "device" which is capable of being programmed to respond to a computer-based stimulus, and activate an external signal.

There are many ways of achieving this, from ready-made products, through DIY kits, and al the way to discrete ICs that you can use to build your own system.

Personally I would recommend one of the many DIY kits available, such as the Velleman K8055 USB Experiment Interface Board. There are many others like it.

If you want to get in to the nitty gritty, then you need to be looking at such things as Microcontrollers (which the Velleman is based on), which can be programmed to do many different things. Some of these have built-in USB support, but it takes quite a lot of in-depth knowledge of the USB protocol to get to grips with these. Most people use an "FTDI" chip to convert the USB into RS-232 first.

If you are wanting an off-the-shelf product then you may want to look at the possibility of a USB Parallel Printer Port product, that you can program and access as if it were a parallel (Centronics) port directly attached to your computer. This would give you 8 outputs you can turn on and off.

One more thing to watch out for though - most computer / microcontroller outputs won't be powerful enough to directly drive a relay. You will need to feed the signal through some "driver" circuit to achieve enough power. This may be as simple as a single transistor, or you may be looking at something more complex such as a MOSFET, etc. Oh, and beware of "Back EMF" - a relay is an "Inductive Load".