Introduction to ARDUINO
Arduino is an open source electronics platform accompanied with a hardware and software to
design, develop and test complex prototypes & products.
The hardware consist of a micro-controllers with other electronics components which can be
programmed using the software to do almost any task.
Arduino was created in the year 2005 by two Italian engineers David Cuartielles & Massimo Benzi.
Introduction to ARDUINO
To program the Arduino we require the Arduino IDE Software that runs on regular personal
computer & allow user to program the board using C or C++.
Arduino shields are pre-built circuit boards that are easily plugged on top of your Arduino headers to
extend its capabilities.
Shields have various functionalities from wifi connectivity,ethernet,driving and controlling
motors,camera storage,touch screen,display and many more.
Adv/Disadv of Arduino
It is an open source.
It has large community of people using and troubleshooting it.This makes it easy to help in
debugging projects.
Arduino IDE is cross-platform which means you can run it on Windows,MacintoshOSX,Linux etc.
Disadvantages:
Requires effort to accomplish some tasks such as scheduling and database storage.
If you need more processing power and working memory than you have to switch from Arduino
environment.
Types of Arduino Board
Arduino UNO:Arduino is a single-board microcontroller meant to make the application more
accessible which are interactive objects and its surroundings.
. The hardware features with an open-source hardware board designed around an 8-bit Atmel AVR
microcontroller or a 32-bit Atmel ARM.
Current models consists a USB interface, 6 analog input pins and 14 digital I/O pins that allows the
user to attach
Real time applications of Arduino
Arduino Based Home Automation System
The project is designed by using Arduino uno board for the development of home automation
system with Bluetooth which is remotely controlled and operated by an Android OS smart phone.
Houses are becoming smarter and well developed by using such kind of advanced technologies.
Modern houses are gradually increasing the way of design by shifting to centralized control system
with remote controlled switches instead of conventional switches.
Real time applications of Arduino
Arduino based Auto Intensity Control of Street Lights
This system overcomes this problem by controlling the intensity of LED lights on street by gradually
reducing intensity by controlling the voltage applied to these lamps.
This system uses arduino board to produce PWM pulses and it is programmed in such a way that it
decreases the voltage applied to these lamps gradually till late nights and completely shutdowns at
morning.
Types of Arduino Board
Arduino Leonardo: Arduino Leonardo is another entry level board that uses the ATmega32u4
microcontroller.
It is Arduino?s first development board to use one microcontroller with built-in USB.
Types of Arduino Board
Arduino 101:
The Arduino 101 is simple and has 32-bit Intel Curie microcontroller.
It?s a board that combines the universal appeal of Arduino with the latest technologies ?like the Intel
Curie module,Bluetooth LE capabilities,& a 6-axis accelerometer.
Types of Arduino Board
Arduino ESPLORA:
The Arduino ESPLORA is an Arduino Leonardo based board with integrated sensors and actuators.
The Esplora has built-in USB communication;it can appear to be connected as a mouse or
keyboard,in addition to a virtual COM port.
Types of Arduino Board
Arduino Mini :
It is the smallest Arduino product which is a 24 ?pin microcontroller without any connectors
soldered.
The unit features 8 analog pins and 14 digital pins.The module is based around ATMEGA168
processor.
Types of Arduino Board
Arduino Micro:
Arduino micro is the smallest board of the family,used for interactive computing.
The Micro comes with a built-in USB which makes connection with a computer.
The microcontroller board is based on the Atmega32U4.
Types of Arduino Board
Arduino Nano:
It is a breadboard friendly board based on the ATmega328.
The Nano was the first mini breadboard-compatible board to have onboard USB.
This microcontroller distinguishes itself from the others by having the USB to serial chip and
connector on board.
Types of Arduino Board
Arduino Mega:
It is based on Atmega 2560 microcontroller.
It has more digital input/output pins,analog inputs ,a USB connection,a power jack,and a reset
button.
The large no.of pins make this board very handy for projects that require a bunch of digital inputs or
outputs.
Powering Arduino UNO
USB power:
USB port is used to connect the board to your PC for programming and powering up the Arduino
board.
The USB connection is important as it will be through this port where you will upload your code onto
your Arduino board.
DC Power Jack:
The DC power jack allows your Arduino board to be powered from a wall adapter so that you can
supply more power to your project if needed.
Arduino IDE
Arduino Integrated Development Environment(IDE) is an open ended software that is mainly used to
write and compile the code into Arduino Module.
The program code that is written for Arduino is called a sketch.
It is easily available for operating system like MAC,Windows,Linux and runs on Java platform.
This IDE environment supports both C &C++ language.
Features of Arduino IDE
The project file or sketches for a project are saved with the file extension.ino.
Features such as cut/copy/paste are supported in IDE.
The most basic part of the skeleton will have two fucntions:Void setup() and void loop().
Installing Arduino IDE
Click on the download button for the particular OS the user is using.
Once the exe file is generated click on that which will display Arduino Setup License Agreement.
After agreement we have to choose the installation folder.
Parts of Arduino IDE
The Arduino IDE is the main text editing program used for Arduino programming.
It is where the programmer has to type the codes before uploading the board.
The IDE environment is mainly distributed into three parts:
Menu Bar
Text Editor
Output pane.
Output Pane
The bottom of the main screen is described as an Output Pane that mainly highlights the complaint
status of running code;the memory used by the code,and errors occurred in the program.
Errors need to be fixed in Arduino module.
Built-in data types
Data type-:The kind of data that the variable may hold in a programming language is referred to as
data types.
There are various types of data such as integer,character,string,real etc.
A character variable occupies one byte of space in memory in which the character constant such as
?b? or ?x? can be stored.
The type name for a character is char.
For example char x;
Built-in data types
Int is another type of numerical variable & stores positive or negative numbers.For example: int
y=10;
To store a fraction,you need a floating point type of data variable,called data type float. Float
x=3.333;
Const int y=10;//Used with reference to global declaration
Signed variables can have both positive and negative value,so you can store negative numbers.
Keyword Void
The void keyword is used only in function declaration.
It indicates that the function is expected to return no information to the function from which it was
called.
void loop()
{ rest of the code
Void setup()
{ initializing the devices/sensors}
Different types of variables
Boolean: A Boolean holds one of two values:true or false.
Each Boolean variable occupies one byte of memory.
Boolean val=false;
Unsigned char:An unsigned char is an unsigned data type that occupies one byte of memory.The
unsigned char data type encodes from 0 to 255.
Byte: A byte stores an 8-bit unsigned number,from 0 to 255.
byte m=25;
Different types of variables
Int: Integers are the primary data-type for number storage.
Int stores a 16-bit value.
This yields a range of -32768 to 32767.
Int counter=32;
Word:A word can store an unsigned number of at least 16 bits.(0 to 65535)
Word w=1000;
Long: The long variables are extended size variables for number storage and store 32 bits.It ranges
from -2,147,483,648 to 2,147,483,647.
Long velocity=102346;
Different types of variables
Short: A short is a 16-bit data type.
On all Arduinos,a short stores a 16-bit value.
This yields a range of -32768 to 32767.
short val=15;
Double : Double precision floating point number occupies four bytes.
double mnum=45.352;
String: It generally contains a particular class of characters.
String x=?hello class?;
Different types of variables
Character strings:Character are written in single quotes whereas strings are written in double
quotes.
char str2[6]={?h?, ?e?, ?l?, ?l?, ?o?};
Array: An array is a collection of variables that are accessed with an index number.
Size_t is a data type cable of representing the size of any object in bytes.
Variable Scope
The scope of a variable refers to the extent to which different parts of a program have access to the
variable-in other words the variable is visible.
Inside a function or a block,which is called local variables.
In the definition of function parameters,which is called formal parameters.
Outside of all functions,which is called global variables.
Variable Scope
Local Variables:
Void setup()
void loop()
{ int x,y;
int z;//Local variable declaration
x=0;y=0;z=10;
}
Global variables
Global variables are defined outside of all the functions,usually at the top of the program.
The global variables will hold their value throughout the life-time of your program.
The variables declared prior to the void setup function is called global variables.
Float c=0;//Global variable
Void setup(){ }
Void loop{ }
PREPROCESSOR DIRECTIVE
A preprocessor directive in Embedded C is an indication to the compiler that it must look in to this
file for symbols that are not defined in the program.
In Embedded C programming,we usually use the preprocessor directive to indicate a header file
specific to the microcontroller ,which contains all the SFR?s and bits in those SFR?s.
In Embedded C, preprocessor directives are usually represented using #include or #define.
For example:#include<LiquidCrystal.h>
#include<Keypad.h>
#define directive
The #define directive determine the identifier & sequence of characters that is substituted for the
identifier,each time it appears for the text of the program.
These statements will not end with semicolon.
For example:#define LED_PIN 13
In a given program whereever the LED_PIN appears it will be substituted by 13.
CLASSIFICATION OF FUNCTIONS
There are two required functions in an Arduino sketch or a program i.e. setup() and loop().Other
functions must be created outside the brackets of these two functions.
Function are of two types:
1.Built-in function or Library function
2.User-defined function.
Standard library of C function in Arduino IDE
Standard library of C function in Arduino IDE
Digital I/0:digitalRead() reads the value from a digital pin.Accepts a pin number as a parameter,and
returns the HIGH or LOW constant.
digitalWrite() writes a HIGH or LOW value to a digital output pin.You pass the pin number and HIGH
or LOW as parameters.
pinMode() sets a pin to be an input or an output.
Analog I/O:analogRead()-reads the value from an analog pin.The name of the analog input pin to
read from (A0 to A5 on most boards).
analogReference()-configures the value used for the top input range in the analog input,by default
5Vor 3.3V
Standard library of C function in Arduino IDE
analogWrite()-writes an analog value to a pin.Can be used to light a Led at varying brightness or
drive a motor at various speeds.
analogReadResolution() -lets you change the analog bits resolution for analogRead() by default
10bits.
analogWriteResolution() -lets you change the analog bits resolution for analogWrite() by default
10bits.
Standard library of C function in Arduino IDE
Time functions-:
delay()-pauses the program for a number of miiliseconds specified as parameter.
delayMicroseconds()-pauses the program for a no. of microseconds specified as parameter.
micros()- the no. of microseconds since the start of the program.
Standard library of C function in Arduino IDE
Math functions:
Abs()-calculates the absolute value of a number
Constrain()-constrains a no. to be within a range
Max()-Calculates the maximum of two nos.
Min()-calculates the minimum of two nos.
Pow()-Calculates the value of a number raised to a power.
Sqrt()-Calculates the squareroot of a number.
Standard library of C function in Arduino IDE
Communication
Serial.begin()- Everything begins with the serial.begin function.It accepts the parameter inside the
parenthesis as baud rate.It is always written in the setup function and executed only once.
Serial.print()-To write something serial output .
Serial.println()-To write a printing statement in the next line.
Standard library of C function in Arduino IDE
Advanced I/o
pulseIn()-reads a pulse(either HIGH or LOW) on a pin.
shiftIn()-shifts in a byte of data one bit at a time.Starts from either the most or least significant bit.
shiftOut()-shifts out a data one bit at a time.Starts from either the most or least significant bit.
tone()-sends a square wave on a pin,used for buzzers/speakers to play tones.
Notone()-stops the tone() generated wave on a pin.
Standard library of C function in Arduino IDE
String functions
String()-defines a string object.
compareTo()-compares two strings.
Length()-finds the no. of character in the string,excluding the null character.
replace()-replaces one given character with another given character.
Substring()-finds a substring within a string.
User-defined function
The function that is defined to perform a set of particular operation is called user-defined function.
Types of User-defined function:
These types are based on the formal arguments or parameters passed to the functions and usage
of return statement.
Types of user-defined function:
A function is invoked or called without passing any formal argument from the calling portion of a
program and also the function does not return any value to the calling function.
User-defined function
A function is invoked by the calling program and formal arguments are passed from the calling
program but the program does not return any values to the calling program.
A function is invoked with formal arguments from the calling portion of a program nand the function
returns value to the calling program.
analogRead() function
Whenever we are using an analog sensor,we have to read this type of sensor,we need a different
type of pin.
In the lower- right part of Arduino board,we will see six pins marked ?Analog In?.
These special pins not only tell whether there is voltage applied to them,but also its value.
By using the analogRead() function,we can read the voltage applied to one of the pins.
analogRead() function
By using the analogRead() function,you can read the voltage applied to one of the pins.
Syntax-analogRead(pin);
int analogPin=3;
int val=0;
void setup()
{ Serial.begin(9600);
void loop()
{ val=analogRead(analogPin);
Serial.println(val);
}
Pulse Width Modulation(PWM)
Pulse width modulation or PWM is a common technique used to vary width of the pulses in a pulse
train.
PWM has many applications such as controlling servos and speed controllers,limiting the effective
power of motors and LED?s.
Basic principle of PWM:
On-time:Duration of time signal is high.
Off-time:Duration of time signal is low.
Period :It is represented as sum of off-time and on-time of PWM signal.
Duty-Cycle:It is represented as the percentage of time signal that remains on during the period of
PWM signal.
Light Dependent Resistor(Photoresistor)
A photoresistor (also known as a light-dependent resistor, LDR, or photo-conductive cell) is a
passive component that decreases resistance with respect to receiving luminosity (light) on the
component's sensitive surface.
The resistance of a photoresistor decreases with increase in incident l intensity; in other words, it
exhibits photoconductivity.
Light Dependent Resistor(Photoresistor)
A photoresistor can be applied in light-sensitive detector circuits and light-activated and
dark-activated switching circuits acting as a resistance semiconductor.
In the dark, a photoresistor can have a resistance as high as several megaohms (M?), while in the
light, a photoresistor can have a resistance as low as a few hundred ohms.
If incident light on a photoresistor exceeds a certain frequency, photons absorbed by the
semiconductor give bound electrons enough energy to jump into the conduction band.
Connection of LDR with Arduino
OPERATOR
An operator operates on variables and perform an action in a program,
It consist of words or symbols.
For instance ,the arithmetic operator(+) &(-) cause us to add or subtract two numbers effectively.
Operators in C language may be classified as:
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Pointer operators
Special operators
Arithmetic operators
An arithmetic operator is a symbol which performs an arithmetic operation namely,addition
,subtraction etc.
The data on which these operations are carried out may be a variable or constant.
The operators thus operates on an operand.
Sum=a+b;
Arithmetic operators
Subtraction:The subtraction operator subtracts one number from another using minus sign(-).
For example
int x=7,y=2,sub=x-y;
After execution of program,the sub will contain the value 5.
Multiplication:Multiplication is done by using the multiplication operator(*).
For example: int x=7;
Int y=2;
Int multi;
Multi=x*y;
Serial.print(multi);
Arithmetic operators
Division: The division operator(/) is used to perform division in the Arduino.For example:
Int x=7;int y=2;
Int div;
div=x/y;
The div will contain the value 3;
float x=7.0;
Float y=2.0;
Float divi;
Divi=x/y; the result will be 3.5
Remainder(Modulo division)
The remainder operator (or modulo operator) is used to find the remainder after the division of two
numbers.
The percentage sign(%) is used as the modulo operator.
For example:
Int x=7;
Int y=2;
Int remain;
Remain =x%y;
Remain value will be equal to 1.
Increament and decreament operator
The increament and decreament operators are represented by ++ and ? operators.
For example:there are two cases in increament:++x andx++.
Let x=2;
y=++x;//x now contains 3,y contains 3
y=x++//x contains 4,but y still contains 3
For example:there are two cases in decreament:--x andx--.
x=2;
Y=--x//x now contains 1,y contains 1
Y=x--//x contains 0 and y still contains 1
Connection of push button with Arduino
EXPRESSION
An expression is a combination of operators,numbers and names that work together to carry out the
computation.
The following are some of the examples demonstrating expressions:
5+j;//Constant plus a variable
5*j +6//Constant times a variable plus a constant value 6
&- Bitwise AND
||-Bitwise OR
^-Bitwise XOR
>>-Right shift
<<-Left shift.
Constant & Literals
Constant is a value,written into a program instruction,that does not change during the execution of a
program.
A Literal constant is a value that is typed directly into a source code,whenever it is needed.
For example:name may be assigned to data item
There are three types of constants:
String constant
Numeric constant
Character constant
String Constant
A string constant or literal is a sequence of alphanumeric characters enclosed in double quotation
marks.
The maximum length of a string constant is limited to 255 characters.
Each string constant is automatically added with a terminating character.
Syntax:const char message1[]=?Press any key?;
Numeric Constant
Numeric constant has a constant value in numbers.
The value of the constant can be positive or negative numeral.
These are integer constant,floating-point constant,octal constant and Hex-constant.
Integer constant:Integer constants are whole numbers(0,1,-1,2,-2,3,-3).
Const.int minimum=-100;
const float T_max=32.60;
Octal numbers are integer numbers with a base 8.(0 to 7)
Hexadecimal numbers are integer numbers with a base 16.(0 to 9 and A to F).
Character constant
Character constant is either a single alphabet or a single digit,or a single special symbol enclosed
within a pair of single quotation mark.
Syntax:Const char I_CLASS=?A?;
The const keyword stands for constant.It is a variable qualifier that modifies the behavior of
variable,making a variable ?read-only?.
This means the variable can be used just as many other variable of its type,but its value cannot be
changed.
Conditional statements and loops
Control statements allow you to change the sequence of instructions for execution.
Control statements are of the following two types:
Conditional branching,a program is to decide whether or not to execute next statements based on
the resultant value of the current expression.
If statement:
if (statement)
{statement(s);}
Conditional statements and loops
if (expression)
{ statement1}
else
{statement2}
If the expression within the paranthesis is true, then thye statement will be executed otherwise the
else condition will be executed.
Conditional statements and loops
Conditional operator(?:)-The conditional operator helps in building a simple conditional expression in
the following form:
Variable=expression1?expression_2:expression_3;
Nested if ?else statement: Specifying the several if condition and choosing one of them.
If (condition1)
statement1;
Else if (condition2)
statement2;
else if (condition3)
statement3;