Serial port oddness: Code reset on disconnect?

Hi Guys,

I got my Arduino Duemilanove the other day and I wrote a little python script to light up an LED when I have unread emails in my Gmail account.

The script has the following logic:

1.) Connect to gmail, check unread email count 2.) Set signal to 'H' if unread emails exist unless set to 'L' 3.) open serial port, wait 1 sec. write the signal to the port. Disconnect 4.) Sleep for 3 minutes before doing it all over again.

The sketch on the arduino looks like this:

int ledPin = 13; 
int signal;

void setup() 
  pinMode(ledPin, OUTPUT);

void loop() 

  if (Serial.available() > 0) 
    signal =;
    Serial.write( signal );
  // if it's a capital H (ASCII 72), turn on the LED:
  if (signal == 'H') 
    digitalWrite(ledPin, HIGH);
  // if it's an L (ASCII 76) turn off the LED:
  if (signal == 'L') 
    digitalWrite(ledPin, LOW);

  delay( 1000 );  

Everything works fine but I'm seeing some strange behaviour:

The script will successfully connect to the Arduino and set the LED pin HIGH but the LED will go dead when the python script disconnects from the serial port. I've seen the same behavour when using "Serial Monitor".

Can someone explain to me how Arduino serials ports work in relation to the Serial library ?

Your comments are appreciated.


On each activation / deactivation of the serial port the arduino board resets. This is caused by the auto reset circuit which is an AC coupled signal from the DTR line to the arduino's reset line. You can unsolder the capacitor coupling these to stop this from happening but you then have to manually reset the arduino at exactly the right time when uploading sketches.

Thanks Grumpy_Mike. You saved me a lot of heartache. I'm most greatfull. I'll buy a random stranger a pint and call them Grumpy_Mike for evening in your honor :-)

I can't solder to save my life, so I'm gonna tweak the python code a bit i.e connect and stay connected.

Thanks again.

If your host side serial library allows turning off DTR when connecting, you should do that.