Hello Arduino!
Let's write our first program on the Arduino IDE. Go to File
and click New
. A text editor will open with a few lines of code. Delete those lines first. Then, on the editor section, type the following code. Don't worry, the code will be explained later:
void setup() { Serial.begin(9600); } void loop() { Serial.print("Hello Arduino!\n"); }
From the menu, go to Sketch
and click Upload
. It is a good practice to verify/compile the code before uploading it to the Arduino board. There will be a prompt to save the code on your system. Just give it any name you want and save it. To verify/compile the code, you need to go to Sketch
and click Verify/Compile
. You will see the message Done
Compiling
on the bottom of the IDE if the code is error-free. See the following screenshot:
After the successful upload, you need to open the Serial Monitor of the Arduino IDE. To open the Serial Monitor, you need to go to Tools
and click Serial
Monitor
. You will see the following screen:
Dissecting our first code
Now let's discuss how the code compiled on the board and the structure of the code.
The basic structure of the code is as follows:
void setup() { } void loop() { }
There are two functions in our code. The setup()
function and the loop()
function. Every Arduino code will have these two functions. Let's get to the functions better.
setup() function
The setup()
function helps to initialize variables and set pin modes, and we can also use libraries here. This function is called first when we compile/upload the whole code. setup()
runs only for the first time it is uploaded to the board and later, it runs every time we press the Reset button on the Arduino.
In our code, we used Serial.begin(9600
), which sets the data rate per second for the serial communication. We know that serial communication is the process by which we send data one bit at a time over a communication channel. For Arduino to communicate with the computer, we used the data rate 9600, which is a standard data rate.
This is also known as baud rate. We can set the baud rate any of the following, depending on the connection speed and type:
- 300
- 1200
- 2400
- 4800
- 9600
- 19200
- 38400
- 57600
- 74880
- 115200
- 230400
- 250000
I will recommend using 9600 for general data-communication purposes. If you use a higher baud rate, the characters we print might be broken, because our Arduino might not be speedy enough to process the bits at a higher baud rate.
If we do not declare the baud rate inside the setup()
function, there will be no output on the serial monitor.
Note
Baud rate 9600 means 960 characters per second. This is because, in serial communication, you need to transmit one start bit, eight data bits, and one stop bit, for a total of 10 bits at a speed of 9600 bits per second.
loop() function
The loop()
function will let the code run again and again, until we disconnect the Arduino from the power source.
We have written a print statement inside the loop()
function, which will execute infinitely. To print something on the serial monitor, we need to write the following line:
Serial.print("Anything We Want To Print");
Between the quotations, we can write anything we want to print. In the preceding code, we have written Serial.print("Hello Arduino!\n")
. That's why, on the serial monitor, we saw that Hello Arduino!
was printing infinitely. We used \n
after Hello Arduino!
This is called the escape sequence. For now, just remember we need to put this after each line inside the print statement to break a line and print the next command on the next line. We will learn more about the escape sequence in Chapter 3, Exploring C with Arduino.
Note
We can use Serial.println("Hello Arduino!");
instead of Serial.print("Hello Arduino!\n");
. Both will give the same result.
Now, what do we need to do if we want to print Hello Arduino! only once?
Yes, you are right. We need to put Serial.println("Hello Arduino!")
inside the setup()
function. Now let's see what happens if we put a print statement inside the setup()
function. Have a look at the following screenshot. Hello Arduino!
is printed only once: