You are on page 1of 73

Unit-02: M2M Communications1 and Arduino2

EC600OE: Fundamentals of Internet Of Things

Dr. Mohammad Fayazur Rahaman


Associate Professor, mfrahaman_ece@mgit.ac.in

Dept. of Electronics and Communications Engineering,


Mahatma Gandhi Institute of Technology, Gandipet, Hyderabad-75

B.Tech. ECE III Year II Semester (R18)


2022 - 2023 1 / 73
Unit-02: M2M Communications1 and Arduino2 [1]
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

1
Machine to Machine Communications
2
Introduction to Arduino Programming
2 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

3 / 73
Introduction

• The terms IOT and M2M (Machine to Machine Communication) are


often used interchangeably, though these terms have evolved from
different backgrounds
• This section describes some of the differences and similarities between
IOT and M2M

4 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

5 / 73
M2M

i. Machine to machine refers to network-


ing of machines (or devices) for the
purpose of remote monitoring, control
and data exchange.
ii. The figure shows an end-to-end archi-
tecture for M2M systems comprising
• M2M area networks,
• communication network and
• application domain.

6 / 73
I. The M2M area network comprises work.
of machines or M2M nodes which • While the M2M area networks use
have embedded hardware modules for either proprietary or non-IP-based
sensing, actuation and communica- communication protocols the com-
tion. munication network uses IP based
• Various communication protocols network
can be used for M2M local area • This network can use either wired
networks such as Zigbee, Blue- or wireless networks
tooth, 6LoWPAN, IEEE 802.15.4, • To enable the communication be-
Etc. tween remote non-IP based M2M
• These communication protocols area networks, M2M gateways are
provide connectivity between M2M used
nodes within an M2M area net-

7 / 73
II. The communication between the • With an M2M Gateway, each node
M2M nodes and the M2M Gateway in an M2M area Netwerk appears
is based on the communication pro- as a virtualized node for external
tocols which are native to the M2M M2M area networks
area network
• M2M gateway performs protocol
translations to enable IP connec-
tivity for M2M area networks.
• M2M gateway acts as a proxy for
performing translations from/to
native protocols to/from the Inter-
net protocol (IP)

8 / 73
III. The M2M data is gathered into some home automation, industrial au-
point solutions, such as enterprise tomation, smart grid, etc.
applications, service management, • M2M solution designs, such as
applications, or remote monitoring data connection and storage archi-
applications. tectures and applications are spe-
• M2M has various application do- cific to the M2M application do-
mains such as smart metering, main.

9 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

10 / 73
DIfference between IoT and M2M
Though both M2M and IoT involve networking of machines or devices, they differ in
the underlying technologies, systems architectures and types of applications

I. Communication Protocols: IOT is usually on the proto-


• M2M uses either proprietary or cols above the network layer such
non-IP-based communication pro- as HTTP, CoAP, web sockets,
tocols for communication within MQTT, XMPP, DDS, AMQP, etc
the M2M area networks
• Commonly used M2M protocols
include Zigbee, Bluetooth, 6LoW-
PAN, IEEE 802.15.4
• The focus of communication in
M2M is usually on the protocols
below the network layer
• The focus of communication in
11 / 73
II. Machines in M2M vs Things in • M2M systems, in contrast to IoT,
IoT: typically have homogeneous ma-
• The "things" in IoT refer to phys- chine types within and M2M area
ical objects that have unique iden- network
tifiers and can sense and commu- III. Hardware vs Software Emphasis:
nicate with their external environ- • While the emphasis of M2M is
ment more on hardware with embedded
• The unique identifier for the things modules, the emphasis of IoT is
in IoT are the IP addresses more on software
• Things have software components • IOT devices run specialised soft-
for accessing, processing and stor- ware for sensor, data collection,
ing sensor information or control- data analysis and interfacing with
ling actuators connected the cloud through IP-based com-
• IOT systems can have heteroge- munication
neous things
12 / 73
IV. Data Collection and Analysis: cations
• M2M data is collected in point so- • IOT data is collected in the cloud
lutions and often in on-promises, and can be accessed by cloud ap-
storage infrastructure plications such as analytics appli-
• In contrast to M2M, the data in cations, enterprise applications, re-
IOT is collected in the cloud. mote diagnosis and management
◦ The analytical components, application
analyse the data and store the ◦ The scale of data collected in
results in the cloud database IOT is massive
◦ The IOT data and analysis re- ◦ As a result, cloud-based real-
sults are visualised with the time and batch analysis frame-
cloud-based applications works are used for data analysis
V. Applications:
• M2M data is collected in point
solutions and can be accessed
by on-premises applications such
as diagnosis, service management,
and on-promises enterprise appli- 13 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

14 / 73
Current Challenges in IoT

I. Large Scale of Co- figuration: The different con-


Operation: The cooperation figuration modes for IoT de-
and coordination of millions vices which come from un-
of distributed devices are re- known owners
quired on Internet IV. Semantic Conflicts: Differ-
II. Global Heterogeneity: Het- ent processing logics applied
erogeneous IoT devices and to same IoT networked devices
their subnets or applications.
III. Unknown IoT Device Con-

15 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

16 / 73
What is Interoperability ?

i. Interoperability is a character- future, in either implementa-


istic of a product or system, tion or access, without any re-
whose interfaces are completely strictions.
understood, to work with other • Communicate meaningfully
products or systems, present or • Exchange data or services

17 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

18 / 73
Why Interoperability is Important in Context of IoT ?
I. To fulfill the IoT objectives tion protocols such as ZigBee
• Physical objects can interact with (IEEE 802.15.4), Bluetooth (IEEE
any other physical objects and can 802.15.1), GPRS, 6LowPAN, and
share their information Wi-Fi (IEEE 802.11)
• Any device can communicate with • Different wired communication
other devices anytime from any- protocols like Ethernet (IEEE
where 802.3) and Higher Layer LAN Pro-
• Machine to Machine communi- tocols (IEEE 802.1)
cation(M2M), Device to Device • Different programming languages
Communication (D2D), Device to used in computing systems and
Machine Communication (D2M) websites such as JavaScript, JAVA,
• Seamless device integration with C, C++, Visual Basic, PHP, and
IoT network Python
II. Heterogeneity • Different hardware platforms such
• Different wireless communica- as Crossbow, NI, etc.
19 / 73
III. Different operating systems IV. Different databases: DB2,
• As an example for sensor node: MySQL, Oracle, PostgreSQL,
TinyOS, SOS, Mantis OS, RE- SQLite, SQL Server, and Sybase
TOS, and mostly vendor specific V. Different data representations
OS VI. Different control models
• As an example for personal com- VII. Syntactic or semantic interpreta-
puter: Windows, Mac, Unix, and tions
Ubuntu

20 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

21 / 73
Different Types of Interoperability

I. User Interoperability : Interoperability problem between a user


and a device
II. Device Interoperability : Interoperability problem between two
different devices

22 / 73
Example:
• Using IoT, both A and B provide a real- × User U can’t understand the service
time security service provided by A and B
• A is placed at Delhi, India, while B is × Similarly, A and B do not mutually
placed at Tokyo, Japan understand each other
• A, B, U use Hindi, Japanese, and English
language, respectively
• User U wants real-time service of CCTV
camera from the device A and B
• Problems are listed below
× user does not know the devices A and
B
× Devices A and B are different in
terms of syntactic and semantic no-
tions
× Therefore, it is difficult to find CCTV
device
23 / 73
User Interoperability

The following problems need to be solved Device identification and


I. Device identification and catego- categorization for discovery: There are
rization for discovery different solutions for generating unique
II. Syntactic interoperability for de- address
vice interaction i. Electronic Product Codes (EPC)
III. Semantic interoperability for de- ii. Universal Product Code (UPC)
vice interaction iii. Uniform Resource Identifier (URI)
iv. IP Addresses : IPv6
There are different device classification
solutions
• United Nations Standard Products and
Services Code (UNSPSC)
• eCl@ss
24 / 73
Syntactic Interoperability for Device • Open standard protocols such as
Interaction: IEEE 802.15.4, IEEE 802.15.1, and
i. The interoperability between devices WirelessHART
and device user in term of message for- • Closed protocols such as Z-Wave
mats v. Middleware technology
ii. The message format from a device to • Software middleware bridge
a user is understandable for the user’s • Dynamically map physical devices
computer with different domains
iii. On the other hand, the message for- • Based on the map, the devices can
mat from the user to the device is ex- be discovered and controlled, re-
ecutable by the device motely
iv. Some popular approaches are vi. Cross-context syntactic interoperabil-
• Service-oriented Computing ity
(SOC)-based architecture • Collaborative concept exchange
• Web services • Using XML syntax
• RESTful web services
25 / 73
Semantic Interoperability for Device ing of user’s instruction that is sent
Interaction from the user to the device.
i. The interoperability between devices iii. Similarly, the user can understand
and device user in term of message’s the meaning of device’s response sent
meaning from the device
ii. The device can understand the mean-

26 / 73
Device Interoperability

Solution approach for device


interoperability: Universal Middleware
Bridge (UMB), consists of UMB Core
(UMB-C) and UMB Adaptor (UMB-A)
◦ Solves seamless interoperability prob-
lems caused by the heterogeneity of sev-
eral kinds of home network middleware
◦ UMB creates virtual maps among the
physical devices of all middleware home
networks, such as HAVI, Jini, LonWorks,
and UPnP
◦ Creates a compatibility among these
middleware home networks
27 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

28 / 73
Arduino Board

Features of Arduino Types of Arduino Board


• Open source based electronic pro- I. Arduino boards based on AT-
grammable board (micro con- MEGA328 microcontroller
troller)and software(IDE) II. Arduino boards based on AT-
• Accepts analog and digital signals MEGA32u4 microcontroller
as input and gives desired output III. Arduino boards based on AT-
• No extra hardware required to MEGA2560 microcontroller
load a program into the controller IV. Arduino boards based on
board AT91SAM3X8E microcon-
troller
29 / 73
Arduino UNO
vi. Analog Input Pins
vii. Digital I/O Pins

Board Details:
i. Power Supply: USB or power barrel
jack
ii. Voltage Regulator
iii. LED Power Indicator
iv. Tx-Rx LED Indicator
v. Output power, Ground 30 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

31 / 73
Setup
• Arduino IDE is an open source software • TOOLS -> PORT -> select your port
that is used to program the Arduino con-
troller board
• Based on variations of the C and C++
programming language
• It can be downloaded from Arduino’s of-
ficial website and installed into PC
• Power the board by connecting it to a
PC via USB cable
• Launch the Arduino IDE
• Set the board type and the port for the
board
• TOOLS -> BOARD -> select your
board
32 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

33 / 73
Arduino IDE

i. Program coded in Arduino IDE is


called a SKETCH
ii. To create a new sketch
iii. File -> New
iv. To open an existing sketch
v. File -> open ->
vi. There are some basic ready-to-use
sketches available in the EXAMPLES
section
vii. File -> Examples -> select any pro-
gram

34 / 73
The list of actions that can be performed
with the sketches are:
• Verify: Checks the code for compilation
errors
• Upload: Uploads the final code to the
controller board
• New: Creates a new blank sketch with
basic structure
• Open: Opens an existing sketch
• Save: Saves the current sketch

35 / 73
• Serial Monitor: Opens the serial con-
sole
• All the data printed to the console are
displayed here

36 / 73
Structure of a Sketch
i. A sketch can be divided into two parts: void setup(){
I. setup() Serial.begin(9600);
II. loop() }
ii. The function setup() is the point
where the code starts, just like the void loop(){
main() function in C and C++ Serial.println("Hello World !");
iii. I/O Variables, pin modes are initial- }
ized in the setup() function
iv. loop() function, as the name sug-
gests, iterates the specified task in the
program

37 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

38 / 73
Supported Datatypes

Arduino supports the following viii. Unsigned int


data types: ix. Unsigned long
i. Void x. Unsigned char
ii. Long xi. Float
iii. Int xii. Double
iv. Char xiii. Array
v. Boolean xiv. String-char array
vi. Byte xv. String-object
vii. Word xvi. Short

39 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

40 / 73
Function Libraries
Input/Output Functions: • digitalRead(pin) : Reads a
• The arduino pins can be config- HIGH or LOW value from a digi-
ured to act as input or output pins tal pin
using the pinMode(pin, mode) • analogWrite(pin, HIGH) :
function Writes a value between 0 and 255
I. pin - pin number on the Ar- value to a digital PWM (pulse
duino board width modulation) pin
II. mode- INPUT/OUTPUT • analogRead(pin) : Reads from
• digitalWrite(pin, HIGH) : the analog input pin i.e., voltage
Writes a HIGH or LOW value to applied across the pin
a digital pin
41 / 73
• Character functions such as • delay() function is one of the
i. isdigit(), most common time manipulation
ii. isalpha(), function used to provide a delay
iii. isalnum(), of specified time. It accepts inte-
iv. isxdigit(), ger value (time in miliseconds)
v. islower(),
vi. isupper(),
vii. isspace()
viii. return 1 (true) or 0 (false)

42 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

43 / 73
Operators
• Arithmetic Operators:
=, +, -, *, /, %
• Comparison Operator:
==, !=, <, >, <=, >=
• Boolean Operator:
&&, ||, !
• Bitwise Operator:
&, |, ^, ~, <<, >>,
• Compound Operator:
++, --, +=, -=, *=, /=, %=, |=, &=
44 / 73
Control Statement

I. If statement
if(condition){
statements if the condition is true;
}
II. If-Else statement
if(condition){
statements if the condition is true;
}else{
statements if the condition is false;
}

45 / 73
III. If-Elseif-Else statement
if(condition1){
statements if the condition1 is true;
}else if(condition2){
statements if the condition1 is false and condition2 is true;
}else{
statements if both the conditions are false;
}
IV. Switch-Case statement
switch(choice){
case opt1: statement_1;break;
case opt2: statement_2;break;
.
case default: statement_default;break;
}
V. Conditional Operator
val=(condition)?(statement1):(statement2)
46 / 73
Loops
I. For loop:
for(initialization; condition; increment){
Statement till the condition is true;
}
II. While loop:
while(condition){
Statement till the condition is true;
}
III. Do-While loop:
do{
Statement till the condition is true;
}while(condition);
IV. Nested loop: Calling a loop inside another loop
V. Infinite loop: Condition of the loop is always true, the loop will never terminate
47 / 73
Arrays
• Collection of elements having homogenous datatype that are stored in adjacent
memory location.
• The conventional starting index is 0.
• Declaration of array:
<Datatype> array_name[size];
int values[5];
• Alternative Declaration:
int values[] = {0,1,2,3,4};
int values[5] = {0,1,2};
• Multi-dimentional array Declaration:
<Datatype> array_name[n1][n2][n3]... ;
int values[row][col][height];
int values[5];

48 / 73
String
• Array of characters with NULL as termination is termed as a String.
• Declaration using array:
char str[] = "ABCD";
char str[4];
str[0] = 'A';
str[1] = 'B';
str[2] = 'C';
str[3] = 'D';
• Declaration using String Object:
String str = "ABCD";
• Functions of String Object:
◦ str.ToUpperCase(): change all the characters of str to upper case
◦ str.replace(str1,str2): is str1 is the sub string of str then it will be
replaced by str2
◦ str.length(): returns the length of the string without considering null
49 / 73
Math Library
• To apply the math functions and mathematical constants, “MATH.h”
header files is needed to be included.
• Functions:
◦ cos(double radian);
◦ sin(double radian);
◦ tan(double radian);
◦ fabs(double val);
◦ fmod(double val1, double val2);
◦ exp(double val);
◦ log(double val);
◦ log10(double val);
◦ square(double val);
◦ pow(double base, double power); 50 / 73
Random Number

• randomSeed(int v): reset the pseudo-random number generator


with seed value v
• random(maxi): gives a random number within the range [0,maxi]
• random(mini,maxi): gives a random number within the range
[mini,maxi]

51 / 73
Interrupts
• An external signal for which system blocks the current running process
to process that signal
• Types:
I. Hardware interrupt
II. Software interrupt
• digitalPinToInterrupt(pin): Change actual digital pin to the
specific interrupt number.
• attachInterrupt(digitalPinToInterrupt(pin), ISR,
mode);
◦ ISR: a interrupt service routine have to be defined

52 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

53 / 73
1. Write a program to blink the built-in LED of Arduino
void setup() {
pinMode(LED_BUILTIN , OUTPUT);
}

void loop(){
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

54 / 73
2. Write a program to implement a traffic signal that operates
continuously as follows: red - 2 secs, orange - 1 secs and green -
2 secs
const int LED_RED = 2;
const int LED_ORANGE = 3;
const int LED_GREEN = 4;

void setup() {
pinMode(LED_RED , OUTPUT);
pinMode(LED_ORANGE , OUTPUT);
pinMode(LED_GREEN , OUTPUT);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_ORANGE, LOW);
digitalWrite(LED_GREEN, LOW);
}
void loop(){
digitalWrite(LED_RED, HIGH);
delay(2000);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_ORANGE, HIGH);
delay(1000);
digitalWrite(LED_ORANGE, LOW);
digitalWrite(LED_GREEN, HIGH);
delay(2000);
digitalWrite(LED_GREEN, LOW);
} 55 / 73
3. Write an Arduino program to gradually increase and decrease the
brightness of an LED connected to an PWM pin.
const int LED = 3;
int step ;
void setup(){
pinMode(LED, OUTPUT);
}
void loop(){
for(step=0; step<=255; step++){
analogWrite(LED, step);
delay(20);
}
for(step=255; step>=0; step--){
analogWrite(LED, step);
delay(20);
}
}
56 / 73
4. Write an Arduino program to randomly change the colours of an
RGB LED every second and print the RGB values used to the serial
monitor.
const int myPINS[] = {6,9,11};
int i, value;
void setup(){
for(i=0; i<3; i++){
pinMode(myPINS[i],OUTPUT);
}
Serial.begin(9600);
}
void loop(){
for(i=0; i<3; i++){
value = random(0, 255);
analogWrite(myPINS[i], value);
Serial.print(i);
Serial.print(":");
Serial.println(value);
}
Serial.println();
delay(500);
}

57 / 73
5. Write a sketch to turn-on an LED when a push button is pressed
and turn-off when button is released.
const int BUTTON = 2;
const int LED = 13;
int buttonState = 0;
void setup(){
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
}
void loop(){
buttonState = digitalRead(BUTTON);
if(buttonState == HIGH){
digitalWrite(LED, HIGH);
}else{
digitalWrite(LED, LOW);
}
delay(20);
} 58 / 73
6. Write a sketch to control the rate of blinking of an LED using
a potentiometer.
const int DIM = A0;
const int LED = 13;
int value = 0;
void setup(){
pinMode(DIM, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop(){
value = analogRead(DIM);
Serial.println(value);
digitalWrite(LED, HIGH);
delay(value);
digitalWrite(LED, LOW);
delay(value);
} 59 / 73
7. Write a sketch to gradually rotate a Servo motor in steps of 1
degree upto 180 degree position and back repeatedly.
#include <Servo.h>
int pos = 0;
Servo servoDemo;
void setup(){
servoDemo.attach(9);
}
void loop(){
for (pos = 0; pos <= 180; pos += 1) {
servoDemo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
servoDemo.write(pos);
delay(15);
}
} 60 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

61 / 73
Sensors

i. Electronic elements • Temperature


ii. Converts physical quantity/ • Humidity
measurements into electrical • Compass
signals • Light
iii. Can be analog or digital • Sound
iv. Some commonly used sensors: • Accelerometer

62 / 73
Sensor integration with Arduino

i. Digital Humidity and Tempera-


ture Sensor (DHT)
ii. PIN 1, 2, 3, 4 (from left to
right)
• PIN1-3.3V-5V Powersupply
• PIN2-Data
• PIN3-Null
• PIN4-Ground

63 / 73
DHT Sensor Library Sketch
i. Arduino supports a special library for i. Write the sketch (code) in an Arduino
the DHT11 and DHT22 sensors IDE in a PC
ii. Provides function to read the tempera- ii. Connect the board to the PC
ture and humidity values from the data iii. Set the port and board type to the ap-
pin propriate values in Arduino IDE
• dht.readHumidity() iv. Verify and upload the code
• dht.readTemperature() Output
Connection i. The readings are printed at a delay of
i. Connect pin 1 of the DHT to the 3.3 2 seconds as specified by the delay()
V supply pin in the board function
ii. Data pin (pin 2) can be connected to
any digital pin, here 12 Humidity: 65.80%, Temp: 26.00 Celsius
iii. Connect pin 4 to the ground (GND) Humidity: 65.80%, Temp: 26.00 Celsius
pin of the board ....
64 / 73
#include <DHT.h> ;
DHT dht(8, DHT22);
float humidity;
float temperature;
void setup(){
Serial.begin(9600);
dht.begin();
}
void loop(){
humidity = dht.readHumidity();
temperature= dht.readTemperature();
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("%, Temp: ");
Serial.print(temperature);
Serial.println(" Celsius");
delay(2000);
} 65 / 73
Where are we ?
1. Machine-to-Machine Communications 4.4 Supported Datatypes
1.1 Introduction 4.5 Function Libraries
1.2 M2M 4.6 Arduino Programming
4.6.1 Operators in Arduino
2. Difference between IoT and M2M 4.6.2 Control Statement
2.1 DIfference between IoT and M2M 4.6.3 Loops
3. Interoperability in IoT 4.6.4 Arrays
3.1 Current Challenges in IoT 4.6.5 String
3.2 What is Interoperability ? 4.6.6 Math Library
3.3 Why Interoperability is Important in Context of IoT ? 4.6.7 Random Number
3.4 Different Types of Interoperability 4.6.8 Interrupts
3.4.1 User Interoperability 4.7 Example Programs
3.4.2 Device Interoperability 5. Integration of Sensors and Actuators with Arduino
4. Introduction to Arduino Programming 5.1 Sensors
4.1 Arduino Board 5.1.1 Sensor integration with Arduino
4.2 Setup 5.2 Actuators
4.3 Arduino IDE 5.2.1 Servo Motor integration with Arduino

66 / 73
Actuators

i. Mechanical/Electro-mechanical Types of Motor Actuators


device i. Servo motor
ii. Converts energy into motion ii. Stepper motor
iii. Mainly used to provide con- iii. Hydraulic motor
trolled motion to other compo- iv. Solenoid
nents v. Relay
iv. Uses different combination of vi. AC motor
various mechanical structures
like screws, ball bearings, gears
to produce motion
67 / 73
Servo Motor integration with Arduino
• High precision motor
• Provides rotary motion 0 to 180
degree
• 3 wires in the Servo motor
◦ Black or the darkest one is
Ground
◦ Red is for power supply
◦ Yellow for signal pin

68 / 73
Servo Library on Arduino: the 5V pin of the board.
• Arduino provides different library- • Connect the signal wire to any
SERVO to operate the servo mo- digital output pin (we have used
tor pin 8).
• Create an instance of servo to use Sketch:
it in the sketch • Create an instance of Servo
Servo myservo: • The instance must be attached to
Connection: the pin before being used in the
• Connect the Ground of the servo code
to the ground of the Arduino • write() function takes the de-
board. gree value and rotates the motor
• Connect the power supply wire to accordingly

69 / 73
#include <Servo.h>
const int PIN = 12;

Servo myservo;
void setup(){
myservo.attach(PIN);
}
void loop(){
myservo.write(0);
delay(1000);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
} 70 / 73
Board Setup: Other functions in Servo
• Connect the board to the PC library:
• Set the port and board type • knob()
• Verify and upload the code • sweep()
Output: • write()
• The motor turns 0◦ , 90◦ and 180◦ • writeMicroseconds()
with a delay of 1 second each • read()
• attach()
• detach()

71 / 73
Text Books
[1] P. S. Misra, “Nptel: Introduction to internet of things,” 2019.
https://www.youtube.com/@introductiontointernetofth4217/featured
[Accessed: Feb 2023].

[2] A. Bahga and V. Madisetti, Internet of Things: A Hands-on Approach.

[3] K. K. Terokarvinen and V. Valtokari, Make Sensors.


1st Edition, Maker media, 2014.

[4] OpenAI.com, “chatgpt - an openai based language models for dialogue,” 2023.
https://openai.com/blog/chatgpt/ [Accessed: Feb 2023].

72 / 73
Thank you

73 / 73

You might also like