0

I want to write code in C language to switch on/off an electric bulb via USB port. USB port gives 5 volts, so I want that bulb can take power from computer's USB port.

Is it possible?

Anthon
  • 69,918
  • 32
  • 186
  • 246
kapil
  • 505
  • 4
  • 18

2 Answers2

2

Using USB for this task is a bit complicated, the common way is to use some (ftdi) usb to serial converter. To switch a light bulb or motor you need a micro controller on the other side of the usb cable.

In fact, an arduino-pcb has an build in usb to serial converter, so you can use an arduino to switch something on/off by using an usb port.

How to read/write a serial port depends on your operating system, but there a lot of examples for each operating system

flotto
  • 535
  • 5
  • 17
  • How does USB to serial help? Just get a USB microcontroller. The chip in the serial converter is already more powerful than what you actually need. – Potatoswatter Sep 03 '15 at 01:34
  • @Potatoswatter A USB microcontroller does actually contain a USB to serial converter. USB to serial helps, because it is much less complicated then USB. Using a USB-to-Serial bridge, you don't have to write a device driver, you only have to choose the correct com port. I wrote that he should use a microcontroller to switch the light bulb and USB-to-serial to communicate with the microcontroller, not use the usb-to-serial to switch the light bulp. I have tried to give a abstract solution first, followed by an practical example. (A Arduino is a very user friendly usb microcontroller). – flotto Sep 04 '15 at 08:39
  • No, a USB microcontroller contains USB endpoint hardware. Given a microcontroller with USB *and* RS232, you can implement a converter. But why on earth would you need RS232 to control a light? By definition that's adding complexity, regardless whether RS232 is simpler. You've turned a one-microcontroller problem into a two microcontroller problem. Including the host PC, now there's a network running three different programs… – Potatoswatter Sep 04 '15 at 09:56
  • @Potatoswatter where did i mention a second microcontroller or three different programs? i can't find that part in my comment. For clarification: i talked about a microcontroller with usb debug port (e.g arduino) and a pc host. the microcontroller receives commands(switch on/off) from the pc host via serial connection (uart). The usb microcontroller usually contains some kind of USB-to-Serial Bridge on its PCB or embedded in the controller. Look in the Arduino-Spec: https://www.arduino.cc/en/Main/ArduinoBoardLeonardo. https://www.arduino.cc/en/pmwiki.php?n=Reference/Serial – flotto Sep 04 '15 at 11:43
  • The PC runs a program. The USB-serial converter contains a microcontroller running a program. The serial microcontroller runs a program. That makes three. – Potatoswatter Sep 04 '15 at 13:49
  • @Potatoswatter I don't know how exacatly a usb-to-serial bridge works, but i gues the logic handled by hardware and the driver software. Actually you don't have to care about this part. And as i mentioned, a microcontroller with usb interface does contain a usb-to-serial bridge. Uart is the common way to interface a microcontroller from a pc host. The question was how to switch a light bulp using usb, i supposed that means using a pc host. Using a single microcontroller to switch a light blub is overengineerd, because you could simple use a switch and a voltage source. Sorry, i dont – flotto Sep 04 '15 at 19:42
  • @Flotto - you are misusing terminology. A microcontroller is a single IC, what you are referring to is an entire PCB like an Arduino containing several ICs of distinct function. Using one IC to convert from USB to serial and then a second to interpret serial data and switch I/O lines is a bit roundabout and hardly cost effective when there are microcontroller ICs with built in USB (in fact, the modern Arduino uses one of these just for its USB serial conversion). The exception would be the one-off/hobbyist case where using something easily purchased like an Arduino may be simplest. – Chris Stratton Dec 31 '16 at 09:21
0

It seems that there's no easy way to directly switch the power of USB ports. At least, on a normal PC. So you will need some kind of relay.

ddbug
  • 1,392
  • 1
  • 11
  • 25