# Microcontroller reset on mains connection

I'm helping someone build a static VAR compensator. We figure it'll be evident that the power factor is being corrected if there's a display that shows AC voltage and current waveforms, so one can see as they come into phase. For getting the mains voltage waveform on a TFT display, I used this circuit:

Only difference is I'm using a cheap 240V-12V transformer in place of the adapter. Also I'm using an LM358 in place of the LMV321.

The 'brain' is an Arduino Mega powered through a USB port, for now. The problem is each time I plug the transformer primary into mains, the Arduino resets but after that the waveform is displayed as expected, though it has some weird dips at the crest and trough. If I disconnect or something affects the plug's contact with the mains socket, the Mega resets again. And the thing is, it not only resets but I actually hear my PC make the tone of a disconnected USB device. At some point, avrdude started acting up till I disconnected and reconnected the Mega.

Calculations and the TFT waveform show that the voltage at the analog input is within bounds. This seems like an issue of excessive current draw over USB but I'm not sure. I've also not gotten around to properly terminating the other op-amp in the LM358, if that's relevant to this issue. I would appreciate any suggestions as to the cause and a solution.

• What is the input voltage range of the arduino you are using? – Andy aka Oct 13 '16 at 3:30
• Can you put a scope on the input to the Arduino and single shot capture the waveform as you plug the mains transformer in? – ThreePhaseEel Oct 13 '16 at 3:31
• @Andyaka The analog input range is the usual 0-5V range. – TisteAndii Oct 13 '16 at 3:33
• @ThreePhaseEel Sadly, I don't have a scope. However the TFT screen, which seems the closest thing I have to a scope, shows some spikes on the screen (as I try to fix the transformer plug firmly into mains) right before the Mega resets. – TisteAndii Oct 13 '16 at 3:39
• Change the voltage divider from 100k/10k to 50k/10k/50k (with the output still taken off the 10k resistor). Make any difference? – Bruce Abbott Oct 13 '16 at 6:33