What Programming Language is Arduino

Arduino uses programming languages similar to C and C++. To know more about the syntax and semantics of this language, read this article till the end.

All the programs of Arduino are written in Arduino Integrated Development Environment (IDE). Arduino programs are written in Arduino IDE. Arduino programming language is called Processing. Processing is used to write programs to interact with hardware. Processing hardware programming language is similar to C programming language.

To Arduino programming language Arduino IDE is used. The Arduino IDE is used in Windows, Mac OS X, and LINUX platforms.

Basics of Arduino Programming Language

Arduino program has a simple program structure. An Arduino program is developed using two blocks. One block is ‘Preparation’, and the second block is ‘Execution’. Programming instruction within each block is enclosed within curly braces.

void setup( )
{
    Statement -1;
     .
     .
     Statement - n;
}

void loop( )
{
    Statement -1;
    .
    .
    Statement - n;
} 

setup( ) is known as preparation block and loop( ) is known as execution block. 

The block to be executed in the Arduino programming structure is the setup() block.  setup() block is called only one time in the execution of the Arduino programming structure. setup() block is called to initialize the pin modes and begin serial communication. This is the mandatory function to be included in each Arduino programming structure. The setup() block is included even if there exist no programming instructions within the Arduino programming block.

void setup ( ) 

{

    pinMode (pin-number, Output); // this is used to initialize the pin-number as the output.

    pinMode (pin-number, INPUT); //is used to set the PIN as the input.

}

When the setup( ) block is executed successfully, then the execution block is executed. The execution block is executed to read inputs, to trigger output, this block is also used to check conditions etc. 

The execution block contains loop( ) function. The loop( ) function is used to execute the set of programming instructions repeatedly. This loop( ) is similar to loop statements of for loop, while loop, and do-while loop in C programming language.

An example of loop( ) function in Arduino programming language is:

Void loop ( )

{

    digitalWrite(pin-number, HIGH); 

             //this switch ON all the components that is associated to particular pin number. 

    delay(10000); // to specify wait.

    digitalWrite(pin-number, LOW); 

    // this is used to specify the switch to OFF all the components associated with a particular  ‘pin-number’

}

Time duration in Arduino is measured in milliseconds.

Arduino is also used to blink the LED and Fading-in and Fading-out of the LED.

Arduino program works with breadboard circuits. To build an Arduino breadboard circuit following components are required:

  • Arduino UNO R3-1
  • Breadboard-1
  • Breadboard connectors – 3
  • LED -1
  • 1k resistor -1

Arduino Programming Language 

The Arduino programming language can be divided into three main sections: functions, values (consisting of Arduino variables and Arduino constants), and structure.

To work with Arduino boards and carry out required computations following functions are supported by Arduino programming language:

Digital I/O functions:

digitalRead( ): 

Syntax: digitalRead(pin)

Here pin is the PIN to read. This function returns HIGH or LOW.

Example code:

Int led_pin =13; //this connects LED to pin 13
Int in_Pin = 7;   //this connects pushbutton to pin 7 
Int val = 0;       //this is used to save the value read.

void setup( )
{
    pin_Mode(led_Pin, OUTPUT); //it sets the output pin to 13
    pin_Mode(in_Pin, INPUT);      // it sets the input pin to 7
}

void loop( )
{
    val = digitalRead(inPin); // this is used to read the pin
    digitalWrite( led_Pin, val); //this is used to set the button value to LED
}

If the pin is in unconnected mode, the digitalRead( ) can return either HGH or LOW.

digitalWrite( ):

This function is used to assign a HIGH or LOW value to the digital pin. For example, if the PIN is set to OUTPUT using pinMode( ), its voltage is set to 5V or 3.3V. The 3.3V value is used for the 3.3V board for HIGH, and 0V is used for LOW.

If the PIN is set to INPUT, the function digitalWrite( ) will be HIGH or LOW. To set the internal pull-up resistors, the pinMode( ) is set to INPUT_PULLUP.

The LED appears DIM if pinMode() is set to OUTPUT, and the connection of an LED to pin is not done.

Syntax:

digitalWrite(pin, value)

There are two parameters of this function, first, pin, and second, value. The parameter Pin is used to set the PIN, and the parameter value is either HIGH or LOW.

This function returns no value. 

An example program is:

void setup()
{
    pinMode(13, OUTPUT); // this is used to set the OUTPUT to digital pin 13
}
void loop( )
{
   digitalWriter(13, HIGH); // this sets the digital PIN 13 to HIGH
   delay(1000);  //this is used for wait
   digitalWrite(13,LOW); // this is used to set the PIN 13
   dealy(1000); //this is used for wait
}

pinMode()

This function is used to set the PIN either as input or as output. 

Syntax

pinMode(pin, mode)

Parameter of this function is pin and mode. The parameter pin is used to set the pin with the defined PIN. The second parameter mode is INPUT, OUTPUT or INPUT_PULLUP.

This function returns no value.

Example Code:

void setup()
{
    pinMode(13, OUTPUT); //this is used to set the pin 13 to output.
}
void loop( )
{
     digitalWrite(13,HIGH); //setes the PIN 13 to HIGH
     dealy(1000); // this is for delay
     digitalWrite(13, LOW); // this sets the dPIN 13 to off
     delay(1000); // this is used for wait
}

Math functions:

abs( ):

This function is used to calculate the absolute value of a given number. 

Syntax:

abs(x)

This function has one parameter, the number x.  The number x can be greater than 0 or equal to 0. Likewise, the number x can be less than 0.

An example code:

abs(a++); // this form od abs results into incorrect result. 

// correct way of using this function includes

abs(a);
a++; //keep other math outside the function

constrain( ):

This function is used to set the number within a specified range.

Syntax

constrain(x,a,b)

This function has three parameters x,a,b. The parameter x is used to specify allowed data types, parameter a is used to specify the range, and parameter b is used to specify the range’s upper bound.

This function return value x if the value is between a and b, this function returns value an if the value is less than a, and the value b is returned if the value is greater than b.

An example code

sensVal = constrain (sensVal, 10, 150); // this function limits the values between 10 and 150, these values are sensor values.

More Arduino Functions and Syntax

This section provides a brief description and syntax of functions that will be used in Arduino programming.

Arithmetic

Arduino programming is done using math functions. The math functions are given in math.h library. The math.h library is used to perform basic arithmetic. operations such as:

Z = X+Y;

Z = X-Y;

Z = X*Y;

Z = X/Y;

Math.h also supports trigonometric functions.

Arrays

Arrays in Arduino can be used to maintain more than one value. The example of an array is as follows:

int myArrays[5] = {1,2,3,4,5};

The number of items an array can contain is defined in square brackets, and items in the curly brackets specify the items to be kept in the array.

Comparison Operators

Arduino programming language supports greater than, less than, and equal to comparison operators. In Arduino, these programming language supports in the following way:

==  (equal to)

!=   (not equal to)

<     (less than)

>     (greater than)

<=   (less than or equal to)

>=   (greater than or equal to)

FOR

The FOR statement is used to iterate a certain number of statements for a specified number of times and gets terminated when the condition is not met.

For example, to flash LED 10 times following code is used:

int led = 13;

void setup()
{
    pinMode (led, OUTPUT);
    for(int item=0; item<10; item++)
    {
        digitalWrite(led, HIGH);
        delay(1000); 
    }
     Serial.print(item);
     digitalWrite(led, LOW);
    dealy(1000);
    if(item==9)
    {
        break;
    }
}
Void loop()
{
}

For function is set inside the setup( ), this means that for function will be executed one time. If the for function is set inside the loop function, it will iterate from 0 to 9.

The variable integer item will be incremented by 1. The for loop terminates when the condition within the for loop becomes false.

Include

The Arduino program may also require other libraries for the successful execution of the program. To include other library #include pre-processor directory is used.

For example, to include stepper header library following is the technique:

#include<Stepp.h>

Increment/Decrement

An increment/decrement operator is used to increase or decrease the number by one. For example, the operator ++ or — are unary operator and ++ operator increments integer number by one and — operator decreases the number by 1.

Interrupts

Interrupts are used to stop the execution of the loop. Following is the example of the Interrupts.

int led = 13;

Void setup( )
{
    pinMode(led, OUTPUT);
}
Void loop
{
    Int switch = digitalRead(2);
    If (switch1 == HIGH)
    {
           digitalWrite(led, HIGH);
           delay(1000);
           digitalWrite(led, LOW);
           delay(10000);
     }
}

IF/Else

If/Else is used to set conditions, and this condition is evaluated. In that case, if the condition is evaluated to true, action is taken, and if the condition is evaluated to false, then another action is taken.

Following is the example of an if/esle statement using a switch case.

Switch1 == HIGH
{
    digitalWrite(led1, HIGH)
}
else
{
   digitalWrite(led2, HIGH);
}

Mapping

This function maps a number from a specific range to another range. 

Random

This function is used to generate a pseudo-random number, the syntax of the Random function is as follows:

randomNumber = random(min, max); 

The random function gives a number that lies within the range of minimum and maximum values. For example, random(4,11) returns a number between 5 and 11.

Switch/Case 

Switch/Case is used to switch between different options. The switch is done based on the conditions defined in the switch statement. When this condition is evaluated, a particular case is executed.

Following is the switch-case example:

Switch (myVaribale)
{
    Case A:
             Serial.print(“A”);
              Break;
    Case B:
             Serial.print(“B”);
             break;
    Case C:
             Serial. print(“C”)
             break;
}

While

The While function is used to iterate a particular set of programming instructions. The iteration is done until the condition defined in the while loop becomes false.

Following is the example of a while statement:

buttonPress = 0;

while(buttonPress < 10)
{
     buttonpress++;
} 

Resources to learn Arduino programming

Following Books can be used to learn Arduino programming.

  • Arduino Adventures: Escape from Gemini Station: This book provides an introduction to Arduino programming. The book presents Arduino programming concepts interwoven with the science function adventure story. This book is written by James Floyd Kelly and has experience in LEGO robotics, open-source software, and building CNC machines.
  • Arduino Cookbook: To experiment with the Arduino microcontroller and programming environment, Arduino Cookbook is perfect. This book is used to create different types of objects and prototypes that work like toys, that work like detectors, that work like robots, and that work like interactive clothing. This book also provides a detailed section on creating boards that can sense and appropriately deliver responses against touch, sound, position, heat and light. 

This book has detailed practical examples and provide you with guidance that works with the project. In addition, this book has required and essential software concepts.  

  • Getting Started with Arduino: Arduino is open-source. It is a platform that is used to build an electronic prototype. This book provides you with all the information required to build a prototype. In addition, this book provides you with interaction design and the basics of physical computing. This book also gives you the basics of Arduino hardware and essential details related to the software development model. This book also has elaborative details on electricity and electronics, along with the use of solderless breadboards. This book gives you critical use of a schematic diagram. This book advocates the use of Arduino Uno and other semantic models.
  • Make Lego and Arduino Projects: This book has detailed chapters on building Robotics with Bricks, Sensors, and  Microcontrollers. This is an amazing book that delivers chapters on robotics and gadgets. Furthermore, this is a book that easily delivers instructions. In this book, instructions are given to develop devices using Lego Mindstorms NXT 2.0. This book has incredible gadgets. This book has illustrations that provide you with a path that is used to develop breadboards. This book is most liked by students, teachers, makers, and kids. This book helps you create a Drawbot. This book also contains instructions to develop a Gripperbot rolling robotic to create Lego Synthesizer; then explore this book.         
  • Make Things Move: This book is not on Arduino, but it has examples and explanations useful for creating electronics and robotics. This book contains non-technical explanations, collaborative examples, and projects to creates toys. This book contains photographs, best illustrations, useful screenshots, and impressive images of 3D models. This book provides you with fabrication techniques to be used with Arduino materials. This book includes projects that are related to breakfast machines, projects that are related to powered cars, projects that are related to Motor direction and speed control. This book also has illustrative examples on spur gears, animated creations, and illustrations related to the wind turbine.

There also exists websites that are used to build Arduino breadboards. Following are the websites that can be used to build Arduino projects.

  • adafruit.com is a website that can be used to develop electronic projects. The website has useful tutorials and programming examples that can be used to build Arduino projects.
  • Playground.Arduino. cc is a website that has Arduino code. In addition, the website has complete programming examples and experiments.
  • instructables.com is a website that provides topics on electronics and Arduino topics.
  • makezine.com is a website that is used to build a paper magazine.
  • sparkfun.com is a website that has tutorial videos and articles related to electronic resources.

Other Arduino programming language includes ArduBlock, Snap4Arduino, C#, and Python.

ArduBlock is a programming language that has a visual interface. The ArduBlock is used to develop programs that can work with Arduino boards. Snap4Arduino is a visual programming language that operates with the Arduino boards. Snap4Arduino is taken from Snap. Snap-in inherited from Scratch. This is a block-based programming language. This is a dynamic programming language and supports live programming environments.