What is Void setup & Void loop in Arduino??
Void setup and void loop are in-built functions that do not return any value. The main difference between void setup and void loop is that void setup runs only once while void loop continuously repeated constantly. Suppose an example.
Teachers in schools take attendance only once at the beginning of school hours or at the first period of class, she will not take attendance in each and every lecture, then after your regular lectures period goes on and on for a day similarly your void setup function is like your attendance taking teacher and void loop is the period or lecture that will be happening repeatedly and launch break is like a delay function which holds for some time. So let’s see them one by one.
Void setup()
Its start with curly { } - opening bracket & close with closing brackets. The void setup function executes only one time as soon as you upload, power up, or reset Arduino. In void setup, there are three things we have to define.
- Serial.begin()
- pinMode()
Serial.begin()
Serial.begin is a built-in function that passes one argument such as Serial.begin(9600), Serial.begin(115200) .the value 9600 defines baud rate means a number of bits per secs. Serial.begin() command will start a serial communication between Arduino and computer. A connection is being established between Arduino and the computer through a USB cable from which data is being sent or received. The argument which we are passing through this function tells how fast or slow data is to be sent. You can change the value as per requirement but by default, it's 9600. If you are changing the value of the baud rate then please change the value over the serial monitor also otherwise you will not get anything to see on the serial monitor.
Different Baud rate – 2400, 4800, 9600, 14400, 19200, 28800, 38400,
57600, or 115200
Note – please
keep the value of serial.begin and serial monitor same.
pinMode ()
pinMode function is also a built-in function similar to serial.begin(), pinMode() in void setup functions define how the pins of Arduino are to work either input
or output. it is necessary to declare the pin's mode of operations to know the Arduino
whether input or output, the INPUT, and OUTPUT are predefined keywords that define the pin modes
also we can enable internal pull-up resistor with INPUT_PULLUP keywords.
INPUT_PULLUP is a keyword that tells Arduino whether to use the pin as a pull-up
or pull-down through software without any need for an external hardware resistor.
pinMode(pin, mode);
pinMode(pin2, INPUT);
pinMode(ledpin, OUTPUT);
The pin mode
function takes two parameters the first one is the pin that is going to be
used and another is the mode of operations of that pin.
So first in the pin mode function, we define the mode of operation of pins. When we configure the pin as INPUT we enable that pin to read sensor values or read a button status if the button is pressed or Not.
When we configure the pin as OUTPUT, means Arduino provide current to other circuit or devices. Examples are LEDs and motors.
Digital input/output pins are
default set as inputs and hence there is no need to declare as input initially in
the pin mode inside void setup.
pin Mode(pin2, INPUT);
But in the case of declaring pins
at the output, it is mandatory to set the pin as OUTPUT in the pin mode function because of the current limiting resistor therefore it is advisable that one should declare
each and every digital pin of Arduino as input or output in pin mode.
If we have not set pins as OUTPUT in
pin Mode () and we have connected an LED to a pin, then when we call the digital
Write (11, HIGH) function inside the void loop, then the LED may appear dim. So
without configuring pins in pin Mode (), digital Write () enabled the internal pull-up
resistor, which acts like a large current-limiting resistor.
pinMode(pin2, OUTPUT);
It is mandatory to declare each and every digital pin of Arduino as output in pin mode.
pinMode(buttonpin, INPUT_PULLUP);
When we configure pins as input to read a button
state whether the button is pressed or not, then you have noticed it is a floating
pin. When the switch is not being pressed the value fluctuates between 1 and 0 i.e.
high and low because of external interference. So to avoid this we use an external
resistor that acts as a pull-up and pull-down resistor. If you don’t have a
resistor then don’t have to worry, this hard configuration can be eliminated
by using INPUT_PULLUP.
Keyword. INPUT_PULLUP keyword is used as an argument in the pin mode function. Now the pin will be high when the button is not
pressed and low when the button is pressed.
If the Arduino digital pin is configured as INPUT_PULLUP in pin mode, then it will invert the behavior of the INPUT mode, which means HIGH will acts as a button not pressed and LOW will act as a button pressed.
Void loop()
The void loop function takes no argument inside it. Code within Void loops functions repeat consecutively until Arduino is turned off. Whatever we write inside these curly brasses will run again and again. Its start with curly { } opening bracket & close with closing brackets. Whatever will be inside this will run from the top of the first line just after opening the bracket and end before closing the brackets. Whatever you write inside this code will be repeated over and over.
In this
example, we write a code to turn led ON and turn led OFF with a delay time of 1
sec. as we discussed earlier this loop continues to repeat so led will turn on and off again and again. we use keywords HIGH and LOW to turn ON and OFF led with delay (1000) means a delay of 1 sec.
Conclusion
-
Today
we learn about void setup & void loop, we also learn about Serial begin and pin mode function inside void setup and the continuous loop of the void loop.
"I hope you find this lesson on Arduino very helpful to you and understand the void loop and void setup used in Arduino. In the upcoming lesson, we will learn about variables & data types in Arduino till then bye. See you all in my next chapter of Arduino lessons."