Structure of Arduino Code

ArduinoArduino BoardsArduino IDEArduino Programming Language

The Arduino Code has a very simple structure. If you open a new project in the Arduino IDE, you will see the following template prepared for you −


void setup() {
   // put your setup code here, to run once:
void loop() {
   // put your main code here, to run repeatedly:

The comments here explain it well. The setup function contains code that will be executed once. This function is executed first when your board reboots. The loop function contains code that will run repeatedly. In other words, if you write 5 lines of code within loop, once the control reaches the 5th line, it will go back to the first line.

Please note that the setup and loop functions are mandatory for any Arduino sketch. You can't delete either of these. If you do, you'll get a compilation error. You can leave these functions empty if you wish to.

Published on 23-Mar-2021 16:01:46