Hi/Low analog output to USB

Hi,
I am trying to use MatLAB script as an on/off switch. In simple words, I wish to set a constant output of ~0V (low) or ~5V (high) at the USB port whenever I need to. How could I do this in a most simple and inexpensive way?
Thank you

 Accepted Answer

Walter Roberson
Walter Roberson on 20 Jan 2013
This cannot be done directly with USB. USB is a shared serial bus, and only digital signals can be carried on it. If you were to use USB you would need to have a USB-interfaced device on the other end that decoded the serial commands to turn a switch on or off.
One important question for this purpose is what kind of rate of change do you need for this purpose? Another question is what precision you need in the timing. A third question is how much control you need over the pulse widths.

13 Comments

Ok. What I need is to push button on GUI and make matlab to output High to USB as long as I need it to. No precision for timing (seconds). I just want to drive a transistor with this signal. Lets say I want to turn on/off LED.
Does it have to be USB? It would be easier in some ways with parallel, and easier still with a real serial port. I think it could likely also be done with one of the sound outputs (might be easier using two channels.)
How steady does the output have to be? Some of the easier possibilities would not promise that the output would be completely continuous, with there being the possibility of small drops. A bit of low-pass filter hardware (a capacitor and maybe an inductor too) could smooth that out.
It does not have to be USB. My goal is to somehow output to the transistor. I don't think my computer has serial port. It would be more professional to run it via serial port, however, the 'sound' idea seems to be the easiest. I guess, I will just output some sinewave to the audio output and rectify it. That should work. I hope there will be enough current to run N2222.
You do not need a sine wave: you could output a constant value, which would adjust the voltage through the sound channel. If you want to get fancier, rig the logic so that "high" / "on" is determined by the difference between the left and right channels: then, when you are not outputting it would be "off", and you would output high signal to one channel and low to the other to signal "on" (and you could even go tri-state by reversing which channel the high signal was going to.) [It made more sense to me earlier tonight. :) ]
With regards to using a real serial port: the trick there would be to ignore the data and instead look at the RTS pin (serial port RequestToSend property), as that is held high when 'on'. Latched high by the hardware, without need from the controlling software to continually send a signal.
Using the RTS pin that way is very common in serial port usage. For example, if you want a modem to answer phone calls, you set RTS on, and when you want to hang up, you set RTS off. (In turn, devices often signal that they are engaged in a primary activity by setting Carrier Detect, CD.)
Naz, if you just want to set a pin high to turn a transistor on or off, then you don't need an analog output, and the digital I/O devices would work fine. But USB is serial - that's what the S in USB stands for. Maybe you could use the built-in communications commands within MATLAB to talk directly to the USB wires, pulling them high or low.
Digital I/O is not suitable as it is pulsed, but Naz needs steady state.
USB controllers are not designed for software control of individual lines. USB is a digital data-bus type design. There is no equivalent of RTS or CD in USB, just receive / transmit / ground with a carefully timed cycle.
OK, maybe he can't use USB directly, so we're back to using a device. I do know that with my device, I can set any pin high or low and it will stay there in a steady state (not pulsing) until I change it. It seems that that is all he needs to supply voltage to the rest of his circuit (the base of the transistor plus whatever that is attached to).
IA, which of the device models are you using in the above comment?
One possibility with a pulsed (e.g., digital) output is to use a JK flip-flop, which should be incredibly cheap if you can still find them.
I'm mainly using the PMD-1024HLS but you can do it with the 3101 also. If you stick a flip flop IC on a breadboard, you need to be able to talk to it, so how would you do that without using a USB cable or a device like the PMD?
The JK flipflop would be on the output of the Digital I/O, and would effectively convert a pulsed digital I/O line to a steady-state control.
Perhaps different devices use different meanings of "digital I/O". The devices I look at the documentation for usually emit pulsed outputs rather than hold the output level, but I could imagine that other devices hold instead.
The devices I'm using do not give pulsed outputs (rectangle waves or pulse trains) unless you tell them to - basically I think you'd have to manually tell it to go high, then go low, then go high, etc. Normally you just set a value and it stays there.
That would certainly be easier than breadboarding in a flip-flop.
Walter, Image Analyst: Thank you very much for such an extensive discussion. I greatly appreciate your time. This discussion will be usefull for many people. Regards, Naz

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 20 Jan 2013
For $89 or so you can get a USB device that you can read pins from. See http://www.mccdaq.com/usb-data-acquisition/USB-1024-Series.aspx

5 Comments

What do you think about this one: http://www.dlpdesign.com/usb/io8.shtml ? Sine I need only one pin, maybe I could use it. However, how should I know if it is possible to interface this device with MatLab?
It looks like there is no driver for Win7
The particular device IA lists has digital I/O but no analog I/O. The device that Naz lists has digital I/O and analog input but no analog output.
MCC has numerous such devices, many of which support analog. I have one: http://www.mccdaq.com/solutions/USB-Data-Acquisition.aspx - the USB-3100.
I'm only familiar with some of the MCC devices, not the DLP Design or other brands. MCC has excellent free tech support.
It looks like their 1208LS might be suitable: USB, 2 analog outputs of 12 bit resolution, $129.
In terms of effort involved, possibly their device with built-in relays would be easier: USB, 8 SPDT relays, $329. I'm just thinking in terms of the time and effort to put together a relay, mount it on a small breadboard, test it, and so on. A relatively easy lab assignment for an EE student who was provided with parts and testing equipment, but a nuisance for people who do not have those things on-hand or have the relevant experience.
(I think I still remember how to solder, but it would probably take me at least 3/4 hour just to track down a local store that still bothered to stock the parts, now that the days are long gone where RadioShack carried parts like these...)

Sign in to comment.

Matyas Varga
Matyas Varga on 3 Sep 2015

1 Comment

Price and availability of those appear to be questionable. It appears to me that they are no longer sold by NI, and auction sites list them between $US150 and $US300 plus about $US25 shipment from overseas.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!