We use the term ‘sketch’ when referring to programs written for Arduino. A sketch is a unit of code that can be uploaded to, and run on an Arduino board (doc.arduino.cc). As such, understanding the components of a sketch is a must if you are going to be writing code for Arduino.
Moreover, grasping the fundamentals of a sketch is helpful if you are going to be studying sketches written by other members of the Arduino community.
Nevertheless, sketches are unbelievably simple to understand, as you will soon see. Also, There are only two components that every sketch must have. These are explained below.
Components of an Arduino sketch
As mentioned earlier, there are two functions that every Arduino sketch must have. These functions are
You maybe happy to know that your Arduino IDE will provide you with these functions(as shown in the image above) every time you create a new project.
But what are these functions used for anyway?
The purpose of the setup() and loop() functions in an Arduino sketch
The setup function is an ideal place to put code that our program needs to run before it can safely move on to executing the rest of our code. The setup function is called once at the start of the sketch. This makes the setup() function a good place put code that we want to run only once at the start of the the sketch. An example of code normally found in the setup() function is shown below.
The code above tells Arduino that our sketch needs the GPIO pin 1 to be set to OUTPUT mode. In some sketches, we only need to do this setup only once. And in such cases, it would make sense to put this code in the setup() function.
The loop() function is the core of a sketch. This is because, in contrast to the setup() function, the
loop() function is called over and over again. For as long as our Arduino board is powered on. As a result, we are normally going to be putting into the loop() function, code that we need Arduino to repeatedly execute.
For instance, suppose we want our Arduino to continuously blink an LED. One way of achieving this, is to put the blinking LED code into the loop() function. That way, our ‘blink’ code will be continuously executed every time the loop() function is called.
setup() function is run only once at the start of our sketch. Hence, we normally would want to put into the setup() function, code that we need to execute once before executing code in the remainder of our sketch. The
loop() function runs continuously (in a loop, hence the name) . As a result, we would put code that we want to run continuously, into the loop() function .
setup() function and
loop() function should always be included in all of your Arduino sketches, regardless of whether or not you actually put any code in them. Luckily the Arduino IDE provides us with blank setup() and loop() functions every time we start a new project.
Later on, when we start looking at timers and interrupts. You will see that there are actually better ways of implementing repetitive task in our Arduino, as compared to using the loop() function. But that is a subject for another day.
If you found this information helpful please give it a like and/or a share to help other budding innovators find it. See you next time.