Arduino IDE is the open source software to write code and upload it to the Arduino board. Arduino IDE runs on Windows, Mac OS X, and Linux. The environment is written in Java and based on Processing and other open-source software.
Visit here if you have not installed Arduino IDE.
An Arduino sketch is the name that Arduino uses for a program. It’s the unit of code that is uploaded to and run on an Arduino board. A basic Arduino sketch consists of two functions called setup() and loop(). The purpose of these will be explained later in the tutorial.
Now, open the Arduino IDE and select File > New (or press Control + N on your keyboard) to see the two functions.
We will now create a “Hello World” program in Arduino IDE. We will simply write the text “Hello World” to the screen. We will use serial monitor window, to write our message.
To write in the serial monitor, you have to first initialise serial monitor which you can do with 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 initialising serial monitor and writing print statement inside setup() function. You will end with this code:
Save your sketch by selecting File > Save.
Plug your evive into your computer using a USB cable. Click the Upload button to load the program to the Arduino. Now open the Arduino IDE Serial Monitor Window to see the sketch run and print the text message. The text that the program outputs should be visible in the serial monitor window.
In an Arduino sketch, program statements (individual lines of code) are executed or run from top to bottom. This top-to-bottom execution of statements can only be altered 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 run first. 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 are communicating 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 of the serial 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 run only once, every time that the sketch is run (which you can see in 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, then the second statement, then the first statement again and so on in a loop. Hence, the statements in the main loop will be executed continuously until the Arduino is switched off or reset.
As there are no statements in the loop() function in our hello world example, program execution will end up in the loop and get 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. The microcontroller will try to execute whatever it finds in memory as an instruction, but the loop() function prevents it from doing this by keeping program execution in the loop.
Given below is an example sketch that demonstrate 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 get in your serial monitor:
As the above demonstration shows, the text in the setup() function is only displayed once when the serial monitor window is first opened and 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.
In this tutorial you have learned about the basic structure of Arduino sketches and the flow of execution of program using setup() and loop() function.