Arduino code only runs properly when powered by USB to laptop

Hi everyone,

I am having an issue running my Arduino code when powered externally. I have an Arduino Mega 2560 with an Ethernet shield plugged in on top. The code is set up to power a rain gauge that approximates the amount of rainfall using two normally-closed, 12 V solenoid valves and an ultrasonic sensor. When the code starts, the top solenoid valve opens allowing water to collect. The ultrasonic sensor is placed to measure the amount of water collected. When the water reaches 12 cm, the code opens the bottom solenoid valve and allows the water to drain from the collection tube. The valves are powered by their own 12 V power adapter. The power goes to the valves when the Arduino sends voltage to a transistor acting as a switch.

When the Arduino is plugged into my laptop with a USB, everything runs perfectly. When the USB is plugged into a wall adapter it no longer works. I connected a multimeter in both cases to measure the voltage and current being supplied by the USB and they are identical. I used an oscilloscope to look at the voltage signal and there appears to be a little noise when the USB is connected to a wall adapter. The weird thing is when the oscilloscope is connected, everything works perfectly fine again. When I measured the voltage when the USB was connected to the laptop, it was actually noisier than the signal from the wall adapter. So right now I can only get the code to run properly when the USB is connected from the Arduino to my laptop or when a USB is connected from the Arduino to an oscilloscope to a wall adapter.

Any feedback would be much appreciated.


How do you power the Mega. DC socket, V-in, or 5volt pin.
What is the voltage and current of your external supply.
Is the supply regulated.

When using the 9V adapter I use the DC socket otherwise it is powered directly by the USB socket. The one supply is a 9V 1A adapter, the USB when connected to the wall socket and or to the laptop outputs 4.82V and about 250mA. I have measured all three and none seem to be regulated. However, the laptop gives the noisiest signal and is the only power source that works.

Is there anything in your code (which you have not posted) that relies on USB communication?

Is it possible the USB cable is providing a GND connection to something?


Are you sure it’s a 9volt DC adapter.
9volt DC on the DC socket should be the same as USB supply from the laptop.
Could you post a picture of the setup and supply.

I have cut the USB cable so that only the power and ground are connected to the Arduino so I don’t think communication is the problem. The project is actually a weather station that has multiple sensors so the code is kind of long. I have confirmed with a multimeter that the 9V adapter is giving a constant 9V.

FullCodeSept9.ino (42.6 KB)

So right now I can only get the code to run properly when the USB is connected from the Arduino to my laptop or when a USB is connected from the Arduino to an oscilloscope to a wall adapter.

A scope ground lead is grounded to mains ground.
Maybe a ground is missing in your setup, and the scope completes this.

It sounds like you have a "common mode voltage" noise problem. This will be exacerbated if the Arduino is connected to very long wires to your weather station sensors and actuators. You could just connect GND to a local Earth and this may fix it.

Disconnect or disable the pin 19 interrupt at the Mega and see if that helps. If this is firing due to the noise then the Ethernet begin function may never return or the initialisation may be delayed by repeated interrupts so the watchdog kicks in. Try moving or disabling the watchdog init code.

To debug the system you can't use print statements unless disconnecting the laptop or connecting the laptop to a charger invokes the error state.. So, switch on a led at the start of setup, if it ever goes out briefly you will know the Mega is rebooting due to the watchdog. Switch on another LED before a suspect function is called, add a delay so it can be seen, switch it off on return from that function. If the LED stays on the code is locking up in that function and then look for a watchdog reboot after 8s. You should be able to narrow down where the problem is using this approach and hence identify the root cause.

Lastly if your weather station is mounted at a high point, then ligthning stikes nearby can blow the Mega due to the pickup on any long wires so add supression circuits to the signal lines from sensors and also the power feed.

Maybe you can get a multimeter to check if the 'negative' terminals of all relevant devices are all electrically connected together...... ie connected to the negative terminal of your power supply. This includes all ac adapters, arduino ground etc.

But make sure power is off when doing this check.

Some things to check/try...

  • Use a star point ground connection.
  • Use only one GND wire from the 12V circuitry to the star point GND.
  • Ensure there are reverse connected diodes across the solenoid coils.
  • Try 10-100µF with 100nF cap from 5V to star point GND.

Thanks a lot guys, connecting to Earth ground fixed our issue. Adding the capacitor also helped smooth the signal.

Glad to hear it is fixed. The low cost switched mode USB power supplies often generate large common mode voltage spikes and this causes problems with long sensor lines. I have measured 80V narrow common mode voltage spikes >:( caused by stray capacitance in the poorly designed power supply, these have low energy so an Earth connection provides a path to dump the charge. Laptop supplies tend to have inline baluns and have better designs so common mode voltages are much lower.

Hey @Andrew and all,

I'm having this exact same problem.

When you say 'connecting the earth ground' fixed it. Exactly 'earth ground' are you talking about?

With my circuit I have connected everything to a common ground. So I have the arduino's GND pin connected to all the other grounds in the circuit.

I'm not sure what else I'm missing.