ATMEGA 328P is "hanging" when it is feeding PWM to a DC motor controller

I am using linear potentiometer to control the speed of a DC motor. I am using the following schematic.

ATMEGA 328P receives the value from linear potentiometer via analog read. This ATMEGA transmits the value via CAN. Other ATMEGA receives it and analog writes the values via optocoupler (4N35) to the motor controller (Kelly's KDH14601E). The motor controller uses this value to control a 110V DC motor (lynch LEM 200 D135 RAGS). The High voltage and low voltage sides are completely isolated in the motor controller. When the motor is disconnected or is in no load condition, everything works fine. But when there is load on the motor, both the microcontrollers stops responding. They basically freeze and the motor rotates at a constant speed. Sometimes the value fed to motor controller is corresponding to the last read value and sometimes a value which is not possible according to the code of microcontroller. I made some changes in the setup and have achieved following results-

  1. Initially I was not using the optocoupler and the microcontrollers were hanging in no load condition also.

  2. If I use a different LV (Low Voltage) supply for motor controller and optocoupler, everything works fine but I cannot do this in my final setup (I must use only 1 LV battery). In this case, both low voltage sources are completely isolated.

  3. If I use a different LV (Low Voltage) supply for motor controller and optocoupler and connect the ground of both Low Voltage batteries, everything works almost fine. Sometimes the microcontroller reads a value which is not practically possible but there is no hanging of microcontrollers.

  4. I put a diode in the path which connects optocoupler output to motor controller, but observed no change.

Please help me out with this.

P.S.: The motor controller manual states that a potentiometer should be used to feed the controllers bu I cannot directly feed the value of linear potentiometer as I have to make certain modifications to the values.

Thanks and regards

Parimeya

Most likely, that somekind of glitch getting into arduino power supply loop, ether via ground or +12V. The solution I could imagine, is to complete DC isolation, that you actually started introducing an optocoupler. If secondary power supply for arduino isn't an option, than DC-DC isolated converter may be an answer.
https://www.arrow.com/en/products/rfm-0505s/recom-power