Go Down

Topic: Programming ATMEGA328 via optocoupler (Read 1 time) previous topic - next topic


Hi guys
I need to program Atmga328 via optocoupler, I added 3 optocouplers to signals (RX, TX, RESET).
It's seemed everything is ok but it's not work. And I read about DTR pin so I modify the circuit by adding op amp to RESET pin (to increase input impedance )but it's not work too.
By the way I can read through serial monitor.
Anyone have an idea to solve this problem. Thanks a lot.


Do you have bootloader on the chip?
You need connect Rx of one to Tx of the other! Not connecting Rx to Rx.
You are missing pull up on the Arduino side of Tx - is it part of the board?
On 8-bit (i.e. Uno) use "byte" instead of "int" if possible - it is faster and saves resources!


thanks for your replay
*yes I have bootloader and chip programed in normal state i.e. without optocoupler. therefor the connection is right.
*pull up resistor is exist in circuit but Im not update the diagram (sorry for this mistake) .


Can you see the new 328p do a reset (three blinks of pin13 LED)?
Can you get it to upload with a well-timed manual reset?
Do you have a scope to see what the signals (especially reset) are actually doing?  I'm not sure the op-amp buffer will do the right thing here...


*according to (three blinks of pin13 LED) the program is uploaded successfully if neglect optocoupler so there is     no problem with connection or bootloader , the problem is happen only if I insert optocouplers in the path
*Its not work with  well-timed manual reset


The Rx/Tx was stupid, sorry.

Are your pull-ups small enough to pull the lines quickly? Are they large enough the optocoupler is able to pull down? Are the optocouplers able to turn off fast enough?

Is your op amp circuit working OK? If it is LM741 it needs +/-10V. If it is other op amp powered from Arduino is it rail-to-rail op amp (is it able to pull to positive rail to stop current in the diode)?
On 8-bit (i.e. Uno) use "byte" instead of "int" if possible - it is faster and saves resources!


Aug 19, 2017, 04:29 pm Last Edit: Aug 19, 2017, 08:35 pm by avr_fred
My first question would be why? Why do you need to use optocouplers? I can assume it is because the two devices are at different voltages. But, assumptions don't fix things and that's all we can do, assume, because you've told us so little. A schematic without resistor values is not helpful. No voltages anywhere. If the issue is different voltage levels, a TXB0108 breakout board would have solved all of this days ago.

But, as we say in my part of the world, since you're already up the tree looking for wood and given the limited facts we have, I can point to several things that could be wrong.

1) As already mentioned, the '741 will not work in this application as the output will never go below ~2 volts. Several choices here:

   a) Replace the 741 with a rail to rail opamp.
   b) Provide a negative supply for the 741.
   c) Replace the 741 with a proper logic buffer, something like a SN74LVC2G17 or similar.

2) Your configuration of U5 assumes that INPUT_PULLUP is turned on in the ATMEGA16U2-MU. Unless less you have verified this with an oscilloscope, it would be safer to add a pullup resistor. Note that the pullup will be in series with the 1k resistor network on the Uno board between the USB interface and the '328.

3) There is no bypass capacitor shown on your target '328.

4) Your unknown resistor values could be the problem. The PC817 is a really cheap part, the current transfer ratio (CTR) can be as low as 50%, meaning you have to put 2ma into the LED to get 1ma of current through  the transistor.

5) You show no crystal network on the target '328. If that means you're relying on the the 8mhz internal R/C clock, which bootloader did you install? Further, did you install support for download to a 8mhz internal clock target with MiniCore or another compatible hardware configuration?


Dear avr_fred
Thanks for your deep opinion, I made some attempts may be it will answer your questions and please see the attached diagram which is only simplify the circuit to minimum component to reduce possibilities and diagnose the problem .
This simple circuit is not work but I found these notes:
•   Its run 100% properly    when using testing  jumper (mentioned  in diagram )
That's means the connections, bootloader and power is correct.
•   The circuit (without jumper) can transmit and receive data via TX and  RX
(checked by serial monitor "transmit and receive" )
•   It's not work when attempt program the chip.
Thanks for your concern.


Continuing with the circuit shown will destroy the target atMega328.

With a 5 volt supply, a 100 ohm resistor in series with an 1.2 volt LED diode - allows two times the maximum rated port pin current to flow.

If you want further assistance, please post a schematic with all resistor values and supply voltages shown.


forget resistor values I will correct it to normal rated current the main purpose why the circuit not work in programming state while its transmit  receive  properly and the reset pin is correct
 external voltage is 5v I correct LED RES TO 470 OHM.


Baud rate while programming is possibly higher than while your sketch is running. It would explain why it works in sketch and not in programming - 10k may be too high to pull line up quickly enough to generate 1 at high baud rate. Also it takes some time for the phototransistor to turn off when the LED is turned off.
On 8-bit (i.e. Uno) use "byte" instead of "int" if possible - it is faster and saves resources!


you get it my friend , you are right
 I tried with 115200 boadrate and the signal not pass,So I will try with faster optocoupler
thanks a lot


Why is there no pullup of the output of U5?
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0


So I will try with faster optocoupler
You may try smaller (about 2k) pull-up resistor, it may be enough.
On 8-bit (i.e. Uno) use "byte" instead of "int" if possible - it is faster and saves resources!


its not work may be faster optocoupler

Go Up