Space In HIndi

 


Pages

Popular

BASICS OF ARDUINO PROGRAMMING



Every Arduino code is known as “sketch” in Arduino terminology. Arduino programming is similar to C++ programming and many of us already familiar with it. Even if you are not, we will be covering everything from basics. Structure of Arduino Sketch Now open Arduino IDE. After opening it, you would be presented with something similar to below picture. 


This has a very basic structure of Arduino sketch, showing two functions which are needed in every sketch. 


Let us understand these functions one by one.

void setup()

This is the first function which runs when the program execution starts. This function runs only once after the program starts executing. As the name suggest, it does the setup. We need to do peripheral initialization here e.g. pin initialization.

void loop() 

After void setup() this function runs continuously in loop. In this function we need to write code which we want to run again and again e.g. blinking led infinitely.

Functions Function is a piece of code which has collection of executable statements. We have already seen two functions, void setup() and void loop(). 
User can also write their own function to perform certain task. First each function need to be declared and then it could be called to execute. 

Function has following structure, where ‘type’ represents the type of value function will return. After ‘type’ we need to declare name of function. In parenthesis we need to pass ‘Parameters’ to the function. 

type functionName(Parameters) 
Statement; 
Executable statements of function are enclosed in curly braces {}.

 Semicolon ; after each statement represents the end of statement.

// Single line comments 

In single line comments, statement immediately after // represents a comment. This comment ends with the next line in code.

pinMode(pin, mode);

 This function is mainly used in void setup() to declare a pin as output or input. We need to pass two parameters to it, first is the number of pin and second is the mode i.e. INPUT / OUTPUT. 

Ex. pinMode(8, OUTPUT); // Sets pin 8 as output pin

digitalRead(pin); 

This function is used to read data on digital pin. The result of this function is always HIGH / LOW or 0/1.
 Ex. result = digitalRead(4); 

digitalWrite(pin, value);

 This function outputs HIGH / LOW or 0/1 on the pin number mentioned.
 Ex. digitalWrite(3, HIGH); //Sets pin 3 HIGH 

analogRead(pin);

 With this function, we could read data on any one of the analog pin of Arduino. The output value is integer range from 0 to 1023.
 Ex. value = analogRead(A5); 

delay(milliseconds);

 As the name suggest this function gives delay or pause the program for the time mentioned in milliseconds. 
Ex. delay (1000); //Gives delay of 1 second

Serial.begin(rate); 

This function initialize serial port for data transfer. Here ‘rate’ represents the baud rate of data transmission i.e. bits per second. Most commonly used baud rate is 9600, Ex. Serial.begin(9600);

Serial.print(data); 

This function prints data on serial port. When used Serial.println(data); it prints data on Serial port and goes to next line. 
Ex. Serial.print(“Hello”);

No comments: