Controlling voltage supply on usb port using c or any programming language

I don't have much konwledge in electronics. i wanted to turn on/off led connected to usb port using program. usb port consists 4 line (data+,data-,voltage ,ground) if i connect voltage and ground lines to led , it will glow. i've been searching on internet this issue and i find that it is not possible to control led connected to usb using program because supply over datapin is too low which can not be used to tun on off led.

but what if i connect my led to vol and ground pin and control the entire power supply of usb port , i guess it is possible to control led using program. there is a way to disable and enable power supply over usb port.

i also want to know is it bad to follow this approach. does it damage usb controller to frequently enable/disable power supply.

Algo :

      a = Get_input_from_user() ;
      if(a=="ON")
           turn power supply of usb port ON.
          else
           turn power supply of usb port OFF.

this thing is certainly possible using extra hardwares, i don't want to use any extra hardware.


The power for a computer's USB ports is generally not software controllable. In most cases, the power pins of your USB ports are wired directly to the 5V rail of your power supply (usually through a polyfuse), so there is no way to switch them on and off.

Some powered USB hubs do support switching power to their ports, but you said that you didn't want to use any extra hardware, so you're out of luck.


you can use any basic Arduino board to do this there is a complete IDE free and a LOT of sample code that will do what you want out of the box

Arduino的在这里输入图像描述

you can also use a COOL board.

Teensy 3.0

Arduino Compatible code is available for the Teensy

Software Development Tools HERE


You can use an external power supply ( the Vcc and GND pins of the USB port in this case ) with a resistor to power the diode and connect a transistor in the middle working as interruptor. Then, yo connect the base and emisor to de data+ and data- of the USB port.

this page doesnt let me upload images.

Sorry for the quality of the drawing but all I have on the computer is paint. For a more stable performance make sure to make R1 low enough so that transistor is on saturation mode when data pins are ON but not so low that transistor gets burnt (I dont really know what is the voltage level of the data pins on the USB port, sorry). To choose a proper value of R2, you need to know the current you need to power your diode. It's calculated acording to Ohm law:

Idiode = (Vcc - Vce,sat - Vd) / R2.

Where Vce,sat is potencial diference between colector and emisor when transistor is on saturation mode (it is found on the transistor datasheet on the manufacturer webpage, usually around 0.2V) and Vd is the normalized potencial difference of the diode (the same, but this time around 0.6V).

Note that data- and GND pins are connected. I am 99% sure that this won't harm your USB port, but use it at your own risk.

I would test it out using another external power source first anyways, just to make sure you connected everything properly.

链接地址: http://www.djcxy.com/p/81298.html

上一篇: COM事件通知

下一篇: 使用c或任何编程语言控制USB端口的电压供应