principle of void setup && void loop

Hello~ I'm arduino beginner in Korea.

My book says 'void setup' is a structure for initialize variables, pin modes, start using libraries, etc. And 'void loop' is a structure for main function.
After read that, I have some wonder, so i tried below codes.

void setup
{
//none
}

void loop
{
  pinMode(13, OUTPUT); //I put it here
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
void setup
{
//none
}

void loop // Delete 'pinMode(13, OUTPUT)'
{
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
void setup
{
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

void loop
{

}

And that codes doesn't make any building errors, and arduino blinks led well. (Last code, arduino blinks LED once, because the codes are in setup)

I miss 'pinMode(13, OUTPUT)' and even make codes in setup structure. But why that code's working?

Does setup structure is made for in purpose(initialize variables, pin modes, start using libraries)? Or just setup structure run once so people use this structure for specific purpose(initialize variables, pin modes, start using libraries)?

ps. sorry about my english skill ^^;;

1 Like

you only need to do pinMode() and Serial.begin... once. void setup()

void setup
{
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

flashes the led once. put it in void lop(), it blinks

So the way it should be done
for your example

you setup the pinMode as input, output or input_pullup for your digital pin

so that goes in setup() because it only has to be done once.

The rest of your code is repeated all the time so it goes in loop()

void setup
{
pinMode(13, OUTPUT); //I put it here
 }

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

Thank you to all who has taken an interest in my problem.
Thank you so much, Delta_G^^

I have one more question about 'pinMode()'.

void setup()
{

}

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

I tried this one and it works too. Does it means, Arduino port's(0~13) default is OUTPUT? Or any else reason it works without 'pinMode()' code?

to. Delta_G

'Default is input'
And that mean, 'pinMode(OO, INPUT)' is unnecessary when i use port for input mode??

Hi jaurim2893,
Welcome to the forum.

I've nothing useful to add to the information you have been given, but, for posting a well thought through question after doing your own experiments with code and for using code tags on your first post ++Karma;

But from the MCU point of view no it's not technically necessary.

Perfectly correct.
But in practice if you are loading one sketch on top of another you could get what you used to treat as an output remains as an output if you don’t specifically set it to an input.

This used to happen with pin 13 which drives the on board LED during start up, but now the bootloader sets it to be an input before it exits so it too defaults to an input.

Think of setup() as a function that establishes the environment in which the application is to run. That is, that function does things that are needed by the program, but not obvious that they've been done to the user (e.g., open the Serial object, create a DB connection, open a printer port, etc.) Often, the statements in setup() are "invisible" to the user. Just like the IDE creates a list of your 10 most-recently opened files, creating that list would be done in the equivalent of setup()...something done to set up the environment for the user in preparation for their use.

While Delta_G and Grumpy_Mike are correct in saying the pinmode() function call is not necessary, I would encourage you to still place it in setup() because it: 1) it documents that you intend to use that pin for input, 2) most readers of your code will look in setup() to see which pins are already in use as that's where most programmers would detail their use.

most readers of your code will look in setup() to see which pins are already in use as that's where most programmers would detail their use.

In conjunction, I would hope, with giving the pins sensible names by declaring variables or #defining names

'Default is input'
And that means, 'pinMode(OO, INPUT)' is unnecessary when i use port for input mode??

if you are OK with assuming that the default will always work, yes. if you are OK with not informing people who rad your code about what you're doing, yes.

if you prefer positive control, rather than assumptions, use pinMode(pin#, INPUT);