First Program on Arduino using C

"); }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 codeNow 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() functionThe 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:300120024004800960019200384005760074880115200230400250000I 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.

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() functionThe 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!!.").

That's why, on the serial monitor, we saw that Hello Arduino!.was printing infinitely.

We used!.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 can use Serial.

println("Hello Arduino!"); instead of Serial.

print("Hello Arduino!?.");.

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:Things to rememberWatch your caseC is case sensitive.

So, we need to take care about what we type, or our code won’t compile.

For example, loop() and Loop() are not the same thing in C.

We cannot use serial.

print("Hello Arduino!"); instead of Serial.

print("Hello Arduino!");.

Now look at the following code.

Can you tell where it went wrong?void setup() { Serial.

begin(9600) Serial.

println("Arduino is fun");}void loop() {}Exactly?.On the second line, we missed a semicolon.

Let’s see if our Arduino IDE can detect the error.

We wrote the code in the editor of our Arduino IDE and clicked the verify button (), and found the following result:From this screen, we can see that our Arduino IDE could detect the error.

The log (the black portion of the Arduino IDE) shows where the error occurred.

The log says, expected ';' before 'Serial' , which means we missed a semicolon before Serial.

println("Arduino is fun");.

Adding both Setup() and Loop() functionsYou cannot forget to add either the setup() function or the loop() function to our code.

They must be added to the code.

Say we forgot to add the loop() function.

We will see the following error.

The same error will occur if we miss the setup() function:Minding the baud rateSay I have added Serial.

begin(9600) to our setup function.

Our code will be as follows:void setup() { Serial.

begin(9600); Serial.

println("Today"); Serial.

println("is"); Serial.

println("a"); Serial.

println("wonderful"); Serial.

println("day!"); } void loop() { }Clearly, the output of the code on the serial monitor will be as follows:But if, on the serial monitor, we accidentally change the baud rate to something else, say, 19200, what will happen?.What will the output on the serial monitor be?.The output will be similar to the following screenshot:So, we need to have a look at the baud rate.

If you are not doing any high-speed communication keep both the baud rates at 9600.

Formatting your codeTake a look at the following images.

Which code is more readable, left or right?The code on the right is more readable because it clear-coded.

So, it is necessary to format our code.

We can do it manually.

On Arduino IDE, you can format your code automatically, too.

To do this automatically, we need to go to Tools and select Auto Format.

Your code will be formatted as the code on the right-hand side of the preceding image.

Turning the LED OnIf you have a closer look at your Arduino Uno, you will see a few LEDs integrated to the board.

See the following figure for a better look:There are four LEDs on the board:LED on pin 13 (connected with Arduino pin 13)Power LED (turns on when the Arduino is powered)Rx and Tx LEDs (turn on when Arduino code is uploaded or data is transferred via 0 and 1 Arduino pins)We can program the LED connected with pin 13.

After we upload any code to our board, usually, the LED on pin 13 turns on.

Now we will write code to control the LED.

You can also connect an external LED on pin 13, and a ground pin with a proper resistor, as shown in the following figure.

If you have an external LED connected to the board, it will be clear to you if the LED is being controlled by the code, since the integrated LED is small:Now let’s write our code to turn the LED on.

Write the following code in the Arduino text editor, creating a new file on the Arduino Uno:void setup() { pinMode(13, OUTPUT); } void loop() { }We know that the setup() function holds the variables.

In the preceding code, we have written a pinMode() function, where we have passed two parameters: the pin number, and the result of pinMode.

pinMode(13, OUTPUT); means we have selected pin 13.

So, we can now control anything connected to pin 13.

The pinMode() function is for the digital pins of the Arduino board.

If you are not sure about the digital pins of the Arduino board.

If you want to use any other pins (say, pins 9, 6, 4, and 3), can you guess what you have to write?Exactly.

The pinMode of the pins will be as follows:pinMode(9, OUTPUT); pinMode(6, OUTPUT); pinMode(4, OUTPUT); pinMode(3, OUTPUT);Be careful to write OUTPUT in all capital letters.

Now we will send a signal to pin 13 to turn the LED on.

There are two kinds of signals, LOW and HIGH.

LOW is for sending no signal to the pin, and HIGH is for sending a signal to the pin.

You can use 0 and 1 instead of LOW and HIGH.

Let’s send a HIGH signal to pin 13.

Since pin 13 is a digital pin, we need to send the signal using the following function:digitalWrite(13, HIGH);The digitalWrite() function has two parameters; the first one is pin number and the second one is the pulse, or signal.

It works for only digital pins.

Remember that you cannot use the pin number on this function if you do not write the pinMode() function with the same pin number.

To use a digital pin and give it a signal (either HIGH or LOW), you must write two functions to declare and use the pin.

The pinMode() function is used to declare the pin that we have connected something to, and the digitalWrite() function allows us to send a signal to the defined pin.

If we want to send no signal to the pin 13, we need to write the following function:digitalWrite(13, LOW);We can use 0 and 1 instead of LOW and HIGH.

So, we can write digitalWrite(13, 1) and digitalWrite(13, 0) instead of digitalWrite(13, HIGH) and digitalWrite(13, LOW), respectively.

What should we do if we want to turn the LED on?.I guess you know that already.

Yes, we need to write the digitalWrite() function inside the setup() function, unless we do not want to send the same signal again and again.

So, the code to turn on the LED on pin 13 will be as follows:void setup() { pinMode(13, OUTPUT); digitalWrite(13, HIGH);//Sends a pulse to pin 13 } void loop() { }If you upload the code to the board, you will see that the LED is turned on.

Let’s turn it off now.

You can easily guess what the code to turn our LED off will be, right?It is as follows:void setup() { pinMode(13, OUTPUT); digitalWrite(13, LOW); //Sends no pulse to pin 13 } void loop() { }Have you noticed that we can switch our LED on or off from our code without even touching the LED!. More details

Leave a Reply