0% found this document useful (0 votes)
5 views37 pages

Unit 2

The document discusses Machine-to-Machine (M2M) communication, its architecture, and differences with the Internet of Things (IoT), emphasizing the protocols, data handling, and application environments. It also introduces Arduino programming, detailing the hardware components, programming structure, and examples of coding for basic electronic projects. Additionally, it covers serial communication in Arduino, including initialization and data transmission methods.

Uploaded by

21p61a6601
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views37 pages

Unit 2

The document discusses Machine-to-Machine (M2M) communication, its architecture, and differences with the Internet of Things (IoT), emphasizing the protocols, data handling, and application environments. It also introduces Arduino programming, detailing the hardware components, programming structure, and examples of coding for basic electronic projects. Additionally, it covers serial communication in Arduino, including initialization and data transmission methods.

Uploaded by

21p61a6601
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

UNIT-II

IoT and M2M


M2M:
Machine-to-Machine (M2M) refers to networking of machines(or devices) for the purpose
of remote monitoring and control and dataexchange.
 Term which is often synonymous with IoT is Machine-to-Machine (M2M).
 IoT and M2M are often usedinterchangeably.
Fig. Shows the end-to-end architecture of M2M systems comprises of M2M area networks,
communication networks and application fomain.

 An M2M area network comprises of machines( or M2M nodes) whiach have embedded
network modules for sensing, actuation and communicating various communiction
protocols can be used for M2M LAN such as ZigBee, Bluetooth, M-bus, Wireless M-Bus
etc., These protocols provide connectivity between M2M nodes within an M2M area
network.
 The communication network provides connectivity to remote M2M area networks. The
communication network provides connectivity to remote M2M area network. The
communication networkcan use either wired or wireless network(IP based). While the
M2M are networks use either properietorary or non-IP baed communication protocols,
the communication network uses IP-based network. Since non-IP based protocols are
used within M2M area network, the M2M nodes within one network cannot
communicate with nodes in an externalnetwork.
 To enable the communication between remote M2M are network, M2M gateways are
used.
Fig. Shows a block diagram of an M2M gateway. The communication between M2M nodes and
the M2M gateway is based on the communication protocols which are naive to the M2M are
network. M2M gateway performs protocol translations to enable Ip-connectivity for M2M are
networks. M2M gateway acts as a proxy performing translations from/to native protocols to/from
Internet Protocol(IP). With an M2M gateway, each mode in an M2M area network appears as a
virtualized node for external M2M area networks.

Differences between IoT and M2M


1) Communication Protocols:
 Commonly uses M2M protocols include ZigBee, Bluetooth, ModBus, M-Bus,
WirelessM-Bustec.,
 In IoT uses HTTP, CoAP, WebSocket, MQTT,XMPP,DDS,AMQPetc.,
2) Machines in M2M Vs Things inIoT:
 Machines in M2M will be homogenous whereas Things in IoT will be
heterogeneous.
3) Hardware Vs SoftwareEmphasis:
 the emphasis of M2M is more on hardware with embedded modules, the emphasis
of IoT is more onsoftware.
4) Data Collection &Analysis
 M2M data is collected in point solutions and often in on-premises storage
infrastructure.
 The data in IoT is collected in the cloud (can be public, private or
hybrid cloud).

5) Applications
 M2M data is collected in point solutions and can be accessed by on-premises
applications such as diagnosis applications, service management applications, and
on- premisis enterpriseapplications.
 IoT data is collected in the cloud and can be accessed by cloud applications such
as analytics applications, enterprise applications, remote diagnosis and
management applications,etc.

SDN and NVF for IoT


Software Defined Networking(SDN):
• Software-DefinedNetworking (SDN) isanetworking architecture that separates the
control plane from the data plane and centralizes the networkcontroller.
• Software-based SDN controllers maintain a unified view of thenetwork
• Theunderlying infrastructure in SDN uses simple packet forwarding hardware as
opposed to specialized hardware in conventionalnetworks.
Interoperability in IoT
Internet of Things (IoT) is an ever-growing network of physical devices embedded with sensors,
actuators, and wireless connectivity to communicate and share their information among themselves.
The application of IoT is in diverse areas such as agriculture, poultry and farming, smart city, and
health care, where a sensor node must support heterogeneous sensors/actuators, and varying types
of wireless connectivity. Interoperability is the ability of two or more devices, systems, platforms or
networks to work in conjunction. Interoperability enables communication between heterogeneous
devices or system in order to achieve a common goal. However, the current devices and systems are
fragmented with respect to the communication technologies, protocols, and data formats. This
diversity makes it difficult for devices and systems in the IoT network to communicate and share
their data with one another. The utility of IoT network is limited by the lack of interoperability.

Introduction to Arduino Programming


Prerequisite
The requirement to learn Arduino is the basic knowledge of C and C++ programming language. A
basic understanding of circuits, Microcontrollers, and Electronics is also essential.

Arduino is a software as well as hardware platform that helps in making electronic projects. It is an
open-source platform and has a variety of controllers and microprocessors. There are various types
of Arduino boards used for various purposes.
The Arduino is a single circuit board, which consists of different interfaces or parts. The board
consists of the set of digital and analog pins that are used to connect various devices and
components, which we want to use for the functioning of the electronic devices.
Most of the Arduino consists of 14 digital I/O pins.
The analog pins in Arduino are mostly useful for fine-grained control. The pins in the Arduino board
are arranged in a specific pattern. The other devices on the Arduino board are USB port, small
components (voltage regulator or oscillator), microcontroller, power connector, etc.
Features:
The features of Arduino are listed below:
o Arduino programming is a simplified version of C++, which makes the learning process easy.
o The Arduino IDE is used to control the functions of boards. It further sends the set of
specifications to the microcontroller.
o Arduino does not need an extra board or piece to load new code.
o Arduino can read analog and digital input signals.
o The hardware and software platform is easy to use and implement.
Arduino Boards:
There are variety of Arduino board used for different purposes. The board varies in I/O pins, size,
etc. The various components present on the Arduino boards are Microcontroller, Digital
Input/Output pins, USB Interface and Connector, Analog Pins, Reset Button, Power button, LED's,

Core Concepts of IoT By Dr. V. Deepika - 1-


Crystal Oscillator, and Voltage Regulator. Some components may differ depending on the type of
board. some of the popular Arduino boards are listed below.
o Arduino UNO
o Arduino Nano
o Arduino Mega
o Arduino Due
o Arduino Bluetooth
Arduino UNO:
The Arduino UNO is a standard board of Arduino. Arduino is an open-source computer hardware
and software company, project and user community that designs and manufactures microcontroller-
based kits for building digital devices and interactive objects that can sense and control the physical
world. Arduino UNO has 14 digital I/O pins (of which 6 provide PWM output) and 6 analog input
pins. The Arduino Uno can be powered via the USB connection or with an external power supply.
The power source is selected automatically. External (non- USB) power can come either from an AC-
to-DC adapter or battery.
The board can operate on an external supply of 6 to 20 volts. If supplied with less than 7V, however,
the 5V pin may supply less than five volts and the board may be unstable. If using more than 12V,
the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts.
Each of the 14 digital pins on the Uno can be used as an input or output, using pinMode(),
digitalWrite() and digitalRead() functions.

Let's discuss each component in detail.


o ATmega328 Microcontroller- It is a single chip Microcontroller of the ATmel family. The
processor code inside it is of 8-bit. It combines Memory (SRAM, EEPROM, and Flash),
Analog to Digital Converter, SPI serial ports, I/O lines, registers, timer, external and
internal interrupts, and oscillator.
o ICSP pin - The In-Circuit Serial Programming pin allows the user to program using the
firmware of the Arduino board.

Core Concepts of IoT By Dr. V. Deepika - 2-


o Power LED Indicator- The ON status of LED shows the power is activated. When the power
is OFF, the LED will not light up.
o Digital I/O pins- The digital pins have the value HIGH or LOW. The pins numbered from D0
to D13 are digital pins.
o TX and RX LED's- The successful flow of data is represented by the lighting of these LED's.
o AREF- The Analog Reference (AREF) pin is used to feed a reference voltage to the Arduino
UNO board from the external power supply.
o Reset button- It is used to add a Reset button to the connection.
o USB- It allows the board to connect to the computer. It is essential for the programming of
the Arduino UNO board.
o Crystal Oscillator- The Crystal oscillator has a frequency of 16MHz, which makes the Arduino
UNO a powerful board.
o Voltage Regulator- The voltage regulator converts the input voltage to 5V.
o GND- Ground pins. The ground pin acts as a pin with zero voltage.
o Vin- It is the input voltage.
o Analog Pins- The pins numbered from A0 to A5 are analog pins. The function of Analog pins
is to read the analog sensor used in the connection. It can also act as GPIO (General Purpose
Input Output) pins.
Arduino IDE:
The Arduino IDE is an open-source software, which is used to write and upload code to the Arduino
boards. The IDE application is suitable for different operating systems such as Windows, Mac OS X,
and Linux. It supports the programming languages C and C++. Here, IDE stands for Integrated
Development Environment. The Arduino IDE will appear as:

Core Concepts of IoT By Dr. V. Deepika - 3-


Arduino Programming:
Coding Screen:
The coding screen is divided into two blocks. The setup is considered as the preparation block, while
the loop is considered as the execution block. It is shown below:

Setup(): It contains an initial part of the code to be executed. The pin modes, libraries, variables,
etc., are initialized in the setup section. It is executed only once during the uploading of the
program and after reset or power up of the Arduino board.
The loop contains statements that are executed repeatedly. The section of code inside the curly
brackets is repeated depending on the value of variables.
Time in Arduino
The time in Arduino programming is measured in a millisecond.
Where, 1 sec = 1000 milliseconds.
We can adjust the timing according to the milliseconds. For example, for a 5-second delay, the time
displayed will be 5000 milliseconds.
Example 1:
Let's consider a simple LED blink example.
To do this experiment we need the following:
1. Arduino UNO board
2. Light Emitting Diode
3. Connecting Wires
4. Breadboard

PROCEDURE:
▪ Connect the circuit as shown in the circuit diagram.
▪ Place the LED on breadboard.
▪ Connect LED positive pin to D13 pin of Arduino UNO using Jumper wires.
▪ LED negative pin is connected to ground.
▪ Compile and Upload the following program code to Arduino UNO.

Circuit Diagram

Core Concepts of IoT By Dr. V. Deepika - 4-


PROGRAM CODE:
int led=13;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT); // initialize the digital pin as an output.
}
//loop runs again and again
void loop() {
// put your main code here, to run repeatedly
digitalWrite(led, HIGH);// turn the LED on
delay(1000);//wait for a second
digitalWrite(led, LOW);// turn the LED off
delay(1000);// wait for a second
}
Result:

The Arduino UNO is programmed to blink an LED.

o The void setup () would include pinMode as the main function.

pinMode ( )
The specific pin number is set as the INPUT or OUTPUT in the pinMode () function.
The Syntax is: pinMode (pin, mode)
Where,
pin: It is the pin number. We can select the pin number according to the requirements.
Mode: We can set the mode as INPUT or OUTPUT according to the corresponding pin number.
Let' understand the pinMode with an example.
Example: We want to set the 12 pin number as the output pin.
Code:
pinMode (12, OUTPUT);

Core Concepts of IoT By Dr. V. Deepika - 5-


digitalWrite( )
The digitalWrite ( ) function is used to set the value of a pin as HIGH or LOW.
Where,
HIGH: It sets the value of the voltage. For the 5V board, it will set the value of 5V, while for 3.3V, it
will set the value of 3.3V.
LOW: It sets the value = 0 (GND).
If we do not set the pinMode as OUTPUT, the LED may light dim.
The syntax is: digitalWrite( pin, value HIGH/LOW)
pin: We can specify the pin number or the declared variable.

delay ( )
The delay () function is a blocking function to pause a program from doing a task during the specified
duration in milliseconds.
For example, - delay (2000)
Where, 1 sec = 1000millisecond
Hence, it will provide a delay of 2 seconds.
Code:
digitalWrite (13, HIGH);
delay (2000);
digitalWrite (13, LOW);
delay (1000);

Example 2: To light the LED connected to pin number 13. We want to ON the LED for 4 seconds
and OFF the LED for 1.5 seconds.
Code:
void setup ()
{
pinMode ( 13, OUTPUT); // to set the OUTPUT mode of pin number 13.
}
void loop ()
{
digitalWrite (13, HIGH);
delay (4000); // 4 seconds = 4 x 1000 milliseconds
digitalWrite (13, LOW);
delay (1500); // 1.5 seconds = 1.5 x 1000 milliseconds
}

Core Concepts of IoT By Dr. V. Deepika - 6-


Program Flow - Flow Chart

Serial.begin():
Serial Communication:
The serial communication is a simple scheme that uses the UART (Universal Asynchronous
Receiver/Transmitter) on the Microcontroller. It uses,
o 5V for logic 1 (high)
o 0V for logic 0 (low)
For a 3.3V board, it uses
o 3V for logic 1 (high)
o 0V for logic 0 (low)
Every message sent on the UART is in the form of 8 bits or 1 byte, where 1 byte = 8 bits.
The messages sent to the computer from Arduino are sent from PIN 1 of the Arduino board,
called Tx (Transmitter). The messages being sent to the Arduino from the computer are received
on PIN 0, called Rx (Receiver).
These two pins on the Arduino UNO board look like the below image:

The Serial.begin( ) is a part of the serial object in the Arduino. It tells the serial object to perform
initialization steps to send and receive data on the Rx and Tx (pins 1 and 0).
Serial.begin ( )
The serial.begin( ) sets the baud rate for serial data communication. The baud rate signifies the data
rate in bits per second.
The default baud rate in Arduino is 9600 bps (bits per second). We can specify other baud rates as
well, such as 4800, 14400, 38400, 28800, etc.

Core Concepts of IoT By Dr. V. Deepika - 7-


The Serial.begin( ) is declared in two formats, which are shown below:
o begin( speed )
o begin( speed, config)
Where,
serial: It signifies the serial port object.
speed: It signifies the baud rate or bps (bits per second) rate. It allows long data types.
config: It sets the stop, parity, and data bits.
Example 1:
void setup ( )
{
Serial.begin(4800);
}
void loop ( )
{
}

Arduino Serial.print ( )
The serial.print ( ) in Arduino prints the data to the serial port. The printed data is stored in the ASCII
(American Standard Code for Information Interchange) format, which is a human-readable text.
Each digit of a number is printed using the ASCII characters.
The printed data will be visible in the serial monitor, which is present on the right corner on the
toolbar.
The Serial.print( ) is declared in two formats, which are shown below:
o print( value )
o print( value, format)
Where,
serial: It signifies the serial port object.
print: The print ( ) returns the specified number of bytes written.
value: It signifies the value to print, which includes any data type value.
format: It consists of number base, such as OCT (Octal), BIN (Binary), HEX (Hexadecimal), etc. for the
integral data types. It also specifies the number of decimal places.

Serial.print( value )
The serial.print ( ) accepts the number using the ASCII character per digit and value upto two decimal
places for floating point numbers.
Example 1:
Serial.print(15.452732)
Output:
15.45

Core Concepts of IoT By Dr. V. Deepika - 8-


It sends bytes to the printer as a single character. In Arduino, the strings and characters using the
Serial.print( ) are sent as it is.
Example 2:
Serial.print("Hello Arduino")
Output:
"Hello Arduino"

Serial.print( value, format )


It specifies the base format and gives the output according to the specified format. It includes the
formats Octal -OCT (base 8), Binary-BIN (base 2), Decimal-DEC (base 10), and Hexadecimal-HEX
(base 16).
Example 1:
Serial.print(25, BIN)
Output:
11001
Example 2:
Serial.print(58, HEX)
Output:
3A
It converts the decimal number 58 to hexadecimal number 3A.
Example 3:
Serial.print(58, OCT)
Output:
72
It converts the decimal number 58 to octal number 72.
Example 4:
Serial.print(25, DEC)
Output:
25
The conversion is from decimal to decimal. So, the output will be the same.

Serial.println ( )
The Serial.println ( ) means print line, which sends the string followed by the carriage return ('\r' or
ASCII 13) and newline ('\n' or ASCII 10) characters. It has a similar effect as pressing the Enter or
Return key on the keyboard when typing with the Text Editor.
The Serial.println( ) is also declared in two formats, which are shown below:
o println( value )
o println( value, format)
example:
void setup ( )

Core Concepts of IoT By Dr. V. Deepika - 9-


{
Serial.begin ( 4800);
}
void loop ( )
{
Serial.print(" Hello");
delay(1000);
Serial.println("Arduino"); // It will print Arduino followed by a new line.
delay ( 1500); // delay of 1.5 seconds between each printed line.
}
In the output, the word Hello will appear followed by the word Arduino 1 second later. After 1.5
second, another line will be printed.
Output
Hello Arduino
Hello Arduino // The next line will be printed after the specified duration.
.
.
.
.
The output will be printed repeatedly.

Serial.available( )
The Serial.available( ) function in Arduino gets the stored bytes from the serial port that are
available for reading. It is the data, which is already stored and arrived in the serial buffer. The serial
buffer in Arduino holds the 64 bytes.
Ex:
int arrivingdatabyte = 0; // initializing the incoming serial byte
void setup( )
{
Serial.begin(9600); // 9600 is the data rate in bps (bits per second).
}
void loop( ) // loop function that executes repeatedly
{
if(Serial.available( ) > 0) // It will only send data when the received data is greater than 0.
{
arrivingdatabyte = Serial.read( ); // It will read the incoming or arriving data byte
Serial.print("data byte received:");
Serial.println(arrivingdatabyte, DEC); // here, DEC means Decimal
}
}

Core Concepts of IoT By Dr. V. Deepika - 10-


Arduino Functions:
The functions allow a programmer to divide a specific code into various sections, and each section
performs a particular task. The functions are created to perform a task multiple times in a program.
Advantages of using Functions
Let's discuss some advantages of using functions in programming, which are listed below:
o It increases the readability of the code.
o It conceives and organizes the program.
o It reduces the chances of errors.
o It makes the program compact and small.
o It avoids the repetition of the set of statements or codes.
o It allows us to divide a complex code or program into a simpler one.
o The modification becomes easier with the help of functions in a program.
The Arduino has two common functions setup() and loop(), which are called automatically in the
background.
Function Declaration
The method to declare a function is listed below:
o Function return type
We need a return type for a function. For example, we can store the return value of a function in a
variable.
We can use any data type as a return type, such as float, char, etc.
o Function name
It consists of a name specified to the function. It represents the real body of the function.
o Function parameter
It includes the parameters passed to the function. The parameters are defined as the special
variables, which are used to pass data to a function.

Example 2: Here, we will add two numbers.


void setup()
{

Core Concepts of IoT By Dr. V. Deepika - 11-


Serial.begin(9600);
}
void loop() {
int a = 5; // initialization of values to the variables a and b
int b = 4;
int c;
c = myAddfunction(a, b); // c will now contains the value 9
Serial.println(c); // to print the resulted value
delay(1000); // time delay of 1 second or 1000 milliseconds
}
int myAddfunction(int i, int j)
{
int sum;
sum = i + j;
return sum;
}
Arduino Data Types:
The data types are used to identify the types of data and the associated functions for handling the
data. It is used for declaring functions and variables, which determines the bit pattern and the
storage space.
The data types that we will use in the Arduino are listed below:
o void Data Type
o int Data Type
o Char Data Type
o Float Data Type
o Double Data Type
o Unsigned int Data Type
o short Data Type
o long Data Type
o Unsigned long Data Type
o byte data type
o word data type
void Data Type
The void data type specifies the empty set of values and only used to declare the functions. It is used
as the return type for the functions that do not return any value.
Ex:
int a = 3;
void setup( )
{
. //

Core Concepts of IoT By Dr. V. Deepika - 12-


}
void loop ( )
{
.
.
}
Int Data Type
The integer data types are the whole numbers like 5, -6, 10, -123, etc. They do not have any fractional
part. The integer data types are represented by int. It is considered as the primary data type to store
the numbers.
int var = val;

Char Data Type


The char datatype can store any number of character set. The char type is often said to be an
integer type. It is because, symbols, letters, etc., are represented in memory by associated number
codes and that are only integers.
The syntax is:
char var = val;

Float Data Type


A number having the fractional part and a decimal part is considered as a floating-point number.
The syntax is:
float var = val;
Double Data Type
The double data type is also used for handling the decimal or floating-point numbers. It occupies
twice as much memory as float.
The syntax is:
double var = val;
Unsigned int Data Type
The unsigned int stores the value upto 2 bytes or 16 bits. It stores only positive values. The range of
unsigned int data type is from 0 to 65,535 or 0 to ((2 ^ 16) - 1).
The syntax is:
unsigned int var = val;
short Data Type
The short is an integer data type that stores two bytes or 16-bit of data.
The range of short data types is from -32768 to 32767 or - (2^ 15) to ((2 ^ 15) - 1).
The syntax is:
short var = val;

Core Concepts of IoT By Dr. V. Deepika - 13-


long Data Type
The long data types are considered as the extended size variables, which store 4 bytes (32 -bits). The
size ranges from -2,147,483,648 to 2,147,483,647.
The syntax is:
long var = val;
Unsigned long Data Type
The unsigned long data types are also considered as the extended size variables, which store 4 bytes
(32 -bits).
The syntax is:
unsigned long var = val;
byte
1 byte = 8 bits.
It is considered as an unsigned number, which stores values from 0 to 255.
The syntax is:
byte var = val;
word
It is considered as an unsigned number of 16 bits or 2 bytes, which stores values from 0 to 65535.
The syntax is:
word var = val;
Ex:
int a;
int b = 3;
char var = val;
float var = val;
float a = 3.6 ;
int b = a + 0.6 ;
double var = val;

Arduino Variables:
The variables are defined as the place to store the data and values. It consists of a name, value, and
type.
The variables can belong to any data type such as int, float, char, etc.
example:
int pin = 8;
Here, the int data type is used to create a variable named pin that stores the value 8. It also means
that value 8 is initialized to the variable pin.
Variables Scope
It means that in how many ways the variables can be declared.
The variables can be declared in two ways in Arduino, which are listed below:
o Local variables

Core Concepts of IoT By Dr. V. Deepika - 14-


o Global variables
Local Variables
The local variables are declared within the function. The variables have scope only within the
function. These variables can be used only by the statements that lie within that function.
For example,
void setup()
{
Serial.begin(9600);
}
void loop()
{
int x = 3;
int b = 4;
int sum = 0;
sum = x + b;
Serial.println(sum);
}
Global Variables
The global variables can be accessed anywhere in the program. The global variable is declared
outside the setup() and loop() function.
For example,
Consider the below code.
int LEDpin = 8;
void setup()
{
pinMode(LEDpin, OUTPUT);
}
void loop()
{
digitalWrite(LEDpin, HIGH);
}

Arduino constants:
The constants in Arduino are defined as the predefined expressions. It makes the code easy to read.
Logical level Constants
The logical level constants are true or false.
The value of true and false are defined as 1 and 0. Any non-zero integer is determined as true in
terms of Boolean language. The true and false constants are type in lowercase rather than uppercase
(such as HIGH, LOW, etc.).

Core Concepts of IoT By Dr. V. Deepika - 15-


Pin level Constants
The digital pins can take two value HIGH or LOW.
In Arduino, the pin is configured as INPUT or OUTPUT using the pinMode() function. The pin is
further made HIGH or LOW using the digitalWrite() function.
HIGH
The pin configured as HIGH is set at either 5V or 3.3V.
LOW
The pin configured as LOW is set at 0 Volts.

Constant Keyword
The name const represents the constant keyword. The variable will remain the same as other
variables, but its value cannot be changed.
For example,
const int a =2;
//....
a = 7; // illegal - we cannot write to or modify a constant
Arduino Operators
The operators are used to solve logical and mathematical problems. For example, to calculate the
temperature given by the sensor based on some analog voltage.
The types of Operators classified in Arduino are:
1. Arithmetic Operators
2. Compound Operators
3. Boolean Operators
4. Comparison Operators
5. Bitwise Operators

Arithmetic Operators
There are six basic operators responsible for performing mathematical operations in Arduino, which
are listed below:
o Assignment Operator ( = )
The Assignment operator in Arduino is used to set the variable's value. It is quite different from the
equal symbol (=) normally used in mathematics.
o Addition ( + )
The addition operator is used for the addition of two numbers. For example, P + Q.
o Subtraction ( - )
Subtraction is used to subtract one value from the another. For example, P - Q.
o Multiplication ( * )
The multiplication is used to multiply two numbers. For example, P * Q.
o Division ( / )
The division is used to determine the result of one number divided with another. For example, P/Q.

Core Concepts of IoT By Dr. V. Deepika - 16-


o Modulo ( % )
The Modulo operator is used to calculate the remainder after the division of one number by another
number.
Most of the operators are similar to the usual operator used in mathematics.
Let's understand the operators with the help of two examples.
Example 1:
Consider the below code.
int b;
void setup ( )
{
Serial.begin( 9600 );
}
void loop ( )
{
b = 5 + 2;
Serial.println(b);
}

Compound Operators
The compound operators perform two or more calculations at once.
The result of the right operand is assigned to the left operand, as already discussed above. The same
condition will apply to all the compound operators, which are listed below:
Let's consider a variable b.
o b++
Here, b = b + 1. It is called the increment operator.
o b+=
For example, b + = 4. It means, b = b+ 4.
o b--
Here, b = b - 1. It is called as the decrement operator.
o b-=
For example, b - = 3. It means, b = b - 3.
o b*=
For example, b * = 6. It means, b = b * 6.
o b/=
For example, b / = 5. It means, b = b / 5.
o b%=
For example, b % = 2. It means, b = b % 2.
Now, let's use the above operators with two variables, b and c.
o b + = c ( b = b + c)
o b - = c ( b = b - c)

Core Concepts of IoT By Dr. V. Deepika - 17-


o b * = c ( b = b * c)
o b / = c ( b = b / c)
o b % = c ( b = b % c)
We can specify any variable instead of b and c.

Boolean Operators
The Boolean Operators are NOT ( ! ), Logical AND ( & & ), and Logical OR ( | | ).
Let's discuss the above operators in detail.
o Logical AND ( & & )
The result of the condition is true if both the operands in the condition are true.
Consider the below example:
if ( a = = b & & b = = c )
Above statement is true if both conditions are true. If any of the conditions is false, the statement
will be false.
o Logical OR ( | | )
The result of the condition is true, if either of the variables in the condition is true.
Consider the below example.
if ( a > 0 | | b > 0 )
The above statement is true, if either of the above condition ( a> 0 or b > 0 ) is true.
o NOT ( ! )
It is used to reverse the logical state of the operand.
For example, a ! = 2.
The NOT operator returns the value 1 or TRUE when the specified operand is FALSE. It also reverses
the value of the specified expression.
Comparison Operators
The comparison operators are used to compare the value of one variable with the other.
The comparison operators are listed below:
o less than ( < )
The less than operator checks that the value of the left operand is less than the right operand. The
statement is true if the condition is satisfied.
Consider the below code.
int b;
int c ;
void setup ( )
{
Serial.begin( 9600 );
}
void loop ( )
{
b = 3;

Core Concepts of IoT By Dr. V. Deepika - 18-


c = 5;
if ( b < 4 )
Serial.println(b);
if ( c < 4)
Serial.println( c);
}
Output: 3
In the above code, if any of the two statement is correct, the corresponding value of the variable will
be printed. Here, only first condition is correct. Hence, the value of b will be printed.
o greater than ( > )
The less than operator checks that the value of the left side of a statement is greater than the right
side. The statement is true if the condition is satisfied.
For example, a > b.
If a is greater than b, the condition is true, else false.
o equal to ( = = )
It checks the value of two operands. If the values are equal, the condition is satisfied.
For example, a = = b.
The above statement is used to check if the value of a is equal to b or not.
o not equal to ( ! = )
It checks the value of two specified variables. If the values are not equal, the condition will be correct
and satisfied.
For example, a ! = b.
o less than or equal to ( < = )
The less or equal than operator checks that the value of left side of a statement is less or equal to
the value on right side. The statement is true if either of the condition is satisfied.
For example, a < = b
It checks the value of a is less or equal than b.
o greater than or equal to ( > = )
The greater or equal than operator checks that the value of the left side of a statement is greater or
equal to the value on the right side of that statement. The statement is true if the condition is
satisfied.
For example, a > = b
It checks the value of a is greater or equal than b. If either of the condition satisfies, the statement
is true.

Bitwise Operators
The Bitwise operators operate at the binary level. These operators are quite easy to use.
There are various bitwise operators. Some of the popular operators are listed below:
o bitwise NOT ( ~ )
The bitwise NOT operator acts as a complement for reversing the bits.

Core Concepts of IoT By Dr. V. Deepika - 19-


For example, if b = 1, the NOT operator will make the value of b = 0.
Let's understand with another example.
0 0 1 1 // Input or operand 1 ( decimal value 3)
1 1 0 0 // Output ( reverses the input bits ) decimal value is 12
o bitwise XOR ( ^ )
The output is 0 if both the inputs are same, and it is 1 if the two input bits are different.
For example,
For example,
1 0 0 1 // input 1 or operand 1
0 1 0 1 // input 2
1 1 0 0 // Output ( resultant - XOR)
o bitwise OR ( | )
The output is 0 if both of the inputs in the OR operation are 0. Otherwise, the output is 1. The two
input patterns are of 4 bits.
For example,
1 1 0 0 // input 1 or operand 1
0 0 0 1 // input 2
1 1 0 1 // Output ( resultant - OR)
o bitwise AND ( & )
The output is 1 if both the inputs in the AND operation are 1. Otherwise, the output is 0. The two
input patterns are of 4 bits.
For example,
1 1 0 0 // input 1 or operand 1
0 1 0 1 // input 2
0 1 0 0 // Output ( resultant - AND)
o bitwise left shift ( < < )
The left operator is shifted by the number of bits defined by the right operator.
o bitwise right shift ( > > )
The right operator is shifted by the number of bits defined by the left operator.
Arduino Array
The arrays are defined as the data structures that allow multiple values to be grouped together in a
simple way. This is an easily access method.
The array is normally built from the data types like integer, reals, characters, and boolean. It refers
to a named list of finite number (n) of similar data elements.
The set of consecutive numbers usually represent the elements in the array, which are 0, 1, 2, 3, 4,
5, 6,.......n.
For example, if the name of an array of 5 elements is AR, the elements will be referenced as shown
below:
AR[0], AR[1], AR[2], AR[3], and AR[4]

Core Concepts of IoT By Dr. V. Deepika - 20-


Arrays in Arduino
The array in Arduino is declared with the integer data type.
It is also defined as the collection of variables, which is acquired with an index number.
The array is represented as:

Array Declaration
There are different methods to declare an array in Arduino, which are listed below:
o We can declare the array without specifying the size.
For example,
int myarray[ ] = { 1, 4, 6, 7 } ;
o We can declare the array without initializing its elements.
For example,
int myarray[ 5];
o We can declare the array by initializing the size and elements.
int myarray[ 8] = { 1, 4, 7, 9, 3, 2 , 4};

Arduino Delay
The delay( ) function pauses the program or task for a specified duration of time. The time is specified
inside the open and closed parentheses in milliseconds.
Where,
1 second = 1000 milliseconds
Example 1:
delay(500)
Here, the specified time is 0.5 seconds.

Arduino If statement
The if ( ) statement is the conditional statement, which is the basis for all types of programming
languages.
If the condition in the code is true, the corresponding task or function is performed accordingly. It
returns one value if the condition in a program is true. It further returns another value if the
condition is false.
It means that if ( ) statement checks for the condition and then executes a statement or a set of
statements.

Core Concepts of IoT By Dr. V. Deepika - 21-


Let's understand the concept with the help of a flow chart.

The if ( ) statement is written as:


if ( condition)
{
// include statements
// if the condition is true
// then performs the function or task specified inside the curly braces
}
Ex:
int a = 6; // initiaization of values to variables a and b
int b = 4;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (a > b )
{
Serial.println( " a is greater than b ");
}
if (b > a )
{
Serial.println( " b is greater than a ");
}
}

Arduino if-else and else-if


The else and else-if both are used after specifying the if statement. It allows multiple conditions to
be grouped.

Core Concepts of IoT By Dr. V. Deepika - 22-


If else
The if-else condition includes if ( ) statement and else ( ) statement. The condition in the else
statement is executed if the result of the If ( ) statement is false.
The flowchart is shown below:

Example:
if (condition)
{
// statements
}
else
{
//statements
}
Example 1:
int a = 5;
int b= 6;
void setup ( )
{
Serial.begin ( 9600 );
}
void loop ( )
{
if ( a > b )
{
Serial.println ( " a is greater " );
}
else
{
Serial.println ( " a is smaller " );

Core Concepts of IoT By Dr. V. Deepika - 23-


}
}
Else if
The else if statement can be used with or without the else ( ) statement. We can include multiple
else if statements in a program.
Let's understand with an example.
if (condition)
{
// statements
}
else if ( condition)
{
// statements
// only if the first condition is false and the second is true
}
else
{
//statements
}

Arduino for Loop


The for statement is commonly used for repetitive task or operation or to operate on the group of
data/pins in combination with arrays.
The syntax is:
for (initialization; condition; increment)
{
\\ statements
}
where,
o initialization: It is defined as the initialization of the variable.
o condition: The condition is tested on every execution. If the condition is true, it will execute
the given task. The loop ends only when the condition becomes false.
o increment: It includes the increment operator, such as i + +, i - - , i + 1, etc. It is incremented
each time until the condition remains true.
For example,
for ( i = 0 ; i < 5 ; i + +)
Ex: int x;
void setup ( )
{
Serial.begin(9600);

Core Concepts of IoT By Dr. V. Deepika - 24-


for (x = 2; x < 100; x = x * 2)
{
Serial.println(x);
}
}
void loop ( ) {
}
Output:

Arduino while loop


The while loop() is the conditional loop that continues to execute the code inside the parentheses
until the specified condition becomes false.
The while loop will never exit until the tested condition is changed or made to stop. The common
use of a while loop in Arduino includes sensor testing, calibration (calibrating the input of
sensor), variable increment, etc.
The syntax is:
while (condition)
{
// code or set of statements
}
Flowchart

Example 1:
int a = 0;

Core Concepts of IoT By Dr. V. Deepika - 25-


void setup()
{
Serial.begin(9600);
while( a < 5)
{
Serial.println("Welcome to Arduino");
a = a + 1;
}
}
void loop()
{
}
Output:

do...while
The working of the do-while loop is similar to the while loop. The condition inside the do-while will
execute at least once. It is because the condition is tested at the end of the loop instead of the
beginning.
The syntax is:
do
{
// code or set of statements
} while (condition);
Flowchart

Core Concepts of IoT By Dr. V. Deepika - 26-


Example 1:
int a = 0;
void setup()
{
Serial.begin(9600);
do
{
Serial.println("Welcome to the do while loop");
a = a + 1;
} while( a < 3);
}
void loop()
{
}
Output:

Arduino switch case


The switch case controls the flow of the program by executing the code in various cases. A switch
statement compares a particular value of a variable with statements in other cases. When the
statements in a case matches the value of a variable, the code associated with that case executes.
The break keyword is used at the end of each case. For example, if there are five cases, the break
statements will also be five. The break statement exits the switch case.
Flowchart:

Core Concepts of IoT By Dr. V. Deepika - 27-


Example
// switch case example
void setup()
{
Serial.begin(9600);
int a = 1;
switch(a) // the case matching the value in the declared variable will run
{
case 1:
Serial.println(" Case 1 matches");
// the value of variable matches with the value in case 1.
// The message associated with case 1 will be printed
break;
case 2:
Serial.println(" Case 2 matches");
break;
case 3:
Serial.println(" Case 3 matches");
break;
default:
Serial.println(" default matches");
break;
}

Core Concepts of IoT By Dr. V. Deepika - 28-


}
void loop()
{
}
Output:

Arduino String
The string is an array of characters or a set of characters that consists of numbers,
spaces, and special characters from the ASCII table.
The string can be declared in the following ways:
o char StrA[10];
o char StringA[8] = {'w', 'e', 'l', 'c', 'o', 'm', 'e'};
We can also add an explicit null character
o char StringB[8] = {'w', 'e', 'l', 'c', 'o', 'm', 'e', '\0'};
We can also declare an array with extra space for a string constant StrA.
o char StrA[10]; = "Hello";
o char StrD[6]; = "Hello";
o char StrD[]; = "Welcome";
The data type is char. Each character is an element in string. For example, Arduino contains seven
characters. So, we need to declare an array atleast of size 8 (1 extra to store the null character at the
end of the string).
Consider the below example:
char myString[len] = "text";
The above declared string will be stored as:

Example:
const int length = 20;
char myString[length] = "Hello Arduino";
// we can also specify as char myString[20] = "Hello Arduino";
void setup()
{
Serial.begin(9600);
}
Core Concepts of IoT By Dr. V. Deepika - 29-
void loop()
{
for( int i=0; i<length; i = i+1)
{
if(myString[i]== '\0')
// it will stop printing when it sees a null character
{
break; //
}
Serial.print(myString[i]);
}
Serial.println();
delay(500);
}
The code will print Hello Arduino multiple times, as shown below:

Core Concepts of IoT By Dr. V. Deepika - 30-


Integration of Sensors and Actuators with Arduino
Ex: 1. Blinking an LED using Arduino uno
AIM: To blink an LED (Light Emitting Diode) connected to the Arduino board.
APPARATUS:
1. Arduino UNO board
2. Light Emitting Diode
3. Connecting Wires
4. Breadboard
PROCEDURE:
▪ Connect the circuit as shown in the circuit diagram.
▪ Place the LED on breadboard.
▪ Connect LED positive pin to D13 pin of Arduino UNO using Jumper wires.
▪ LED negative pin is connected to ground.
▪ Compile and Upload the following program code to Arduino UNO.

Figure 2: Circuit Diagram


PROGRAM CODE:
int led=13;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT); // initialize the digital pin as an output.
}
//loop runs again and again
void loop() {
// put your main code here, to run repeatedly
digitalWrite(led, HIGH);// turn the LED on
delay(1000);//wait for a second
digitalWrite(led, LOW);// turn the LED off
delay(1000);// wait for a second
}

Core Concepts of IoT By Dr. V. Deepika - 31-


OUTPUT:

The LED blinks indefinitely with a second delay.


RESULT: The Arduino UNO is programmed to blink an LED.

Ex: 2 Run Servo Motor using Arduino Uno


AIM: To run the Servo Motor using Arduino UNO board
APPARATUS:
1. Arduino UNO board
2. Servo Motor
3. Connecting Wires
PROCEDURE:
1) connect yellow color wire to pin 3 of digital pin of Arduino
2) Red color wire to 5v pin of Arduino
3) Brown color wire to GND of Arduino board

Figure 1: Circuit Diagram

Core Concepts of IoT By Dr. V. Deepika - 32-


Program Code:
// Include the Servo library
#include <Servo.h>
// Declare the Servo pin
int servoPin = 3;
// Create a servo object
Servo Servo1;
void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}
void loop(){
// Make servo go to 0 degrees
Servo1.write(0);
delay(1000);
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);
// Make servo go to 180 degrees
Servo1.write(180);
delay(1000);
}

Output:

Core Concepts of IoT By Dr. V. Deepika - 33-


Core Concepts of IoT By Dr. V. Deepika - 34-

You might also like