Topics
Arduino Programming

Arduino Programming

Arduino circuits need to be given instructions on what they need to do just like a computer. These instructions or programs are referred to as Sketches in the Arduino ecosystem. They are written like any other computer program in a high-level language, but there is a difference in their approach. Computer programs are designed to execute and stop instructions in a sequence. They may be restarted based on some external triggers, such as user inputs, or maybe kept running continuously by an operating system process. Sketches are designed to execute instructions in a loop.

Sketches use many of the usual programming constructs found in all high-level languages but may not support some advanced constructs since they are used on Arduino boards whose processing capability is much lesser than typical computers.

The first thing to understand about a Sketch is its structure. Every Arduino Sketch contains two specific functions, such as setup() and loop().

The setup() function is called once when the sketch starts, and it is used to write initialization instructions, which must be executed only once.

The loop() function is called continuously as long as the board is powered on, where instructions for all the functionality expected from the board are written. You need to include both functions in your sketch, even if you don't need them for anything.

When you start a new sketch in the IDE, it opens with the code below:

/*
 * This is the structure of an Arduino sketch. 
 * The instructions in the setup block run only once, 
 * right after the sketch is loaded onto the board.
 * After that the instructions in the loop block run continuously
 * as long as the board is powered on.
 */
 
void setup() {
  // Runs once 
}
 
void loop() {
	// Runs repeatedly
}