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).
// 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,
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.
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.
@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.
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
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
}