Programming the Arduino

Programming or coding the Arduino means to write syntax or logics which can be uploaded to the Arduino and make the Arduino perform the required task. Generally, the code written for Arduino is called Sketch.

Arduino Programs are written in the Arduino IDE and then can be verified and then uploaded into the Arduino Board.

An Arduino program consists of three different parts:

  • Declaration Section
  • Setup function
  • Loop Function

The declaration section is written at the start of the program, which is at the top of the sketch. This section contains declaration of all different variables, Pins, libraries, etc which would be required by the program for execution.

The Setup function is the next part of the Arduino Program. It consists of different assignments. Inside this function, it should be declared which PIN is dedicated to which purpose in the Program.

The Loop Function is the third and the last part of the Arduino Sketch. This part of the program consists of the statements which are the main actions for the Program.

When a new Sketch is Opened, this is what you will see:
arduino-empty-sketch

A Basic Program to print a text

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
    lcd.begin(16, 2);
    lcd.print("chercher.tech");
}
void loop() {
    lcd.setCursor(0, 1);
}

In the above program, #include <LiquidCrystal.h> is the library used. LiquidCrystal lcd(12,11,5,4,3,2) is the Pin declaration.

Next is the void setup() function, where we used functions such as lcd.begin() and lcd.print() to perform our task.

And then we have the void loop() function, which tells that the setup() function should iterate.

Program Output :

arduino-program-demo

The above picture is a demo output. Ignore the code and connections. We shall learn below in detail.

Data Types

Data types are is the type of variables used in Programming. Variables are nothing but a name to a particular memory location. And data is stored inside a variable. A variable can store different data types. Below are the data types in Arduino Programming.

Boolean

This is a data type that can hold either true or false inside it.

Syntax :

bool variable_name=true;
bool variable_name=false;

Implementation Example :

bool status=true;
char

This is a data type that can hold a single character value inside it. This will take one-byte memory and store a character.

Syntax :

Char character_variable_ name= 'character';

Implementation Example :

char letter='a';
unsigned char

This is a data type can encode values from 0-255 to a character.

Syntax :

unsigned char variable_name = 100;

It means, that encode the value 100 to the character variable x.

Implementation Example :

unsigned char letter='e';
byte

This data type can store integer values from 0-255 and the size of this data type is 8 bit.

Syntax :

byte varaible_name = any_integer_between_0_to_255 ;
int

This data type can hold value from -32,678 to 32767. It is a 16 bit(2 byte) sized data type. It is used for direct initialization of variables with integer numbers.

The size of the int data type can vary, that is, the range of the number it can store may vary from board to board.

Syntax :

int variable_name= any_integer;

Implementation Example :

int value=45;
unsigned int

This data type is the same as int, just the difference is that it will not hold negative values. Instead, only 0 to 65535 can be stored. This is generally a 16-bit data type. (May differ with different Arduino Board Variants).

Syntax :

unsigned int variable_name = any_positive_integer;

Implementation Example :

unsigned int value=500;

Implementation Example :

word

This data type stores a 16 bit unsigned integer.

Syntax :

word variable_name= any_unsigned_integer;

Implementation Example :

word value=45;
long

This data type can store values from -2,147,483,648 to 2,147,483,647. It is a 32-bit data type. This is used to store higher values.

Syntax :

long  variable_name= any_integer;

Implementation Example :

long value=65454634;
unsigned long

This data type is of 32-bit size and can store values from 0 to 4,294,967,295.

Syntax :

unsigned long variable_name = any_integer_value;

Implementation Example :

unsigned long value=23454655;

short

This is a 16-bit data type and can store values from -32,768 to 32,767.

Syntax :

short variable_name= any_integer;

Implementation Example :

short value=123;
float

This 32-bit data type can store floating-point numbers.

Syntax :

float variable_name= any_floating_point_number;

Implementation Example :

float float_value=23.33;
double

This data type is the same as the float. Just in some variants of Arduino, the size of the data type can be 64 bit instead of 32 bit.

Syntax :

double variable_name=any_large_floating_number

Implementation Example :

double double_value=233434545.7567567

Variables

Variable is nothing but any name which is assigned to a memory location that can hold values or data. There are three different types of variables based on their scope. The scope of a variable means the region in the program, where the variable is valid.

The three types are:

  • Local Variables
  • Global Variables

Local Variables are the ones whose value cannot be changed outside the function, to which it is declared.

Example :

void loop() {
  int local_value;
}

In the above program the variable local_value is a local variable.

Global variables are the ones whose value cannot be changed throughout the entire program and can be accessible to any function in the program.

Example :

float global_value = 67;
void setup() {
  //some code
}
void loop() {
  //some code
}

In the above example the variable global_value is a global variable.

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions