Error inside Serial serialEvent


I’m building a project using Processing and Arduino using a relay board for turning a 220V light bulb on and off, and running into a strange problem. The application works fine for a while (between 30 seconds and 3 minutes) and after some time the Processing app crashes (the JVM stops working). The serial port is then unusable until I disconnect the USB cable and restart the Arduino board.
I suspected that the Arduino is using too much power so that the OS shuts down the USB port, but the same issue happens when I connect the board with it’s own power supply.
I tried this with Processing 1.5 and 2.0, the issue is similar but the errors I get are slightly different - in all cases it is referring to an error writing to the serial port but I guess this is because it is closed by the OS. I’m pretty new to Arduino and electronics so I may be making no sense here, but this was my guess.

I’m attaching a trimmed down version of my code which recreates the problem. Any help will be greatly appreciated.


import processing.serial.*;

final int UNIT = 250;
int wait;
int last;
boolean on = false;
Serial port;

void setup() {
  port = new Serial(this, Serial.list()[0], 9600);

void draw() {
  if (last + wait - millis() > 0)
   if (on) {
     on = false;
     wait = 3 * UNIT;
   else {
     on = true;
     wait = 5 * UNIT;
   last = millis();


const int LIGHT_PIN = 4;

void setup() {

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    int incomingByte =;
    // say what you got:
    //Serial.print("Received: ");
    //Serial.println(incomingByte, DEC);
    if (incomingByte == 48) {
      //Serial.println("turning off");
      digitalWrite(LIGHT_PIN, HIGH);
    if (incomingByte == 49) {
      //Serial.println("turning on");
      digitalWrite(LIGHT_PIN, LOW);


The communication link between Arduino and computer is being lost or corrupted somehow. These are possible reasons:

-Check and/or replace the USB cable, could be deffective. -Check for strong sources of interference close to your system, such as big AC motors, compressors, etc. -Plug you system into a different outlet. -Move the realy you are actuating a few feet away from Arduino and USB cable. The sparking in the contacts could be interfering. You are switching 220 VAC with a light bulb which is a heavy load. This creates heavy sparking in the realy contacts. When the contacts close and open they spark and radiate Electromagnetic Interference (EMI) maybe strong enough to interfere with your system communication link. There are methods to reduce EMI. -Check the USB connector is not introducing faulty contacts when you move the cable.


Thanks for the quick response - moving the relay farther away from the Arduino (now about 70cm) does seem to do the trick! I also put a layer of tin foil between the arduino and relay. I never would have guessed this.

Ideally I'd like the relay and board to be inside the same box - is there any way to reduce the interference at close distance?

Thanks again.

Yes there are; but as this is a very complex subject, I would pass on the specific advises here. Although there are general meassures that can be taken to reduce EMI, there are also other methods highly dependent on the specifics of your circuits. Try to study the subject so you will understand the principles.

Just a few general subjects. There are many more...

-EMI filters. -Relay contacts spark reduction methods. -Relay coil damper diode. -EMI shielding. -Shieldered cables. -Damper capacitors. -etc.

ok, thanks. I'll go study a bit now...

you've been a tremendous help. I was breaking my head for hours around this problem.