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?
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".
USB is not as simple as you seem to think it is. USB has a communication protocol that doesn't allow you to simply output high or low.
Probably the simplest way to do this is to use a microcontroller connected over usb using a serial to USB method. You can use an FTDI chip to do this. After connecting to the FTDI chip the computer will install a virtual serial port. You can then send data to this serial port and the microcontroller will receive it over UART. The microcontroller can then read in the packets you send and you can have 1 packet for 'on' and another for 'off'. The microcontroller would just turn a pin high or low based off of the last packet it received.
Another option to consider would be to use the serial port directly. Many computers give you control over the clear to send type pins. If you can get this ability then it would just be a matter of having a program that toggled the clear to send state.
USB is a sophisticated serial bus which allows communication between a host (your PC) and up to 127 devices (all kinds of products you connect to your PC). So it appears that you don't have direct access to the bus, anyway that you can't simply switch on and off things with it. But there is a number of solutions to this.
PCs come with ever less EIA232 (often referred to with its old name RS232) ports, but sometimes you need one. A few companies specialized in interfaces, so-called bridges, which convert the USB bus to EIA232. FTDI is a well-known manufacturer of those bridges, and in other answers it's been suggested to use EIA232 control signals to control a relay. This may work, but in fact it's improper use of EIA232.
Use one of the I/Os to switch a relay via a transistor. (The I/Os can't deliver the required current to drive the relay directly.)
See also this answer to a related question.
As others have pointed out, USB is a bus, so you need to use a device on the other end to translate bus commands into physical actions. The easiest way to do that is to use a number of existing products designed for this without needing to write code for the embedded end - for example, Phidgets have a whole range of devices - this relay board is probably what you want.
Directly switching 5v would only be possible if you had low-level programming information and access rights for the USB PHY or hub chip, ie, the ability to enable/disable the 5v VBUS (and to do so specifically for a particular port, if you need to keep other devices such as a usb keyboard working).
There are definitely systems on which this would be possible (the lone USB port of a tablet I was playing with recently, for example), but it's a very implementation-specific and non-portable hack.
Most of the time, the suggestions everyone else is giving you to use a USB-I/O chip such as an FT245 or USB-enabled micro-controller or even ready-to-go USB-relay board are preferable, since they work via drivers that rely on the standard USB protocol and do not depend on intimate knowledge of particular chips used to implement the host interface or hub.
EDIT: I'm trying to recall, there may actually be standard commands for enabling/disabling VBUS of specific downstream ports on a hub, but in practice few hubs actually have the necessary power switching device to implement that. Finding one that does may be no easier/cheaper than buying the USB-relay board.
protected by Dave Tweed♦ Jul 25 '14 at 21:54
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?