1

I wrote my custom sketch, where I am printing some greeting text inside setup() method.

I found, that each time I am restarting IDE, and opening serial monitor, a greeting text is printed there again, i.e. setup() method running again.

How can this happen? I was thinking setup() method run once, when Arduino is powered up by connecting to the USB socket?

If it is possible to restart it already connected, then how this can be done?

UPDATE

Code is follows:

#include <Wire.h>
#include <Adafruit_BMP085.h>

Adafruit_BMP085 bmp;

String command;

String awaitingMessage = "Temperature and pressure sensor awaiting commands\r\nCommand \'help\' is for help";

void awaiting() {
  Serial.println(awaitingMessage);  
}

void help() {
  Serial.println("Temperature and pressure sensor commands:");
  Serial.println("temp\t- output temperature in degrees Celsius");
  Serial.println("press\t- output pressure in Pascals");
  Serial.println("wait\t- print awaiting message");
  Serial.println("help\t- output this text");  
}

void temperature() {
  Serial.println(bmp.readTemperature());    
}

void pressure() {
  Serial.println(bmp.readPressure());    
}

void unknown() {
  if( command.length() > 0 ) {
      Serial.println("Unknown command was \'" + command + "'");  
    }
}

void setup() {
   Serial.setTimeout(50000);
  Serial.begin(9600);
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1) {}
  }
  else {
    awaiting();
  }
}

void loop() {
  command = Serial.readStringUntil('\n');

  if( command.startsWith("temp") ) {
    temperature();
  }

  else if( command.startsWith("press") ) {
    pressure();
  }

  else if( command.startsWith("wait") ) {
    awaiting();
  }

  else if( command.startsWith("help") ) {
    help();
  }

  else {
    unknown();
    //awaiting();   
  }

}

Video is here: https://youtu.be/-QBleGVxaus

4

When you open the serial monitor (or even just go to the serial port menu) the serial port is opened. When the serial port is opened the DTR line is asserted. This is the method that the Arduino uses to reset so that you can access the bootloader and upload a new sketch.

Every time you open the serial port - through whatever means you choose - you reset the board, whether you like it or not.

There are a number of "fixes" for this - the simplest is to add a large (say 10µF) capacitor between the RESET and GND pins of the Arduino to filter out the reset pulse that would otherwise reboot the board.

There are also ways of disabling the reset within Windows, but that requires you to write your own software to interface with the board instead of using the serial monitor.

Some boards also come with a method of completely detaching the reset signal from the USB interface, but modifying the board itself isn't for beginners.

Just remember that once you have disabled reset through whatever means you will have to re-enable it (resolder the track you cut, remove the capacitor, whatever) before you can upload a new sketch.

  • So, how to use this for good? How to restart Arduino by means of /dev/ttyUSB0 device? – Dims Oct 12 '15 at 16:32
  • Note, that my Arduino is connected by USB, I don't believe there are some DTR line activating when I "open" the port. Also I don't understand, what is "opening" at all – Dims Oct 12 '15 at 16:35
  • The DTR line is generated by the USB interface chip form the USB protocol's line state data. You can disable reset on Linux with stty -F /dev/ttyUSB0 -hupcl and re-enable it again with stty -F /dev/ttyUSB0 hupcl – Majenko Oct 12 '15 at 16:37
  • I like the reset feature. How to use it for good? Suppose I did /dev/ttyUSB0 hupcl. Now how can I restart arduino? – Dims Oct 12 '15 at 16:38
  • 1
    Yes, reading also opens the port. Any action on the /dev/ttyUSB0 device opens the port - even the stty command used to disable or enable resets. You can't really use it to conserve power unless you put the chip into sleep mode after you have finished what you are doing when a reset would then start your sketch from the start again. It may be usable like that, or not, depending on what you want to achieve. You would always run once regardless, and then sleep until you next open the port. – Majenko Oct 12 '15 at 16:44
-1

This means your Arduino or ATmega328 is restarting due to another issue. Usually the Arduino will reboot when there is too much amperage draw which is called a brown-out.

It can also mean there is a bug in your code.

Please also note as you mentioned

I found, that each time I am restarting IDE, and opening serial monitor, a greeting text is printed there again, i.e. setup() method running again

The Arduino will reboot when opening the serial monitor.

  • How can restart happen exactly when I run IDE? How can "other reasons" know that I am restarting an IDE? – Dims Oct 12 '15 at 16:22
  • Can you post your code? – PhillyNJ Oct 12 '15 at 16:23
  • See my update please – Dims Oct 12 '15 at 16:29
  • See my edit to my answer – PhillyNJ Oct 12 '15 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.