Arduino sketch is the name that Arduino uses for a program. It’s the unit of code that is uploaded to and runs on an Arduino board. A basic Arduino sketch consists of two functions:
The purpose of these functions will be explained later in the topic.
For now, open the Arduino IDE and click on the File tab. Then, click on New (or press Control + N on your keyboard) to have a look at the two functions.
In this example, we will write a sketch, i.e. create a program in Arduino IDE that will display the text Hello World on the screen. We will use the serial monitor window to write our message.
To write in the serial monitor, you must first initialize serial monitor, you can do it with the help of the following statement:
Here 9600 is the baud rate at which you are communicating over USB with your device.
The statement to write on Serial Monitor is:
Now, modify the code by initializing serial monitor and writing print statement inside setup() function. You will end with this code:
Save your sketch by going to the File tab and then clicking on Save.
Follow the steps below to run the sketch:
In an Arduino sketch, program statements (individual lines of code) are executed, i.e. run from top to bottom. This top-to-bottom execution of statements can be altered only by flow control statements.
There are few things to notice in your sketch:
In the Hello World sketch, statements in the setup() function will run first, from top to bottom. The statement Serial.begin(9600); is the first statement in the setup() function, so it is the first to run. This statement sets up the speed of the serial port to 9600 baud. The baud setting in the serial monitor window must match this value so that the evive and serial monitor window can communicate at the same speed.
The second statement to run in the setup() function is Serial.println(“Hello, world!”);, which sends the text Hello World out for display in the serial monitor window. In this statement, any text can be put between the opening and closing quotes (” “) and it will be displayed in the serial monitor window.
Statements in the setup() function are executed only once when the sketch is run (which you must have noticed in the Hello World sketch).
Statements in the loop() function will run continuously from top to bottom and then back to the top.
If the loop() function contains two statements, the first statement will be executed first, then the second statement, then the first statement again and so on. Hence, the statements in the main loop will be executed continuously until the Arduino is switched off or reset.
In our Hello World sketch, since there is no statements in the loop() function, program execution ends up in the loop and gets stuck there, doing nothing.
It is important to have the loop() function in the sketch, even if it is empty because without it the microcontroller on the Arduino board will try to execute whatever it finds next in memory after the statements in the setup() function have been executed. But the loop() function prevents it from doing so by keeping the program execution in the loop.
Below is an example sketch that demonstrates the main loop execution. Copy the code to your Arduino IDE, upload the code to your evive, and start Serial Monitor.
This is what you’ll get in the serial monitor:
The text in the setup() function is displayed only once when the serial monitor window is first opened and then the Arduino is reset. After this, program execution enters the loop() function and repeatedly executes the statements in the loop from top to bottom and back to the top again in a never-ending loop.