What are the Functions of Arduino??
Functions are nothing but a piece of code that is used to
do or perform tasks when we call the functions.
To get more clear let’s take an example -
IRCTC – Indian railway catering and tourism for the corporation.
SIM – Subscriber identity module.
CCTV – Closed-circuit television.
These are some naming conventions, this short form helps us in the day–to–life while writing or speaking rather than using full-form everywhere.
Similarly, function helps us quickly, easy access to use the piece of code anywhere in the program multiple times while reducing the length and complexity of code. If we want to repeat the piece of code in your programming more than once then you just have to call that function it’s just one line of code only.
To use a function we need to first create functions and call that function whenever required.
In the above example, we have created a function named message (), in that function we have written a simple code to print a message “hello this is a message from Arduino”. Now in the void loop, the code itself repeats continuously.
Inside the void loop we have written a code to print a number of series from 0 to 4, we have given condition i<5, and now as soon condition is completed our for loops get breaks and our function is executed. The code inside our function which prints “hello this is a message from Arduino” is run each and every time as this function exists in a void loop.
Now we understand by calling a function we can access it.
Arduino has some in-built functions (Arduino built-in functions)
So let’s see a few of these functions that are commonly used -
1) digitalWrite()
This function takes two parameters such as pin and values
digitalWrite(pin, values);
To execute this function we have to pass
Arguments in it. Arguments are nothing but real, actual values, and passing this
value is called passing arguments.
digitalWrite(11, HIGH/LOW);
Here is pin no 11 and keywords are arguments
here. This function does not return any values. Keyword HIGH means 5V or logic
1 and LOW means 0v or logic 0.
In this example, we write a code to turn the led ON and turn the led OFF with a delay time of 1 sec. as
we discussed earlier this loop continues to repeat so the led will turn on and off again and again. we use keywords HIGH and LOW
to turn ON and OFF led with delay (1000) means a delay of 1 sec.
2) pinMode()
pinMode function is similar to the digitalWrite function
as it takes two parameters just like digitalWrite()
pinMode(pin, mode); Here we pass two arguments
Example - pinMode(ledpin, OUTPUT);
Pin no 13 and word OUTPUT is a keyword
pinMode function is also a built-in function, pinMode functions define
how the pins of Arduino is to work either input or output. INPUT and OUTPUT are
predefined keywords that define the
pins modes also we can enable internal pull-up resistor with INPUT_PULLUP keywords
Input_pullup is a keyword that tells Arduino whether to use the pin as a pull-up
or pull-down through software without any need for an external hardware resister
required
The pin mode
function takes two parameters the first one is the pin that is going to be
used and another is the mode of operations of that pin.
pinMode(pin2, INPUT);
So first in the pin mode function, we define the mode of operation of
pins. When we configure the pin as INPUT we enable that pin to read sensor values
or read a button status if the button is pressed or Not.
PinMode(ledpin, OUTPUT);
When we configure the pin as OUTPUT, means Arduino provide current to other circuit or devices. Examples are LEDs, motors
PinMode(buttonpin, INPUT_PULLUP);
When we configure pins as input to read a
button state whether the button is pressed or not, then you have noticed it is a floating pin. When the switch is not being pressed the value fluctuates between 1
and 0 i.e. high and low because of external interference. So to avoid this we
use an external resistor that acts as a pull-up and pull-down resistor. If you
don’t have a resistor then don’t have to worry, this hard configuration can
be eliminated by using INPUT_PULLUP
Keyword. INPUT_PULLUP keyword is used as an argument in the pin mode function. Now the pin will be high when the button is not pressed and low when the button is pressed.
3) digitalRead();
This function takes only one argument in it. This digital Read function returns value to us. So let’s see
digitalRead(pin);
int sensor_data = digitalRead(11);
Here we can see digital read function returns
value to the variable name sensor_data. What digital Read tells us, what the current
status of Arduino digital pin is, whether it’s high or low? You can see the status
of that pin by just passing a variable name in the Serial.print() function.
In the above example, the digital Read function reads the values of pin no 11 and stores the status in variable name sensor data.
Example – button press, digital temperature
sensors, switches.
4) analogRead();
analogRead(pin); this function takes only one argument in it.
int mydata = analogRead(A0); This function returns value to a variable.
Here you can see the analog read function returns value to variable name sensor_data. What digital Read tells us, what the current status of Arduino analog pin is, it reads analog voltages which are fed to onboard ADC to give value scale ranging from 0 to 1023.
Example – potentiometer, analog temperature sensor, air quality detector.
5) analogWrite();
This function takes two arguments in it. This
analog Write function does not return value to us.
analogWrite(pin , value);
analogWrite(5 , 115);
analog Write is nothing to do with analog pins of Arduino. Analog Write functions make use of Pulse width modulation (PWM) pins which gives accessibility to control & adjust the output power fed to sensors. PWM pins are represented by the (~) symbol. The PWM pins of Arduino are 3, 5,6,9,10,11.
Example – LED dimmer, motor speed control.
6) millis();
int mytime = millis()
millis functions return the current time from
where Arduino turns ON or resets.
millis functions return time in a millisecond to variable my time
We will learn more about millis() function in the upcoming chapter of the Arduino series.
Conclusion -
Today we learn about functions in Arduino programming, why we required functions in programming, we learn about some in-built functions used in Arduino programming, we also learn how to create and call the function in programming.
"I hope you find this lesson on Arduino very helpful to you and understand about functions in Arduino. In the upcoming lesson, we will learn about what is digital Read function till then bye. See you all in my next chapter of Arduino lessons."