Isolated UART connection- unexpected behaviour - Page 1
Forum section: electronics / beginners

author: Subin
author_rank: Newbie
timestamp: June 15, 2021, 04:08:32 pm
content: 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)

author: Unixon
author_rank: Frequent Contributor
timestamp: June 15, 2021, 08:58:46 pm
content: 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.

author: Manul
author_rank: Super Contributor
timestamp: June 15, 2021, 09:55:30 pm
content: 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.

author: Unixon
author_rank: Frequent Contributor
timestamp: June 16, 2021, 10:12:58 am
content: 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.

author: Subin
author_rank: Newbie
timestamp: June 19, 2021, 02:20:20 pm
content: 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.

author: HB9EVI
author_rank: Frequent Contributor
timestamp: June 19, 2021, 02:38:26 pm
content: 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 fasterI'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