Use USB to activate MOSFET/Relay - c#

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

Related

USB-Serial Port stops working without any reason

I use as USB-SerialPort Converter for a Barcodereader. (Drivers for the converter are supplied on a CD -- company: digitus)
In my program (C#) I wait for SerialPortDataReceived-Events and proceed if I get values -- so far everything works fine.
The problem now is that I don't receive Data anymore sometimes.
The same readers and USB-RS232 converters as well as the same notebooks (lenovo, Win7) are used at 8 stations, but only at 2 of them I have this problem.
When I am in the situation that I do not receive Data when the reader read some, I can't see any problem. Windows says that the serial port is working fine.
But when I unplug the power supply of the reader and reconnect the COM-Port in my program it works again.
(And I do not think that the reader is the problem, because those readers are used a couple of hundred times in the company I work for)
So it must be the converter or the cooperation from the converter and the barcode reader!?
My next idea would be trying another converter, but is there any way to understand the cause?
Or did anybody have a similar problem?
Try disabling the "USB selective suspend" feature
Open Control Panel.
Click on Hardware and Sound.
Click on Power Options.
Click the Change plan settings link for the plan you're currently using. (If you change the settings on the power plan that isn't active, this won't work.)
Click the Change advanced power settings link.
Expand USB settings.
Expand USB selective suspend setting.
Select Disabled from the drop-down menu.
Click Apply.
Click OK.
If you're changing the settings on a laptop or tablet, you'll see two options: On Battery and Plugged in, so make sure to change those settings accordingly.
Once you complete these steps, Windows 10 will no longer power off USB devices connected to your computer, which can fix any problem you may have as a result of this feature.
While this guide focuses on Windows 10, USB selective suspend has been around for a long time, and you can use the same steps on machines running Windows 8.1 and Windows 7.
For more details check https://www.windowscentral.com/how-prevent-windows-10-turning-usb-devices#:~:text=When%20a%20USB%20device%20is,feature%20to%20fix%20the%20problem.&text=Windows%2010%20includes%20the%20USB,re%20not%20actively%20in%20use.
USB ports are terribly unreliable in this way. The only way to get a reliable serial.port is to use a PCI card or an Ethernet device server. You will never get a consistent connection through USB.
We get a bug reported in our serial port comms about one a month, and every single occurrence is due to someone using a USB to RS232 converter. As soon as we install better hardwarec the issue vanishes.

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.

Send/Receive WiFi traffic data

Is it possible to monitor WiFi traffic and send data myself? My goal is to learn how to monitor probe requests, send deauth packets, and other operations similar to AirCrack and the other libraries found on the BackTrack os flavor.
You might have a tough time doing what you want to do in a Windows environment. If you poke around the Aircrack site a bit you'll see that the Windows version is pretty useless in comparison to its Linux counterpart, in large part due to the open source driver availability in Linux and the lack thereof in Windows. You can definitely packet sniff etc. ( http://www.codeproject.com/Articles/12458/SharpPcap-A-Packet-Capture-Framework-for-NET ) but you won't get the robustness that you can get out of a similar application in Linux.
I don't know about sending just random radio signals, but you can certainly monitor the traffic and data. Here is an article which describes how to do it.
AirPcap from Riverbed. USB adapter that can run in monitor mode and has an API for channel switching and more, and you can send Wi-Fi frames if you buy the right adapter. Wireshark has builtin support too. The Alfa cards are a good choice, but only for 'nix

Program LED with just USB port

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 .

Creating a usb time lapse for Cannon EOS Rebel XS using a c# program

I am wondering if this is even possible. I want to make my own software to control the release of the shutter via the remote shutter release. From the diagrams I have seen it has a 3.3 v (no load) and a threshold voltage of 1.8v. I am wondering if I can use limit a usb cables voltage which is around 5.25v, if I am understanding correctly. Is this even possible, or do I need to look in a new direction. I am hoping to write the software in c# which I am fairly confident in.
Thank you in advance for your help :)
I've played a lot with these type of shutter remotes, for both the sony alpha and for the canon line of dslr's. For the cannon, it is usually a 2.5mm stereo plug. It has 3 contacts: ground, focus and shutter. Shorting each one of them and you will trigger that function in the camera.
http://www.doc-diy.net/photo/eos_wired_remote/
A few years ago, I designed my own circuitry that would take serial commands from the host (in my case an HP Pocket PC) and drive one of the lines down using a microcontroller GPIO port.
Nowadays there are similar devices with much easier connection and control options. And if you'd like to take the DIY road, there are very nice options such as the arduinos or if you're a c# addict, there are the netduinos.
Here's a commercially available solution, including SDK:
http://www.breezesys.com/DSLRRemotePro/features.htm
Working with USB devices in .Net is not the most straightforward task... There are no libraries in the BCL to deal with USB. However, it is possible to use Win32 API's.
This article should get you started in the right direction, as far as accessing a USB device if you want to pursue this.
As for whether it's possible, it might be... I don't rule anything out, but I think you're in for a LOT of research time to make it work if it's possible at all. (I've never seen anything in the Win32 API documentation that allows you to control the voltage, for example). However, it would be very cool if you could make it work, and the satisfaction factor alone may be worth the try.
Added
I took a look at PInvoke.net (a very good site for learning about Win32 API calls) to see if they cover power management for USB drives, and I see nothing at all.
http://www.pinvoke.net/search.aspx?search=usb&namespace=[All]#

Categories