# Java/Arduino Controlled 120VAC Power Outlet

I'm working on a project with an end goal of controlling a power outlet with a Java program. Currently, I have the relay wired to outlets, a 3 prong 120 VAC plug, and my Arduino. Everything works as intended: my Java program can trigger the relay/outlets on and off.

The problem:

When I have a load hooked up to the controlled outlets, my Java program will crash after a few cycles of on/off (changes depending on the load plugged in). If I unplug all loads from the outlet- it cycles on/off seemingly forever. How do I change my system so that Java will not crash, irregardless of load? What is causing a difference in the Arduino from a load being connected?

My attempts:

• I have tried using an external power source to power the relay and only using the Arduino to pull pins to a common ground. Same result.

• I have tried to debug my Java program extensively. The error occurs trying to write data to the serial port. Keep in mind, however, the program will successfully write data a couple times (number depending on the load) before it fails.

• I have tried all outlets (both relays) and have had the same results on all.

• A 12 W rated alarm clock causes the error much quicker than a 14 W rated hot glue gun.

• Both outlets are GFCIs.

Please let me know if there is any other information that would help solve this problem.

Edit 1: Just realized that I don't have true optical isolation (that I thought I had) because the jumper was left on Vcc and JD-Vcc. I will try later this evening with and external power source to control the coil (JD-Vcc) and the Arduino to control the 'switches' (Vcc). Will report back to share results.

Edit 2: Pulling the coupler and feeding JD-Vcc from a separate power supply (common ground) did not work. I've put 330 Ohm resistors between Arduino pins and relays; didn't solve the problem (might have given me more on/off cycles before failure, but not a fix). Switched the wiring so the outlet is normally off (was my intended configuration). Working on a wiring diagram, brb.

Edit 3: Circuit Diagram. Yes, its ugly. The switches on the Sain Smart are closed when IN1/IN2 are pulled low. And now I see there is a built in schematic function here. Good to know for next time.

Edit 4: I haven't managed to figure out why it is acting like this. Anyone have any ideas? At this point, a work-around may be acceptable. Capacitors, resistors, opto-couplers? What would be best/cheapest?

Edit 5: Would a snubber (Red Lion, SNUB0000) across each outlet be of any benefit? I finally found a related post (#101815, I can't add another link) but am not sure if I can apply any of the potential solutions. Its been recommended putting a small ceramic capacity across the Vin of the Arduino; is putting it between the 5V arduino pin and ground what was meant by this?

• Larger capacitors across Arduino power supply Vin. | drive relay through small series resistor (1/2 or less of max you find they will tolerate and work well and modest capacitor on Arduino side - maybe 1 uF electrolytic? Aim is to stop load pulses riding back down driver leads. – Russell McMahon Apr 1 '15 at 6:52
• Not an answer to solving your bug but for reference, X10 is an easy way to control power with excellent isolation from the power lines. It's more expensive than a $5 relay but keeps you and your experiment out of harm's way. X10Firecracker computer interface works just fine on 5v and is ~$10 from ebay sellers. An X10 wireless transceiver will run your first device; any other X10 module can be used for additional ones. – JRobert Apr 1 '15 at 21:46
• I know it's tempting to switch the neutral like you did because of the minimal voltage swing and because low-voltage DC stuff does it all the time, but for safety reasons with high-voltage AC or DC, ALWAYS-ALWAYS-ALWAYS switch the hot. Switching the neutral causes the entire load to become hot when switched off. – AaronD Apr 7 '15 at 15:36
• Thanks, good point! I intended to do it that way (switch hot) but then did it wrong and just left it. It is now switching hot, instead of neutral. – FourMileIsland Apr 8 '15 at 1:14
• Can you list how your java app is crashing? Is it getting an unexpected value over serial, losing serial port, or ... ? – CoderTao May 16 '15 at 23:30

That sounds like your Arduino is getting corrupted from noise spikes on switching the AC load on and off. The reason a glue gun affects it less, is that as a pure resistive load, it doesn't generate very much noise, but the alarm clock probably has a switching power supply in it that is dumping higher frequency noise back on the line. Even an older motor driven clock can put spikes back on the line depending on the motor and how it is controlled. Have you tried using an AC filter between the relay(s) and the load (Not a surge suppressor, but an actual LC filter)?

It is also possible, though from the description, unlikely that the relay coils themselves are generating enough of a spike to corrupt your processor. Industrially, for just that reason, we use RC snubber networks across the coils of just about every relay/solenoid driven by an electronic output.

Also the above advice is 100% correct, do NOT switch the neutral of your AC load. Very bad business there from a safety (and at least US Code) standpoint. If the neutral must be switched, to be code compliant, you would need to use special safety relays with force-guided contacts in series.

• I have not tried using an LC filter yet; filters are where my knowledge drops off significantly (haven't thought about them for 4+ years). But you mean this set up link, which if I'm not mistaken can be found in modular form like link, not necessarily that one, just as example. Would that be put between relay and load/outlet, or between relay and arduino? LC's cut out AC because all current must pass through the inductor, right? – FourMileIsland Apr 8 '15 at 14:59
• Actually, more like this [link]en.wikipedia.org/wiki/Line_filter – R Drast Apr 8 '15 at 15:35

A circuit diagram will help a great deal. Something sounds fishy with your connections. It sounds like you have some feedback to your arduino (which you shouldn't have) when a load increases. You could maybe add an opto-coupler between your relay and your arduino, but that would be treating the symptoms and not the cause. My 2\$

• Thanks, I'll pop the box open and draw a circuit diagram later today when I get home from work. I haven't done anything fancy- each wire in the 3-wire cord branches into two wires. Ground to ground on the GFCI's- Hot to hot on the GFCI's- Neutral to relay. Then relay to hot on the GFCI's. I have the relays configured to be normally connected; the outlets are on unless I pull pins low. The Arduino has two digital pins connected to IN1 and IN2 on the relay (pins 11, 12, iirc). 5V Arduino output and ground, to + and GND rails on breadboard. – FourMileIsland Apr 1 '15 at 14:21
• Above should read "Neutral to relay. Then relay to neutral on the GFCI's" not "relay to hot" – FourMileIsland Apr 1 '15 at 17:33