4 min read

In this article by Syed Omar Faruk Towaha, author of Learning C for Arduino, we will be learning how we can connect our Arduino to our system and we will learn how to write program on the Arduino IDE.

(For more resources related to this topic, see here.)

First connect your A-B cable to your PC and then connect the cable to the PC. Your PC will make a sound which will confirm that the Arduino has connected to the PC.

Now open the Arduino IDE. From menu go to Tools | Board:“Arduino/Genuino Uno”. You can select any board you have bought from the list. See the following screenshot for the list:

You have to select the port on which the Arduino is connected. There are a lot of things you can do to see on which port your Arduino is connected.

Hello Arduino!

Let’s write our first program on the Arduino IDE. Go to File and click on New. A text editor will open with few lines of code. Delete those lines first, and then type the following code:

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.print("Hello Arduino!n");
}

From menu go to Sketch and click Upload. It is a good practice to verify or compile the code before uploading to the Arduino board. To verify or compile the code you need to go to Sketch and click Verify/Compile. You will see a message Done compiling. on the bottom of the IDE if the code is error free. See the following figure for the explanation:

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 on Serial Monitor. You will see the following screen:

setup() function

The setup() function helps to initialize variables, set pin modes, and we can also use libraries here. This function is called first when we compile or upload the whole code. The 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.

On our code we used Serial.begin(9600); which sets the data rate per second for the serial communication. We already 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. I will recommend using 9600 for general data communication purposes. If you use higher baud rate, the characters we print might be broken:

  • 300
  • 1200
  • 2400
  • 4800
  • 9600
  • 19200
  • 38400
  • 57600
  • 74880
  • 115200
  • 230400
  • 250000

If we do not declare the baud rate inside the setup() function there will be no output on the serial monitor.

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

loop() function will let the code run again and again until we disconnect the Arduino.

We have written a print statement on the loop() function which will execute for the infinity time. 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. On the last code we have written Serial.print(“Hello Arduino!n”);. That’s why on the serial monitor we have seen Hello Arduino! had been printing for an infinity time. We have used n after Hello Arduino!.This is called escape sequence. For now, just remember we need to put this after each of our line inside the print statement to break a line and print the next command on the net line.

We can use Serial.println(“Hello Arduino!”); instead of Serial.print(“Hello Arduino!n”);. Both will give the same result.

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 figure. Hello Arduino! is printed for only one time:

Since C is a case sensitive language we need to be careful about the casing. We cannot use serial.print(“Hello Arduino!”); instead of Serial.print(“Hello Arduino!n”);.

Summary

In this article we have learned how to connect the Arduino to our system and uploaded the code to our board. We have learned how the Arduino code work.

If you are new to Arduino this article is most important.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here