Author Topic: Isolated UART connection- unexpected behaviour  (Read 376 times)

0 Members and 1 Guest are viewing this topic.

Offline Subin

  • Contributor
  • Posts: 7
  • Country: in
Isolated UART connection- unexpected behaviour
« on: June 15, 2021, 04:08:32 pm »
Hey guys,

I am making a TRUE RMS voltmeter which is directly connected to mains 240V. In order to isolate the output(UART), I have used a PC817C optocoupler.

As the diagram shows, the anode of the optocoupler is connected to 5V through a resistor, and the cathode is connected to TX, on the LIVE uC. Now comes my question.

When I connect the collector of the optocoupler to the Ground of the receiver (USB to Serial converter*), and the emitter to RX, I can receive the data correctly. I don't understand how. If I reverse the connections, there is a periodic output (at the frequency of original output values) of two characters (always the same two characters)  but is not my data.

I thought the output of the optocoupler has to be biased like a typical transistor. What am I missing?

*(which is basically an Arduino UNO with its RESET pulled to Ground, giving access to the USB-UART function of the onboard Atmega16U2) (if its relevant)
 

Offline Unixon

  • Frequent Contributor
  • **
  • Posts: 371
Re: Isolated UART connection- unexpected behaviour
« Reply #1 on: June 15, 2021, 08:58:46 pm »
The receiving side is completely wrong. Collector must be pulled up to Vcc via resistor (~1k) and connected to RX pin, emitter must be connected to GND.
 

Offline Manul

  • Frequent Contributor
  • **
  • Posts: 824
  • Country: lt
Re: Isolated UART connection- unexpected behaviour
« Reply #2 on: June 15, 2021, 09:55:30 pm »
Why it works? Must be because RX has pull-up resistor about which you don't know. Why it does not work the other way (the normal way, with emitter grounded) I'm not totally sure, but it is probably related to higher output capacitance(?) and a miller capacitance causing feedback and slowing the turnoff of phototransistor which in turn causes increased rise time compared to the grounded collector circuit. I mean, the pull-up resistor is probably quite high value (internal MCU pull-up?), so it is possibly very marginal for achieving the required rise time for your data rate.

In any case, you need lower value dedicated pull-up. The higher your baud rate, the lower. 4.7K should be a good starting point.

Note also, that running NPN phototransistor backwards (with emitter positive) could cause base-emitter breakdown and possible damage. 5V might be safe, but who knows. So emitter should be negative.
« Last Edit: June 15, 2021, 11:30:40 pm by Manul »
 
The following users thanked this post: Subin

Offline Unixon

  • Frequent Contributor
  • **
  • Posts: 371
Re: Isolated UART connection- unexpected behaviour
« Reply #3 on: June 16, 2021, 10:12:58 am »
These optocouplers are very slow, so you need marginally low pull-up values to make open collector output swing at a decent rate.
Can't tell exactly what value is best for PC817, you need to check voltage levels at RX pin, but it might be as low as few hundreds of ohms.
If a resistor value is too high you get slow rise time, if it's too small you get elevated low state voltage.
If you want it fast, use ether HCPL060L / HCPL0631 kind of optocouplers or ADUMxxxx series digital isolators.
 
The following users thanked this post: Subin

Offline Subin

  • Contributor
  • Posts: 7
  • Country: in
Re: Isolated UART connection- unexpected behaviour
« Reply #4 on: June 19, 2021, 02:20:20 pm »
Thank you for the explanations guys. :)

I have connected the output of the optocoupler the normal way and put a pull-up of 1K on the collector and now I get the values correctly at 9600 baudrate.
I don't get the values at a higher baudrate (In case anyone is curious). I am aware that for a higher baudrate to work, I will need to use a lower value pull-up resistor or a faster optocoupler.
 

Offline HB9EVI

  • Frequent Contributor
  • **
  • Posts: 695
  • Country: ch
Re: Isolated UART connection- unexpected behaviour
« Reply #5 on: June 19, 2021, 02:38:26 pm »
you cannot decrease the collector resistor unlimited to speed up things, already due to the maximal collector current of the phototransistor, but also other effects are not making the opto switch faster
I'd always go with matching opto, the PC817 is a real slow device, so it might work now in your case, but with an opto from another source it might as well fail; a safe bet is 6N135, nothing goes wrong there with uart data
 
The following users thanked this post: Subin


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf