In this article I explain the design methodology in the opto-coupler inputs of the NMEA2WIFI multiplexer. This type of input is not strictly necessary but it is very useful when the talker and the listener(the multiplexer) do not share the same ground or when due to noise, a voltage difference could develop between the 2 grounds. Here is the design for input port P2:
This is the critical port as it baud rate can go as fast as 38.400 baud. In terms of frequency we may think that we need a circuit that passes a square wave of 19.2KHz through the isolating coupler with minimum modification. The project was built around the 6N139 high speed opto-coupler. As the voltage at the inputs pins of the ESP8266 (pin 6 in the present case) can not go higher than 3V6, the collector of the output transistor is connected to the 3V3 voltage rail (supplied by the internal voltage regulator of the D1 Mini module) through resistor R3. The requirements for the ESP8266 in terms of voltage levels are shown here:
In order to avoid the destruction of the input pin circuitry, the voltage Vo can not be less than -0.3V or greater than 3.6V. For a voltage to be recognized as a logic LOW, Vo should be less than 0,83V and greater than 2.48V to be recognized as a logic HIGH.
It should be noted that there is a great tolerance margin on the parameters of the 6N139. For example the CTR (current transfer ratio) has a typical value of 1300% but a minimum value of 500%. Let us discuss this value using the 2K2-470R design (R2=2K2 and R3=470R). In the next and following pictures the yellow waveform refers to the voltage Vi (more precisely the voltage measured at the connection between Ri ad R2) and the blue waveform refers to the voltage Vo
In this discussion we used a "poor" 6N139 for the sake of the explanation. Normally the waveforms are better than the waveform shown in this picture. The first problem that we note is the difficulty of the output transistor to leave its saturation state. The time tPLH (propagation delay to logic high) is too great. If we look to the 6N139 datasheet it can be as high as 90us (micro seconds) depending mainly on the load resistor (R3 in our case). For a 38400 baud signal (or a 19200 Hz square wave) the width of the pulses have a duration of 26us. Therefore if we had use R3=4K7 as shown in the datasheet for the case of tPLH=90us, the waveform would not leave the LOW stage. We also note that the highest voltage reached by Vo is about 2.1V and so it will not meet the 2.45V requirement to be recognized as a logic HIGH. Normally UARTS read the waveforms not on the middle of the pulse width but further to the right (at least this what happens in the software UART implemented on P1). Even with this "bad" voltage the ESP8266 recognizes it as logic HIGH. Before we discuss this further let us show the same circuit operating with a 2,400Hz (equivalent to 4800 baud). At this speed there is no problem with the circuit as it can be seen in the following picture
So let us return to the "38400 speed". Before mounting the IC on the PCB we test each 6N139 one by one and classify them in two classes: "poor" and "rich". The "poor" instances as the one shown in this article are used on P1 whose baud rate is 4800 or 9600. For P2 we choose the "rich instances". In order to alleviate this problem we changed the design aiming at a better 6N139 performance on P2. The way to go was to decrease R3 which also decreases tPLH. First we tried R3=270R
It is better but may be not enough. So we tried R3=120R
Now the waveform is clearly better but a new problem appears. The transistor current Io is approximately 25mA (3V / 120R). The maximum allowed current for Io is 60 mA so we need to be careful. On the other hand for a transistor to be in saturation its base current Ib should be greater than Ic/hFE. With opto couplers is almost the same using If instead of Ib. We already referred to that the CTR (current transfer ratio) has a minimum value of 500%. This means that for Io=25mA the diode current has a maximum value of 5mA (500% gain is an absolute gain of 5). Actually we can evaluate the If current. The yellow waveform was taken at the junction of Ri and R2. Also, in order to use both oscilloscope channels we had to connect pin 3 on the 6N139 to ground. So we have a voltage across R2 and Vf of 4.2V as measured from the picture. Taking into account that for If=5mA Vf is about 1,4V (see Fig.4 on the datasheet) the voltage across R2 is 2.8V. So If is not 5mA but 2.8V/2K2 or 1,27mA. Therefore the CTR is 25mA/1,27mA x 100% or CTR = 1900% which is even better than the typical value of 1300% shown in the datasheet. But had CTR be 400 we would need If to be 5mA to take the transistor into its saturation state. With R2=2K2 a voltage of about 12,5V would be necessary. We hardly believe that Nmea 0183 talkers are able to supply such high voltage not to speak on the internal resistor Ri. Also, if we look carefully, the blue logic LOW is now about 0.2V. That is OK but in our experiments with different 6N139 instances we got this voltage to be as high as 1V and so not respecting its "logic electric" requirement.
So we return to 270R as a compromise (also less power dissipation) and decreased R2 from 2K2 to 1K. Those are the values that we are now using in the multiplexer. By lowering R2 from 2K2 to 1K we are asking more current from the talkers. However, if an user has a talker that is not capable of supplying 3.5mA at 5V (using Vf as 1.5V) an external series resistance can be used. Since the CTR of the 6N139 is typically much greater than 314% (3V/270R results in Io=11 mA which divided by 3.5mA gives CTR=314%) a greater than 1K value for R2 is admissible. In the following picture it is shown the result with R2=1K and R3=470R
Before I terminate, I refer the possibility of forgetting opto isolation and using a simple transistor to invert the yellow signal. Note that in a idle state (no data coming) the yellow voltage (RS232) is low and the LED diode does not conduct. Pin 6 on the ESP8266 is high (idle state on TTL serial). While taking the picture to write this article I removed the 6N139 IC and inserted a BC547 NPN transistor in the DIL-8 socket as shown here:
The base went to pin 2 of the DIL-8 The emitter went to pin 3 which we short to pin 5 and collector went to pin 6. With R2=2K2 and R3=470R the waveform is as here:
Not surprisingly BAD! If the hfe of the transistor is, say 200 it corresponds to a "CTR" of 20000%!!! So the transistor is almost permanently in saturation. But putting a series resistor of 27K in series with R2 we ge this:
Good but no isolation!
Best Regards, Luis