Arduino programming tutorial for beginners



Arduino Programming with C Language Our today’s tutorial is about the programming of Arduino board using C language. We will learn how to create a project in Arduino using C language? We will learn basics of Arduino Programming in C language. We will discuss structures i.e. loops used in C language to program Arduino. We will also work on some sample projects created in Arduino IDE using C language. I recommend you to check getting started tutorial on arduino as well.
An Arduino program which is written using C language programming is usually called a sketch. The first thing that we need is Arduino IDE which is available on Arduino official website free of cost. After downloading and installing Arduino IDE. Start the Arduino IDE You will see a window like this. (In this tutorial I am using Arduino 1.8.2).



How to write your first program with arduinio

1. Go to File -> Example -> Basics -> Blink. A new window pops up which looks like a figure shown below. It is a program written using C language.

NOTE: We will modify this C language program so that people who are not familiar with C language programming can understan. Write “int LED = 13; ” without quotes above “void setup ()” statement. And replace all “LED_BUILTIN” wit “LED” without quotes. After modification our C language program at Arduino IDE window looks alike



2. Go to Tools -> Boards on Arduino IDE, and select your board which you are having physically with you. Mine is Arduino UNO.

3. Below the board on menu, you can see Port. Select the port you have connected the Arduino. Mine is COM4.
4. Next click on the tick mark below file menu on Arduino IDE windon. This is compilation button for program. If everything is right with your C language program i.e. if you have followed correct syntax of C language programming, you will see a “Done compiling” dialogue below white part of window.

5. As a next step upload the C language program written in Adruino IDE, to board as follows

6. If C language program is successfully uploaded to Arduino Board, you will see “Done uploading” dialogue below white part of window.
You will see LED blinking on board near digital pin 13 of Arduino Board.
Let’s try to understand the program written in C language in Arduino IDE.
7. “int LED=13; ” defines a variable of type integer with value 13. It is actually pin number to which LED is connected on Arduino board. Be careful about ; it is the syntax (rule) of C language programming to end every line with ;
8. Void setup ()
{ }
Anything placed inside this code will be considered once. The small and curly brackets indicate that it is a function not a variable (as defined in above step) of C language programming.
9. Inside this portion we define pinmode and its number. pinmode could be input or output. LED indicates pin number to which output is connected.

10. Void loop ()
{ }
Anything written inside this will be repeated until board is connected to system.
11. digitalWrite function writes the LED pin with HIGH that is 1
12. delay(1000) waits for next instruction. 1000=1sec in physical word.
13. digitalWrite function writes the LED pin with LOW that is 0.

This was our first C language program in Arduino IDE and it was very basic. Explore some other examples and try to figure out other functions from Arduino official website.

Variables and Data Types in arudino programming in C language

In your sketches, most of the times you want to store data and perform some type of calculation. Counting the number of times a button is pushed, storing the voltage on an analog pin, or performing a complex mathematical calculation with vectors: require data to be calculated and stored. This data is saved in a variable, a memory location that can be changed as required. By declaring a variable, you are asking the compiler to allocate a specific amount of memory, depending on the data typeData types and variables are used to store the information. When we declare variable we are actually telling compiler to allocate a specific amount of memory, depending on data type.

Control Structure in Arduino programming in C Language

if Statement in C Language:
The if statement is used to detect if an expression is equal to a result
Following versions are used.
One is:
if (expression)
{
statement;
}
Two conditions are as follows:
if (expression)
{
do_this;
}
else
{
do_that;
}
We can mix several if else statements using:
if (expression)
{
do_this;
}
else if (expression)
{
do_that;
}
Try to evaluate given program:
int var1 = 42;
if (var1 == 42)
{
run_this; // var1 equals 42; this function will be executed
}
else
{
run_that; //This one will not
}
if (var1 < 50)
{
run_another_function; //This will be run, since 42 is less than 50
}

Switch Statement in Arduino programming in C Language

When we need to check a large number of conditions and need to execute a statement according to a specific condition, we use switch/case statement. It is like if statement. A switch statement the value of variable, and execute a different case statement depending on value.

 switch(button)
 {
 case 1:
 turn_on_lights();
 break;
 case 2:
 if (blinds_up == false)
 {
 raise_blinds();
 blinds_up = true;
 }
 break;
 case 3:
 ----
 }

Break; at the end of each instruction actually tells compiler to stop the execution. If break; is not mentioned it will continue executing case instruction. For example if button 4, 5, 6 do same task you can write

 switch (button)
 {
 case 4:
 case 6:
 case 8:
 //code to be run
 break;
 }

while Loop in Arduino programming in C Language

It is one of the basic loops of C language; it will execute same code again and again until a condition is specified. As long as condition is true it will continue running same code and keep checking the condition at the end of each loop.

while (button == false)
{
button = check_status(pin4);
}

In this example, the function check status runs until it returns true. When that happens, the variable button becomes true, and the while loop will be broken. It might be within a few milliseconds, or the system might wait indefinitely.

for Loop in  Arduino programming in C Language

In cases in which you need a portion of code to loop an exact number of times, the for loop is used. It is similar to while, only it is written differently. The for loop keeps track of the number of times it has run.

 for (expression1; expression2; expression3)
 {
 instructions;
 instructions;
 }

This might look complicated, but don’t worry; it is simple. It requires three expressions:

• expression1 is the initializer; it will initialize a variable.
• expression2 is the conditional expression; as long as this condition is true, the loop keeps on executing.
• expression3 is the modifier; when a loop is completed, this action is performed.

For example:
 for (int i = 0; i < 10; i++)
 {
 myfunc(i);
 }

In this example, a variable is defined with the name i. The variable is set to zero, and each time the function myfunc is run, i is increased by one. Finally, when i reaches 10, the loop stops before running myfunc. This saves you from writing out all the commands one by one like this:
myfunc(0);
myfunc(1);

myfunc(8);
myfunc(9);



Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,395 other subscribers