Can't run sketch without serial (and resets)

Hello,

I just bought my very first Arduino so I'm a totally new at this. The board is an Arduino Pro Micro (bought here).

I'm using the default Blink example to test it. When I upload the sketch to the Arduino it doesn't start by itself, but when I open the serial monitor it does start blinking. The moment I close the serial monitor it stops.

Also when I disconnect the USB and reconnect it it doesn't blink anymore.

How can I make the sketch run immediately after uploading, without the need for a serial port connection, and run immediately when connected to the power?

The end goal is to have a motion detector that just turns on an LED on motion detection (I already have the hardware for this).

Please post your full sketch using code tags (</> button on the toolbar).

// the setup function runs once when you press reset or power the board

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

OK, I just wanted to make sure. The first part of what you're experiencing is the expected behavior from a sketch like this:

void setup() {
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

The while (!Serial) causes the code to wait until you open Serial Monitor. But your code doesn't contain that line and even that sketch shouldn't stop when you close Serial Monitor.

I noticed the code does not run at all if there is nothing with serial in it.

This does not work at all (with serial monitor open or closed), no blinking whatsoever

// the setup function runs once when you press reset or power the board

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

This works only when the serial monitor is open.

// the setup function runs once when you press reset or power the board

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  Serial.print("test");
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

So adding the

Serial.print("test");

line makes it run with serial monitor open. Closing it stops the blinking.

You have two problems. The first one is that you need a Serial.print to get it started. This is not normal for a Leonardo or Micro but might be normal for a SparkFun Pro Micro or a clone of that.

Have you observed the behaviour in Windows device manager (or the Linux/Mac equivalents)? What happens after an upload? What happens after a power cycle?

The second one is that it stops at the moment that you close serial monitor; this is normal (at least for a Leonardo). You will need to use Serial.availableForWrite and check if there is space in the TX buffer before you print.

As the Pro Micro isn't an Arduino product but a SparkFun product, you might have a little more luck at SparkFun's forum if you don't come right here.

Note
Your second code in reply #4 is missing a Serial.begin.

sterretje:
You have two problems. The first one is that you need a Serial.print to get it started. This is not normal for a Leonardo or Micro but might be normal for a SparkFun Pro Micro or a clone of that.

Have you observed the behaviour in Windows device manager (or the Linux/Mac equivalents)? What happens after an upload? What happens after a power cycle?

The second one is that it stops at the moment that you close serial monitor; this is normal (at least for a Leonardo). You will need to use Serial.availableForWrite and check if there is space in the TX buffer before you print.

As the Pro Micro isn't an Arduino product but a SparkFun product, you might have a little more luck at SparkFun's forum if you don't come right here.

Note
Your second code in reply #4 is missing a Serial.begin.

Should I buy a different Arduino then?

What should I be observing in Windows device manager?

Will the Serial.begin make it run the blink automatically?

Hi,
Welcome to the forum.

When you load the program, do you have ProMini selected in the "TOOLS", "Boards"?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
Welcome to the forum.

When you load the program, do you have ProMini selected in the "TOOLS", "Boards"?

Thanks.. Tom... :slight_smile:

Thank you.

It only seems to successfully upload with Arduino/Genuine Micro. ProMini doesn't seem to upload.

This is my configuration:

Hi,
This is supposed to be a schematic for the ProMicro, and it does not have a flashing pin13 LED provision.
The 3 LEDS are for power and Tx and Rx serial, so I would say that you are seeing the Tx led blink when you have the Serial comms working.


The component overlay confirms this.
523a1765757b7f5c6e8b4567.png

Tom... :slight_smile:

523a1765757b7f5c6e8b4567.png

vixez:
Should I buy a different Arduino then?

What should I be observing in Windows device manager?

Will the Serial.begin make it run the blink automatically?

I don't know, not familiar with the Pro Micro. See Tom's remark.

In serial monitor, you probably should see it change from ProMicro to ProMicro bootloader when upload starts and back to ProMicro when it's finished.

Serial.begin() is required if you want to print; it was just a remark.

TomGeorge:
Hi,
This is supposed to be a schematic for the ProMicro, and it does not have a flashing pin13 LED provision.
Tom... :slight_smile:

Good catch. Never looked too much into the pro Micro.

How can I make the sketch run immediately after uploading, without the need for a serial port connection, and run immediately when connected to the power?

The pro micro gets into these modes. The way to cure it is to hold down the reset switch while downloading and when it says "loading" in the bar above where you get the feedback release the reset button. It will then load the code. You only need to do this once, it should be fine after that.

I think I have a Pro Micro somewhere. It's a long time since I used it but I will see if I can find it and try it later.

I don't recall any unusual behaviour - it is essentially the same as a Leonardo. I think I did need a Sparkfun core for programming it.

Note that a Pro-Mini is very different from a Pro-Micro.

...R

Pro Micro doesn't have a reset button but you can reset it by connecting the RST pin to a GND pin.

pert:
Pro Micro doesn't have a reset button but you can reset it by connecting the RST pin to a GND pin.

Yes you are right, I remember now I had to wire a reset button up to do this trick. Once it is done it will work correctly.

@TomGeorge Well damn, now I feel stupid haha. I guess I should connect a LED to it then. I only have an IR light available right now, but I guess I can use my camera viewfinder to see if it works.

@sterretje Ah okay, thanks.

How does the reset thing work? Does it just wipe whatever is uploaded to it? Or does it just restart what is uploaded to it?

EDIT: Well, turns out it does work! I connected the IR LED and looked with my camera viewfinder and it's blinking.
Thank you all so much with your quick and on point help.
Learn something new every day :smiley:

Updated code:

// the setup function runs once when you press reset or power the board
 int ledPin = 16; 
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(ledPin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(200);                       // wait for a second
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  delay(200);                       // wait for a second
}

Hi,
Good to hear, I haven't played with that controller but as with all the controllers I buy, I would have tried the blink code as well.

Tom... :slight_smile:

You can always blink one of the onboard RX or TX LEDS on your Pro Micro as an alternative to the missing pin 13 LED:

void setup() {
  pinMode(LED_BUILTIN_RX, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN_RX, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN_RX, LOW);
  delay(1000);
}

Ah great, that will be useful. Thanks!