Hi,
I want to put an optocoupler between a TTL signal and the Arduino's RX pin. The signal is 9600 baud.
It looks like I should use a 6N137 or 6N138 optocoupler.
Can someone point me at a schematic showing which resistor values I should use and how I should wire things?
I've done some research and found out the following. It looks like I'll need two resistors: one between +5V and the LED anode on the TTL/input side, and one between +5V and V_E (6N137) or V_0 (6N138) on the Arduino RX side. 330 ohms was suggested by one site for the LED resistor, but that was for a situation where the LED anode was being driven directly by the TTL output.
I'm trying to connect a MindFlex to the Arduino (How to Hack Toy EEGs | Frontier Nerds) but for safety reasons -- and because people have reported problems with the MindFlex after connecting it directly -- I want to make sure there's an optoisolator between the two.
The LED has a forward voltage of 1.4V and a current requirement between 6.3 and 15 mA so any resistor between 240 and 570 Ohms should work.
The pull-up resistor on the open-collector output of the isolator should not be critical. The data sheet gives 350 Ohm, 1000 Ohm and 4000 Ohm as examples. I'd pick something near 1K because that's in the middle.
edan:
John,
Thanks! How should I reconfigure the circuit so that it doesn't invert the signal?
That can be simply a function of how you wire the input LED diode circuit. If you ground the cathode of the led then the ultimate output of the opto will be inverted. The other (non-inverted) method is to connect the cathode to the TTL signal and the anode would wire to the series current limiting resistor and to +5vdc supply from the signals source. Either method requires access to ground or +5vdc of the device supplying the signal, depending on if you want inverted or non-inverted output signal.
Another method to 'non-invert' the output would be to wire the opto output to a npn transistor with proper pull-up and base resistors. I believe there are also optoisolators that have PNP transistor in it's output section so that the output switching will be non-inverting.
Lots of ways to skin a cat, however it's highly recommended to kill the cat first.
I did just that. I build 2 version. One with 4N35 and one with 6N138
My design use a comparator, an NOT gate and a transistor. I test at max speed 115200 baud. All waveforms was there and schematic for one chanel. I have a test code in Processing to see a "square" pulse at my scope, I did a loop-test to check everything work on all speeds. My issues during design is the speed. The higher you go, the more the wave got "distorded" The signal get smaller and loading it into RX will be a bad idea. That is why my design use a buffer and a transistor. A 2N3904 is the right part for the job. It can switch very fast.
Just check the datasheets of all opto-couplers. I MEAN check it ! And do a lot of breadboarding. I personnaly like the 4N35 design. It is the comparator make the circuit more reliable. If you want a simpler design, use the 6N138 and dont go above 9600 baud, and connected directly, but I don't garanty it going to work reliable.
Techone,
Thanks for the link. I looked at the 4N35 originally but kept reading about how slow it was; I assume that's one reason you had so many problems! Do you mean that the 6N137 or 6N138 might also be too slow? It seems hard to imagine they'll be outperformed by a circuit using the 4N35, even with the extra components you used.
The output waveform of the 4N35 @ 115200 baud, the way I set it up, I did get a small square waveform riding at about 3 V - the comparator make it proper TTL level, depending the Vref of the comparator. The 6N138 did have a waveform , a small square wave riding close to 0 V - about 0.5 V. The 6N138 or 6N137 will work @ 9600 baud, but I don't garanty it will work @ 115200 baud without the help of external parts.
I looked at the 4N35 originally but kept reading about how slow it was;
You are right about it. But, with an increase of I - led and with a lower for R - collector, I did test it and I got a tiny square wave which I can use a comparator an make it better, it was a solution and I make it work. I did program an Arduino with it. The 6N138 is better, but if you planning to use it directely into the RX pin, you may have some problems. The signal from the opto-coupler may be to small. Loading the signal is the cause. I hope you have a scope handy. I bought mine second hand for $150 ca last year at Active Surplus in Toronto.
When you are going to make it, I recommend you test it properly.
It seems hard to imagine they'll be outperformed by a circuit using the 4N35, even with the extra components you used.
That what I call : keep pushing it !! I just trying to do the same thing like Mr.Scotty in Star-Trek
I have a 25 MHz analog scope. It do the job. Those signals, ( serial signal ) are a bit harder to get a lock and stablelize on the sreen. For a better stablelization and lock, I send a "square wave" data - 0x55 -- > b01010101. So the scope can lock on the square wave. My time setting was 2 uS to get a 115200 baud signal. A serial pulse has : Start bit - 8 data bit - parity bit - stop bit. The parity bit can be removed in software. So it like : 1N8 - one stop, no parity, 8 data bit. can be set in software.
edan:
Thanks!
How much bandwidth should an oscilloscope have to troubleshoot these kinds of signals?
An old 'rule of thumb' for desired scope bandwidth was 5 to 10 times the highest frequency of interest to be displayed. So a 100mhz rated scope would be able to display a 10mhz square wave with significant accuracy. Recall that a square wave is comprised of a sine-wave fundamental frequency (10Mhz in this example) and all odd harmonics of that frequency, so 20,30,40,50.etc..are 'added' into the signal. As the scope's bandwidth is reduced or the signal frequency increased, the display will show a slight rounding of the corners of the square wave that really doesn't exist in the original signal. That being said a scope is still a very useful troubleshooting tool even if the bandwidth is less then one might wish for.
Lefty
retrolefty,
Huh, so it's kind of like the Nyquist thing in audio, then. What's the effective frequency of a serial data transmission at 9600 or 115200 baud?
I was thinking... A baud rate is a bit per second ---> n baud = n bits / sec Sending 0x55 , 1 - 0 , repeat... So the clock to send those bits will be n bits / sec = n baud = n Hz. But if you send a 0x55. So I divide by 2. 1 going to 0 and 1. So it will be :
n baud / 2 = frequency of the 0x55 being send.
example : Sending 0x55 at 9600 baud. The frequency of the square wave is : 9600 / 2 = 4800 Hz. The square wave frequency section being send.
Sorry to dig up such an old topic, but it's worth mentioning.
I was struggling to get the 4n25 to do anything useful, wondering if it was even fast enough for 9600 baud (it is), when I thought one last ditch attempt...
I tried hooking up a 3v battery pack and transistor to the NPN end of things to see what would happen if I used the headset's TX to trigger the collector, and accidentally wired the headset in backwards - inverting the signal, and the packets started coming through perfectly.
Other than that, just a pullup resistor as suggested from the chip's pin5 to V+, and you're good to go!