Program LED with just USB port - c#

I want to control LED with C# using only USB port. I don't want to attach any other device with USB. I just want to attach LED directly to USB port pins and program it (blink etc). How can i do that? I am new to hardware programming and it will be my first program.
EDIT: I can do it already (blink LED) using printer port by attaching one led pin to data pin and other to ground. How can i do the same with USB port? My motherboard does not have a printer port. USB is the only option.

This is not possible.
4 usb pins
| | | |
| | | |
1 2 3 4
1: +5V
2: D-
3: D+
4: Ground
To trun on LED connect with 1 and 4
Current on Data pins is too low therefore LED will never turn on if connected with 5V and any Data pin. Hence LED can not be controlled directly using data pins on USB port

Many mainboards will permanently supply USB ports with current.
You could theoretically put the USB controller into some suspend mode and back again but this is bad.
You'd need some sort of controller to accept commands over USB then some discrete elements to open/close current from the permanent USB supply and let it reach your LED.

You really can't do this. You can do it with a parallel port because you have individual control over the data pins. With USB, you only have control over the data the rides on top of the USB protocol. It's likely that protocol is being run in firmware by a chip on the motherboard anyway, so there's most likely no way to do what you want to do, short connecting the LED across the power pins and toggling the USB bus power, which you should NOT do for a whole variety of reasons.
Just get an Arduino or something. In the long run it will be a lot easier and be more flexible.

I can do it already (blink LED) using printer port...
OK, so why not buy a cheap USB based printer port and use that? Since you already have the LPT port code working, this gives you a transparent solution.

You could use a PIC microcontroller . The 18f series can directly interface led's over the usb . It is also possible to code using c# and use only the USB port to control the led . I'm guessing you actually want to control the led and not just get it to glow .

Related

How to control my USB port using C# [duplicate]

This question already has answers here:
Power off an USB device in software on Windows
(5 answers)
Closed 9 years ago.
What I want to write is an application that would be able to access my USB port.
To control when to charge my phone and when not to. And perhaps with time I would like to extend the application to control light bulbs, fans and the television in my room.
I want to learn how to work this out rather than just copy some line of codes and paste. I wish for a quick and very simplified tutorial on this, I would be highly grateful (small language please, You all know am still learning :-)).
The exact job I want to achieve is just to make the port send out power when needed. That is it should be switched off and on using codes if possible.
It isn't possible to change the power status of a USB port from User mode code. It's not a .NET limitation, no user application can change the power status of a USB port. Actually, I think that the power state is controlled by the chipset itself and you can't turn it off without using a specialized chipset.
Besides, if your device short-circuited, you could burn your chipset and your motherboard.
The only (safe) solution is to use an external device that will control a relay to turn power on or off for your devices.
There are multiple answers to similar questions, eg Windows- Power off a USB device in Software
A good option is to use an external device like Arduino (or Netduino), .NET Gadgeteer or any number of external boards. Prices and capabilities vary of course.
If you want to build the device yourself, you can find numerous USB relays with a simple Google search
EDIT
Regarding serial ports, there is almost no relation at all between the two port types. Serial ports are not designed to provide power. You can use tricks to use a pin's voltage to power some external device, although this will lead to problems because there is not enough current to power most devices. Burning the chipset is another likely outcome.
A USB port on the other hand, is explicitly designed to provide power through specific wires which can't be controlled normally. This is why you can charge your phone from a desktop's USB port even if the machine has shut down.
EDIT 2: Sleep-and-charge ports
As Ben Voigt noted, not all USB ports allow charging while shutdown.
There are multiple port types that support charging. Charging ports provide more current than plain ports but shut down when the computer is switched off. Sleep-and-charge ports on the other hand can provide power even when the computer is off.

Turn off power to USB Port programmatically

I have a project that I'm working on and I want to use these:
http://www.woot.com/blog/post/usb-powered-woot-off-lights-2
However it looks like they just have on/off switches. They aren't something that you can interface with programmatically.
So I was thinking, if I could find a way to cut the power to the USB port, that would allow me to turn the lights on and off with my app. However I cant find any way to cut the power to the USB port. Is this possible?
I had a similar problem and solved it using DevManView.exe (freeware):
Download DevManView.exe and put the .exe file somewhere:
http://www.nirsoft.net/utils/device_manager_view.html
Go to your Device Manager and figure out which USB-Controller you have to enable/disable, so that your device does/doesn't get power anymore. For me disabling "USB Serial Converter" does cut the power of all USB slots.
USB Serial Converter in device manager
In C#, create and start a new process for disabling the device (using the name of the USB-Controller).
Process devManViewProc = new Process();
devManViewProc.StartInfo.FileName = #"<path to DevManView.exe>\DevManView.exe";
devManViewProc.StartInfo.Arguments = "/disable \"USB Serial Converter\"";
devManViewProc.Start();
devManViewProc.WaitForExit();
And enable it again.
devManViewProc.StartInfo.Arguments = "/enable \"USB Serial Converter\"";
devManViewProc.Start();
devManViewProc.WaitForExit();
EDIT: unfortunately it seems the products from this company are not sold anymore.
The powered-woot-off-lights-2 are powered via a regular usb plug which mean they are 5 volts and there are in each one light and one motor.
I could not find how many Amps are needs but I know that this extension would be perfect if 200 mA for one powered-woot-off-lights is enough.
USB Controler Extension
The C# or VB.NET code would look like this.
// On
nusbio.GPIOS[NusbioGpio.Gpio0].DigitalWrite(true);
nusbio.GPIOS[NusbioGpio.Gpio1].DigitalWrite(true);
// Off
nusbio.GPIOS[NusbioGpio.Gpio0].DigitalWrite(false);
nusbio.GPIOS[NusbioGpio.Gpio1].DigitalWrite(false);
see Nusbio's website and I think the extension is in their store.
I used a similar nusbio extension myself.
to turn on/off my a USB fan and a battery powered small lamp
If you have admin privileges, then run the following:
Microsoft.Win32.Registry.SetValue(#"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 4, Microsoft.Win32.RegistryValueKind.DWord);

Alt-Tab pedal with an Arduino

I'm creating an Alt-Tab pedal with an Arduino. The idea is that the user will just press down on a pedal that will hit a button on the Arduino, and this would be the equivalent of hitting the ALT+TAB key (thereby allowing you to stealthily switch windows).
I'm also open to just minimizing the window.
The problem is, I'm not really sure how to go about this. Should I use a command that runs a batch file? Is there a command in C I can implement this with? I'm open to any suggestions.
SendKeys.Send("%{TAB}");
Or use SendInput() in a C program.
Based on the other answers, it sounds like you’re proposing to have the Arduino connected to the computer with USB and send a letter when the pedal is pushed, then have a program on the computer side receive that message and invoke Alt-Tab.
There’s a different way to do this: Teensy 2.0 is an Arduino variant based on the ATmega32U4 microcontroller. This is an AVR chip that has built-in USB support, instead of a serial port that’s translated to USB with a USB-serial bridge as you’ll find on an Arduino.
One of the features is that it can actually emulate a USB keyboard. You can plug it into a computer and have it send Alt-Tab on a pedal press without writing any software on the computer side, and it will work anywhere.
See this tutorial: http://blog.makezine.com/archive/2011/04/the-awesome-button.html
Basically, it uses an "Easy Button" (you'd use the pedal) to enter a pre-set keyboard character or characters (Alt-Tab in your case) through a USB port. It should have everything you need to figure out your project.
The Teensy++ is smaller and cheaper than an Arduino and can be hidden in your pedal box, tidy as can be. A program to do what you desire would take less time than installing the Teensyduino software into the Arduino environment. I think it is the bomb, particularly for keyboard and MIDI stuff (you could make a pedal for "Start Song" or the all important "All Notes Off").
http://pjrc.com/store/teensy.html
http://pjrc.com/teensy/td_keyboard.html
There are several recipes in Arduino Cookbook related to using an Arduino to emulate a mouse and/or keyboard.

How to differentiate between USB device types?

Having read up on this question regarding working with USB devices, I got to wondering what method is being used by the operating system to differentiate between types of USB device?
For example, I can plug in a USB mouse and the computer "just knows" it's a mouse. If I plug in my 1GB USB drive it "just knows" it's 1GB's worth of removable storage.
What sort of code is being used by the device to 'announce' what type it is? Or is the operating system polling the device and getting some standardised response?
How would I program a device to identify itself as, say, a keyboard?
In order to build a device that works over USB, you will almost certainly have to read the USB specification available from http://www.usb.org/developers/docs/
In regards to your specific question, there are GET methods associated with various standard USB variables. These are stored in the firmware of the device and provided when requested. Eg, GET_DESCRIPTOR, GET_STATUS etc are request types that can be sent to the device.
USB has its own set of protocols that would be quite a task to implement in their entirety, so most device manufacturers buy a USB controller chip from some third party.
If you're just interested in hacking something together, then I think the specification will be enough to get you sending a few packets.
The only other option I can think of may not be suitable for your project; wire-tap a real keyboard, let the keyboard to all the complicated address resolution and device identification etc, then just have your device push data onto the wires as and when.
This is something I have managed before when I build a keyboard black-box for password sniffing at high-school, the black-box stored keypresses then regurgitated them onto the wires as if it were a keyboard. Once it had stored a few passwords, and I retrieved it, I had to plug it in attached to the keyboard, then unplug the keyboard before setting it to output the keypresses (with a handy gedit window open :P) otherwise the keyboard would interrupt the transmission with its own data.
The type (or rather, the device class) is determined by reading the device's class from the device's device and interface descriptors. You may want to read this segment about USB device descriptors from USB in a Nutshell for information. (I also recommend reading the whole thing, if you're interested)
In Device Manager open the properties of a device and select the Details tab. The various values give the information (or rather is generated from the information from the device and its driver).

Use USB to activate MOSFET/Relay

I am working on a personal project involving sending simple signals from my computer to a circuit via USB. Basically I am using the USB signal as the gate signal for a MOSFET which will in turn activate a relay to turn on/off various AC peripherals. For example if I want to turn on a light bulb for 5 seconds every minute I would be sending a 1 down the first wire for the first 5 seconds of every minute.
This is my problem: I have no idea how to manually send a 0/1 down a specific wire on a USB cable, or even interact with a USB port at all :(
So I guess there are multiple parts to this question, is it possible to interact directly with the bits being sent via a USB port? If so how would I do this? I am familiar with C++ and C#, so I really hope that you can do it in one of those...
Thanks!
edit Hmm so it looks like the USB port actually only has one 5V pin so direct USB interaction wont work. Going to take a look at a parallel adapter and get back on it.
USB is a bad fit for anything that doesn't have a USB interface at the other end of the wire. If you don't want to get into building your own USB device, I'd suggest buying a USB to serial adapter, which gives you two directly-controllable output lines (the flow control lines), or a USB to parallel adapter, which gives you more than 8 lines.
Chris Johnson's answer has a link to instructions for Windows serial port programming. You'll want to look at section 7 -- the SETDTR, CLRDTR, SETRTS, and CLRRTS are your flow control line toggles (for the DTR and RTS lines, respectively).
As far as hardware goes, a "1" (SET) value on a flow control line is +3 to +15 volts on the line, and a "0" is -3 to -15. Actual voltages can vary between devices; measure it to be sure. (EDITED; I got the 1 and 0 mixed up. The control lines use the opposite convention from the data lines.)
Here are Wikipedia pages for voltage characteristics and pinouts.
EDIT: Having done some more research on USB-to-parallel adapters, I don't think they will give the needed level of control. For best results, you'll need a PCI or PCMCIA parallel card, or a parallel port built into the motherboard.
I'm not a Windows programmer, but this library might be useful for controlling the parallel port's lines from Windows.
The easiest thing to do for this application is to use serial port emulation, either with a USB-Serial cable, or with a USB-Serial converter chip (e.g. the FTDI FT232) in your hardware device.
Either way, this allows you to interact with your USB device as you would a serial port (see, e.g. here for how to do this in C++ in Windows)
Much like Chris suggested, you can get a USB slave device from FTDI. $27 at digikey will get you a small board with all the fine wiring already done.
However, for your purposes the bit-banging mode described on page 39 of the datasheet would be much better than the UART mode (UARTs generate pulses at several kilohertz, you want to have the voltage stay at the level you set it to).
Hopefully your MOSFET will turn on with a 3V signal since the FTDI will put out approximately 3.1-3.2V for a high output.
Also, make sure you use the latest drivers from FTDI... a couple years ago they had drivers (WHQL-certified even) that caused frequent BSOD, and I've often found that driver CDs that come with hardware are several years out of date.
I built something very similar to what you are doing (I was running a car window motor from a usb device, used a mossfet H bridge (the HDR1X8 on the diagram) to drive the motor.). You need a USB to I/O device this is what I used (I got a U421, they fit perfectly over the center line of a breadboard, the 401 works well with breadboards too if you don't have a split one.
They give you a dll and you just link in to it with your code. its just as simple as making a call to WriteA and WriteB for writing out to your mosfet device. Be warned logic level lines are not meant to drive current so you will need to hook this up to a transistor if you expect any kind of medium to large current flow. See App1 in the application notes of the menu on the usbmicro site to see the proper way to hook it up.
Use a USB prototype board. It usually comes with a software SDK.
Check out these links from my web site:
1.You can use a usb to serial converter. All you need to know is here
2.You can use programming to access the usb directly but for this one you have to have a driver already installed for you device. Details are here

Categories