Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

F-IoT Unit-2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 85

ANURAG COLLEGE OF ENGINEERING

(Approved by AICTE, New Delhi & Affiliated to JNTU-HYD)


Aushapur (V), Ghatkesar (M), Medchal (Dist.), Telangana-501 301.

FUNDAMENTALS OF INTERNET OF THINGS


B.Tech - III YEAR II SEM - CSE

Presented By:

G. Kiran Kumari
Assistant Professor
Department of ECE
Anurag College of Engineering
Syllabus

UNIT – II

Machine-to-Machine Communications,
Difference between IoT and M2M,
Interoperability in IoT, Introduction to
Arduino Programming, Integration of
Sensors and Actuators with Arduino.

Anurag College of Engineering 2


Machine-to-Machine (M2M)
• Machine-to-Machine (M2M) refers to networking of
machines (or devices) for the purpose of remote
monitoring and control and data exchange.
• Term which is often synonymous with IoT is Machine-to-
Machine (M2M).
• IoT and M2M are often used interchangeably.
• Fig. shows the end-to-end architecture of M2M systems
comprises of M2M area networks, communication
networks and application domain.

3
Anurag College of Engineering
Fig. M2M System Architecture
Anurag College of Engineering 4
Machine-to-Machine (M2M) cont’d

• An M2M area network comprises of machines (or M2M


nodes) which have embedded hardware modules for
sensing, actuation and communication.
• Various communication protocols can be used for M2M
local area networks such as ZigBee, Bluetooth, ModBus,
M-Bus, Wireless M-Bus, Power Line Communication
(PLC), 6LoWPAN, IEEE 802.15.4, etc.
• These communication protocols provide connectivity
between M2M nodes with in an M2M area network.

5
Anurag College of Engineering
Machine-to-Machine (M2M) cont’d

• The communication network provides connectivity to


remote M2M area networks.
• The communication network can use either wired or
wireless networks (IP- based).
• While the M2M area networks use either proprietary or
non-IP based communication protocols, the
communication network uses IP-based networks.
• Since non-IP based protocols are used within M2M area
networks, the M2M nodes within one network cannot
communicate with nodes in an external network.
• To enable communication between remote M2M area
networks, M2M gateways are used.
6
Anurag College of Engineering
M2M gateway
• Since non-IP based protocols are used within M2M area
networks, the M2M nodes within one network cannot
communicate with nodes in an external network.
• To enable communication between remote M2M area
networks, M2M gateways are used.
• Fig. Shows a block diagram of an M2M gateway

7
Anurag College of Engineering
Fig. Block Diagram of M2M Gateway
8
Anurag College of Engineering
M2M gateway cont’d
• The communication between M2M nodes and the M2M
gateway is based on the communication protocols
which are native to the M2M area network.
• M2M gateway performs protocol translations to enable
IP-connectivity for M2M area 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.

Anurag College of Engineering 9


M2M Application Domains
• The M2M data is gathered into point solutions such as
enterprise applications, service management
applications, or remote monitoring applications.
• M2M has various application domains such as
✓ Smart metering
✓ Home automation
✓ Industrial automation
✓ Smart grids, etc.
• M2M solution designs (such as data collection and
storage architectures and applications) are specific to
the M2M application domain.
Anurag College of Engineering 10
Difference between IoT and M2M
• Though both IoT and M2M involve networking of
machines or devices, they differ in the underlying
technologies, system architectures and type of
applications.
• The differences between M2M and IoT are:
1. Communication Protocols
2. Machines in M2M Vs Things in IoT
3. Hardware Vs Software Emphasis
4. Data Collection & Analysis
5. Applications

11
Anurag College of Engineering
Difference between IoT and M2M
The differences between M2M and IoT are:
1. Communication Protocols:
• M2M and IoT can differ in how the communication between
the machines or devices happens.
• M2M uses either proprietary or non-IP based
communication protocols for communication within the
M2M area networks.
• Commonly used M2M protocols include ZigBee, Bluetooth,
ModBus, M-Bus, Wireless M-Bus, Power Line
Communication (PLC), 6LoWPAN, IEEE 802.15.4, Z-Wave
etc.
• The focus of communication in M2M is usually on the
protocols below the network layer.
• The focus of communication in IoT is usually on the
protocols above the network layer such as HTTP, CoAP,
WebSockets, MQTT, XMPP, DDS, AMQP , etc.
12
Anurag College of Engineering
Difference between IoT and M2M
2. Machines in M2M Vs Things in IoT:
• The "Things" in IoT refers to physical objects that have
unique identifiers and can sense and communicate with their
external environment (and user applications) or their internal
physical states.
• The unique identifiers for things in IoT are the IP addresses
(or MAC addresses).
• Things have software components for accessing, processing
and storing sensor information or controlling actuators
connected.
• IoT systems can have heterogeneous things (eg. a home
automation IoT system can include IoT devices of various
types such as fire alarms, door alarms, lighting control
devices, etc).
• M2M systems, in contrast to IoT, typically have
homogeneous machine types within an M2M area network.
13
Anurag College of Engineering
Difference between IoT and M2M
3. Hardware Vs Software Emphasis:

• While the emphasis of M2M is more on hardware with


embedded modules, the emphasis of IoT is more on
software.
• IoT devices run specialized software for sensor data
collection , data analysis and interfacing with the cloud
through IP-based communication.

14
Anurag College of Engineering
Difference between IoT and M2M
4. Data Collection & Analysis:

• M2M data is collected in point solutions and often in on-


premises storage infrastructure.
• In contrast to M2M, the data in IoT is collected in the
cloud (can be public, private or hybrid cloud).

15
Anurag College of Engineering
Difference between IoT and M2M
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- premises enterprise applications.
• 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.
• Since the scale of data collection in IoT is so massive,
cloud-based real time and batch data analysis
frameworks are used for data analysis.
16
Anurag College of Engineering
Interoperability in IoT
Introduction:
• Internet of Things (IoT) is an ever-growing network
of physical devices embedded with sensors, actuators
and wire-less 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, etc. where a sensor node must support
heterogeneous sensors/actuators, and varying types of
wireless connectivity.

Anurag College of Engineering 17


Interoperability in IoT
• Interoperability is the ability of two or more devices,
systems, platforms or networks to work in conjunction.
• Interoperability is the ability of different systems, devices,
applications or products to connect and communicate in a
coordinated way, without effort from the end user.
• Interoperability enables communication between
heterogeneous devices or system in order to achieve a
common goal.
• Functions of interoperable components include data
access, data transmission and cross-organizational
collaboration regardless of its developer or origin.
18
Anurag College of Engineering
Interoperability in IoT
• The interoperability issues in IoT can be seen from
different perspectives due to heterogeneity.
• Heterogeneity is not a new concept nor restricted to a
domain. Even in the physical world there are many types
of heterogeneities. For example, people speak dissimilar
languages, but they can still communicate with each other
through a translator (human/tools) or by using a common
language.
• Likewise, the diverse elements comprising IoT (devices,
communication, services, applications, etc.) should
seamlessly cooperate and communicate with each other to
realize the full potential of IoT ecosystem.
19
Anurag College of Engineering
Interoperability in IoT

• In IoT interoperability can be seen from different


perspectives such as

✓ Device interoperability
✓ Networking interoperability,
✓ Syntactic interoperability,
✓ Semantic interoperability, and
✓ Platform interoperability.

20
Anurag College of Engineering
Device interoperability
• IoT is composed of a variety of devices.
• These devices, which are called “smart objects/things”,
may consist of high-end devices or low-end devices .
• The high-end IoT devices have enough resources and
computational capabilities such as Raspberry Pi and smart
phones.
• On the other hand, the low-end IoT devices are resource-
constrained in terms of energy, processing power and
communication capabilities than typical hosts such as
RFID tags, tiny and low-cost sensors and actuators, etc.
• The devices that want to exchange information may be
using different communication technologies which
requires interoperability between the different types of
heterogeneous devices that co-exist in the IoT ecosystem 21
Anurag College of Engineering
Device interoperability
• Device interoperability refers to enabling the
integration and interoperability of heterogeneous
devices with various communication protocols
and standards supported by heterogeneous IoT
devices.
• Device interoperability is concerned with
(i) the exchange of information between
heterogeneous devices and heterogeneous
communication protocols and
(ii) the ability to integrate new devices into any
IoT platform.
Anurag College of Engineering 22
Networking interoperability

• The networks that IoT devices will be operating on will


continue to be heterogeneous, multi-service, multi-vendor
and largely distributed.
• IoT devices generally rely on various short-ranged
wireless communication and networking technologies
which is rather more intermittent and unreliable.

23
Anurag College of Engineering
Networking interoperability
• Network level interoperability deals with mechanisms to
enable seamless message exchange between systems
through different networks (networks of networks) for
end-to-end communication.
• To make systems interoperable, each system should be
able to exchange messages with other systems through
various types of networks.
• Due to the dynamic and heterogeneous network
environment in IoT, the network interoperability level
should handle issues such as addressing, routing,
resource optimization, security, QoS, and mobility
support.
24
Anurag College of Engineering
Syntactical interoperability

• Syntactic interoperability refers to interoperation of the


format as well as the data structure used in any
exchanged information or service between
heterogeneous IoT system entities.
• An interface needs to be defined for each resource,
exposing some structure according to some schema.
WSDL and REST APIs are examples.
• The content of the messages need to be serialized to be
sent over the channel and the format to do so (such as
XML or JSON).

25
Anurag College of Engineering
Syntactical interoperability

• The message sender encodes data in a message using


syntactic rules, specified in some grammar.
• The message receiver decodes the received message
using syntactic rules defined in the same or some other
grammar.
• Syntactic interoperability problems arise when the
sender’s encoding rules are incompatible with the
receiver’s decoding rules, which leads to mismatching
message parse trees.

26
Anurag College of Engineering
Semantic interoperability
• Semantic interoperability is defined as “enabling
different agents, services, and applications to
exchange information, data and knowledge in a
meaningful way, on and off the Web”.
• The WoT addresses the current fragmentation by
exposing things and systems data and metadata
through API. But, such efforts have been hampered
because the corresponding parties need to share
knowledge of an API and many devices do not speak
the same language and cannot exchange across
different gateways and smart hubs .
Anurag College of Engineering 27
Semantic interoperability
• To be more precise, the data generated by things about
the environment may have a defined data format (e.g.
JSON, XML or CSV), but the data models and schemas
used by different sources are usually dissimilar and not
always compatible.
• Besides, the data may be represented in diverse units of
measurements and consist of other information.
• This semantic incompatibility between data models and
information models results in IoT systems not being able
to dynamically and automatically interoperate as they
have different descriptions or understandings of resources
and operational procedures, even if IoT systems expose
their data and resources to others .
Anurag College of Engineering 28
Platform interoperability
• Platform interoperability issues in IoT arises due to the
availability of diverse operating systems (OSs), programming
languages, data structures, architectures and access mechanisms
for things and data.
• There are currently many different OS developed specifically for
IoT devices such as Contiki, RIOT, TinyOS and OpenWSN , each
with several versions, to deliver services to users. Besides, the IoT
platform providers such as Apple HomeKit, Google Brillo,
Amazon AWS IoT, and IBM Watson provide different Oss,
programming languages, and data structures.
• For example, Apple HomeKit supports its own open source
language Swift, Google Brillo uses Weave, and Amazon AWS IoT
offers SKDs for embedded C and NodeJS.
• This non-uniformity causes hindrance for application developers
to develop cross-platform and cross-domain IoT applications. 29
Anurag College of Engineering
Platform interoperability
• Developers need to obtain extensive knowledge
of the platform specific APIs and information models of
each different platform to be able to adapt their
applications from one platform to another.
• A cross-platform IoT application can access different IoT
platforms and integrate data from various platforms.
• For example, consider the following application
scenario: a user who has health problems uses an IoT
cross-platform application every day to help him with his
everyday tasks. The IoT application connects to the
user’s smart health platform of wearable sensors to
continuously monitor his health conditions (heart rate,
fall situation, and glucose level) and in an emergency,
30
locates him and sends an ambulance. Anurag College of Engineering
Platform interoperability

• The application can also access a smart-city platform to


buy a ticket to the users desired destination and shows the
fastest route to the bus/train station. The cross-platform
interoperability between things and data in this scenario
enables interoperability across separate IoT platforms
specific to one vertical domain such as smart home, smart
healthcare, smart garden, etc.
• After cross-platform interoperability is enabled, cross-
domain interoperability can be achieved in which
different platforms within heterogeneous domains are
federated to build horizontal IoT applications

31
Anurag College of Engineering
Introduction to Arduino Programming
• One of the greatest advances in physical computing has
been the proliferation of microcontrollers.
• A microcontroller consists of a processor with a small
instruction set, memory, and programmable input/output
circuitry contained on a single chip.
• Microcontrollers are usually packaged with supporting
circuitry and connections on a small printed circuit board.
• Microcontrollers are used in embedded systems where small
software programs can be tailored to control and monitor
hardware devices, making them ideal for use in sensor
networks.
• PIC microcontroller, ARM microcontroller, 8051
microcontroller, AVR microcontroller, MSP microcontroller 32
Anurag College of Engineering
What is an Arduino?
• The Arduino is an open source hardware prototyping
platform supported by an open source software
environment.
• It was first introduced in 2005 and was designed with the
goal of making the hardware and software easy to use
and available to the widest audience possible.
• We do not have to be an electronics expert to use the
Arduino.
• The original target audience included artists and
hobbyists who needed a microcontroller to make their
designs and creations more interesting.

33
Anurag College of Engineering
What is an Arduino?
• Because of its ease of use and versatility, the Arduino
has quickly become the choice for a wider audience and a wider
variety of projects.
• We can use the Arduino for all manner of projects from reacting
to environmental conditions to controlling complex robotic
functions.
• The Arduino has also made learning electronics easier through
practical applications.
• Another aspect that has helped the rapid adoption of the Arduino
platform is the growing community of contributors to a wealth
of information made available through the official Arduino web
site (http://arduino.cc/en/).(“getting started” tutorial as well as a
list of helpful project ideas and a full reference guide to the C-
like language for writing the code to control the Arduino (called
34
a sketch). Anurag College of Engineering
Arduino

• Arduino also provides an integrated development


environment called the Arduino IDE.
• The IDE runs on our computer (called the host), where
we can write and compile sketches and then upload them
to the Arduino via USB connections.
• The IDE is available for Linux, Mac, and Windows.
• It is designed around a text editor especially designed for
writing code and a set of limited functions designed to
support compilation and loading of sketches.

35
Anurag College of Engineering
Arduino
• The Arduino supports a number of analog and
digital pins that we can use to connect to various devices
and components and interact with them.
• The mainstream boards have specific pin layouts, or
headers, that allow the use of expansion boards called
shields.
• Shields lets us to add additional hardware capabilities
such as Ethernet, Bluetooth, and XBee support to our
Arduino.
• The physical layout of the Arduino and the shield allow
us to stack shields.
• Thus, we can have an Ethernet shield as well as an XBee
shield, because each uses different I/O pins.
Anurag College of Engineering
36
Arduino Models
• A number of Arduino boards are available.
• Some are configured for special applications, whereas
others are designed with different processors and
memory configurations.
• Some boards are considered official Arduino boards
because they are branded and endorsed by Arduino.cc.
• Because the Arduino is open source and, more
specifically, licensed using a Creative Commons
Attribution Share-Alike license, anyone can build
Arduino-compatible boards (often called Arduino
clones).

37
Anurag College of Engineering
Arduino Models
• The basic layout of an Arduino board consists of a USB
connection, a power connector, a reset switch, LEDs for
power and serial communication, and a standard spaced
set of headers for attaching shields.
• The official boards sport a distinctive blue-colored PCB
with white lettering.
• With the exception of one model, all the official boards
can be mounted in a chassis (they have holes in the PCB
for mounting screws). The exception is an Arduino
designed for mounting on a breadboard.

38
Anurag College of Engineering
Arduino Models

Some of the more popular Arduino boards:

• Arduino Uno
• Arduino Leonardo
• Arduino Due
• Arduino Mega 2560
• Arduino Mini
• Arduino Micro
• Arduino Nano
39
Anurag College of Engineering
Arduino Uno

40
Anurag College of Engineering
Arduino Uno

Anurag College of Engineering 41


Arduino Uno
• The Uno board is the first standard Arduino board
featuring an ATmega328 processor;
• It has 14 digital I/O pins, of which 6 can be used as
PWM output; and 6 analog input pins.
• The Uno board has 32KB of flash memory and 2KB of
SRAM.
• The Uno is available either as a surface-mount device
(SMD) or a standard IC socket. The IC socket version
allows to exchange processors, that are desired to use an
external IC programmer to build custom solutions.
• It has a standard USB type B connector and supports all
shields.
42
Anurag College of Engineering
Arduino Leonardo
• The Leonardo board represents a bold leap forward
for the Arduino platform.
• It supports the standard header layout, ensuring the
continued use of shields.
• It also includes a USB controller that allows the board to
appear as a USB device to the host computer.
• The board uses a newer ATmega32u4 processor
• It has 20 digital I/O pins, of which 12 can be used as
analog pins and 7 can be used as a pulse-width
modulation (PWM) output.

.
Anurag College of Engineering 43
Arduino Leonardo
• It has 32KB of flash memory and 2.5KB of SRAM
• The USB connection uses a smaller USB connector. The
board is also available with and without headers.

.
Anurag College of Engineering 44
Arduino Due
• The Arduino Due is a new, larger, and faster board based
on the Atmel SAM3X8E ARM Cortex-M3 processor.
• The processor is a 32-bit processor, and the board
supports a massive 54 digital I/O ports, of which 14 can
be used for PWM output; 12 analog inputs; and 4 UART
chips (serial ports); as well as 2 digital-to-analog (DAC)
and 2 two-wire interface (TWI) pins.
• The new processor offers several advantages:
• 32-bit registers
• DMA controller (allows CPU-independent memory tasks)
• 512KB flash memory
• 96KB SRAM
45
• 84MHz clock Anurag College of Engineering
Arduino Due
• The new board has one distinct limitation:
unlike other boards that can accept up to 5V on the I/O
pins, the Due is limited to 3.3V on the I/O pins.
• The Arduino Due is intended to be used for projects that
require more processing power, more memory and more
I/O pins.
• Despite the significant capabilities of the new board it
remains open source and comparable in price to its
predecessors.

46
Anurag College of Engineering
Arduino Due

.
Anurag College of Engineering 47
Arduino Mega 2560
• The Arduino Mega 2560 is an older form of the Due.
• It is based on the ATmega2560 processor (hence the name).
• The board supports a massive 54 digital I/O ports, of which 14
can be used as PWM output; 16 analog inputs; and 4 UARTs
(hardware serial ports).
• It uses a 16MHz clock and has 256KB of flash memory.
• The Mega 2560 is essentially a larger form of the standard
Arduino (Uno, Duemilanove, etc.) and supports the standard
shields.
• Arduino Mega 256 is the board of choice for Prusa Mendel
and similar 3D printers that require the use of a controller
board named RepRap Arduino Mega Pololu Shield (RAMPS)
48
Anurag College of Engineering
Arduino Mega 2560

49
Anurag College of Engineering
Arduino Mini
• The Arduino Mini is a small form-factor board
designed for use with breadboards.
• Thus, it has all its pins arranged in male headers that plug
directly into a standard breadboard.
• It is based on the ATmega328 processor (older models use
the ATmega168) and has 14 digital I/O pins, of which 6 can
be used as PWM output, and 8 analog inputs.
• The Mini has 32KB of flash memory and uses a 16MHz
clock.
• Unlike other Arduino boards, the Mini does not have a USB
connector. To connect to and program the Mini, you must use
a USB Serial adapter or RS232-to-TTL serial adapter.
• The Mini has a limitation with regard to input voltage. We
should avoid voltages over 9V
Anurag College of Engineering
50
Arduino Mini

Anurag College of Engineering 51


Arduino Micro
• The Arduino Micro is a special form of the new
Leonardo board and uses the same ATmega32u4
processor with 20 digital I/O pins, of which 12 can be
used as analog pins and 7 can be used as PWM
output.
• It has 32KB of flash memory and 2.5KB of SRAM.
• The Micro was made for use on breadboards in the
same way as the Mini but in a newer, updated form.
But unlike the Mini, the Micro is a full-featured board
complete with USB connector. And like the
Leonardo, it has built-in USB communication,
allowing the board to connect to a computer as a
mouse or keyboard.
52
Anurag College of Engineering
Arduino Micro

53
Anurag College of Engineering
Arduino Nano
• The Arduino Nano is an older form of the Arduino
Micro.
• In this case, it is based on the functionality of the
Duemilanove4 and has the ATmega328 processor (older
models use the ATmega168) and 14 digital I/O pins, of
which 6 can be used as PWM output, and 8 analog
inputs.
• Like the Micro, it has all the features needed for
connecting to and programming via a USB connection.

54
Anurag College of Engineering
Arduino Nano

Anurag College of Engineering 55


Download and install Arduino IDE
• Start the internet browser and enter the
address arduino.cc and press enter.
• Go to “Download the Arduino IDE”
• It gives different options for installation according to the
operating system
• We can download Arduino IDE windows installer for
windows. We can download windows zip file for non-
admin installation. We can download and install windows
app for windows 8.1 or windows 10.
• There are also installation options for MAC operating
system and Linux Operating system.
(using windows, so click on windows zip file option).
56
Anurag College of Engineering
Download and install Arduino IDE
• On the next page, will give two options, whether we want
to contribute for this download or want a free download.
• If we want to contribute then click on “contribute and
download”. If we want it free then click on “just
download” option
• The zip file is downloaded.
• Now extract the zip file to a directory of your choice.
• Open the extracted folder and run the application file.
Within few seconds, we should see the IDE for the
Arduino. If we see the IDE, we can be fairly certain that
the software is downloading and installation was
performed successfully.
57
Anurag College of Engineering
ARDUINO PROGRAMMING:
• Arduino family boards uses Arduino IDE software
to make sketches (Arduino programs are called sketches)
• Sketches that are developed on Arduino IDE can be
transferred directly by connecting our computer via USB
port.
• IDE is compatible with Linux, MAC or Windows
operating system.
• Programming languages C and C++ are used.
• Thousands of preloaded sketches are easily available
which can be used in order to get Arduino to do
something according to our requirements.

58
Anurag College of Engineering
Basic Arduino Sketch Structure

59
Anurag College of Engineering
• Once the IDE launches, we see a simple
interface with a text editor area (a white background by
default), a message area beneath the editor (a black
background by default), and a simple button bar at the
top.
• The buttons are (from left to right) Compile, Compile
and Upload, New, Open, and Save.
• There is also a button to the right that opens the serial
monitor.
• We use the serial monitor to view messages from the
Arduino sent (or printed) via the Serial library.

60
Anurag College of Engineering
Arduino Sketch Structure
• A basic Arduino sketch consists of two functions called
(i) setup() and
(ii) loop().
• These two functions appear in a default new Arduino IDE
window.
• All functions must have a unique name, setup is one
example of a unique function name (setup and loop are
special functions in Arduino programming and form part of
the structure of a basic sketch).
• The function name is followed by opening and closing
parentheses () that may or may not contain something.
• All functions must have a return type.
Both setup and loop have a void return type.
• The body of a function consists of an opening and closing
brace ({ and }).
61
Anurag College of Engineering
"Hello, world!" Sketch Example

Anurag College of Engineering 62


• The "hello world" program simply writes the text
"Hello, world!" to the screen.
• The purpose of this program is to verify that the
programming environment is properly installed and
working.
• If the "hello world" program works, then you are ready
to start learning the new programming language.
• The Arduino doesn't have a screen to write the "hello
world" text to, but we can use the USB port and serial
monitor window.

63
Anurag College of Engineering
Parts of a Sketch
• The image below shows the parts of an Arduino sketch.
• Statements are lines of code that are executed as the
program runs.
• Each statement is terminated with a semicolon.

64
Anurag College of Engineering
Integration of Sensors and Actuators
with Arduino
Program :
To illuminate LED whenever the button is pushed

• The goal is to illuminate the LED whenever the


button is pushed.
• The project uses an Arduino UNO board, LED, a
breadboard, push button and some jumper wires. The
LED turns on and off by pushing the button through
the course of the loop() iteration.

65
Anurag College of Engineering
Program
To illuminate LED whenever the button is pushed

Fig. Sketch and the result of a successful compile operation.


66
Anurag College of Engineering
Fig. sample sketch and the result of a successful compile operation.

Note: At the bottom that it tells that we are programming an Arduino Uno board on a
specific serial port
Anurag College of Engineering 67
• One of the first things to be done when we start the
IDE is choose the board from the Tools ➤ Board menu.
• We see a number of boards available.
• Be sure to choose the one that matches our board.
• If we are using a clone board, check the manufacturer’s
site for the recommended setting to use.
• If we choose the wrong board, we typically get an error
during upload, but it may not be obvious that you’ve
chosen the wrong board.

68
Anurag College of Engineering
Fig. Choosing the Arduino board
Anurag College of Engineering 69
• After choosing board, the next thing need to
be done is to choose the serial port to which
the Arduino board is connected.
• To connect to the board, use the Tools ➤
Serial Port menu option.

Anurag College of Engineering 70


Fig. Choosing the serial port
Anurag College of Engineering 71
• Sometimes no serial ports are listed.
• This can happen if we haven’t plugged Arduino into
the computer’s USB ports (or hub).
• We had it plugged in but disconnected it at some
point (Or)
• We have not loaded the drivers for the Arduino
(Windows).
• Typically, this can be remedied by simply unplugging
the Arduino and plugging it back in and waiting until
the computer recognizes the port.

Anurag College of Engineering 72


• Now the Arduino IDE is installed, we can connect
Arduino and set the board and serial port.
• We see the LEDs on the Arduino illuminate. This is
because the Arduino is getting power from the USB.
Thus, we do not need to provide an external power
supply when the Arduino is connected to the computer.

73
Anurag College of Engineering
Hardware Connections
Assembling an Arduino:

• Be sure to disconnect (power down) the Arduino first.


• Any Arduino variant that has I/O pins can be used.
• Place one LED and one pushbutton on the breadboard.
• Wire the 5V pin to the breadboard power rail and the
ground pin to the ground rail, and place the pushbutton in
the center of the breadboard. Place the LED to one side
of the breadboard, as shown in Figure.

74
Anurag College of Engineering
Fig. Diagram of an LED with a pushbutton
75
Anurag College of Engineering
• Now wire a jumper from the power rail to one
side of the pushbutton, and wire the other side of the
pushbutton to (DIGITAL) pin 2 on the Arduino (located
on the side with the USB connector).
• Next, wire the LED to ground on the breadboard and a
150-Ohm resistor. The other side of the resistor should be
wired to pin 13 on the Arduino.
• A resistor is required to pull the button low when the
button is not pressed.
• Place a 10K Ohm resistor on the side of the button with
the wire to pin 2 and ground. The longest side of the LED
is the positive side. The positive side should be the one
connected to the resistor. Resistor is used to limit the
current to the LED. Anurag College of Engineering 76
Writing the Sketch
• The sketch needed for this project uses two I/O pins on
the Arduino: one output and one input.
• The output pin will be used to illuminate the LED, and
the input pin will detect the pushbutton engagement.
• Connect positive voltage to one side of the pushbutton
and the other side to the input pin. When we detect
voltage on the input pin, we tell the Arduino processor to
send positive voltage to the output pin.
• Here, the positive side of the LED is connected to the
output pin. As in Figure, the input pin is pin 2 and the
output pin is pin 13.
Anurag College of Engineering 77
Writing the Sketch

• A variable is used to store these numbers.


• Use the pinMode() method to set the mode of each pin
(INPUT, OUTPUT).
• We place the variable statements before the setup()
method and set the pinMode() calls in the setup() method,
as follows:

Anurag College of Engineering 78


Writing the Sketch
int led = 13; // LED on pin 13
int button = 2; // button on pin 2
void setup()
{
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}

Anurag College of Engineering 79


Writing the Sketch
• In the loop() method, place code to detect the button
press.
• Use the digitalRead() method to read the status of the pin
(LOW or HIGH), where LOW means there is no voltage
on the pin and HIGH means positive voltage is detected
on the pin.
• We also place in the loop() method the code to turn on
the LED when the input pin state is HIGH.
• In this case, we use the digitalWrite() method to set the
output pin to HIGH when the input pin state is HIGH and
similarly set the output pin to LOW when the input pin
state is LOW.
Anurag College of Engineering 80
Writing the Sketch
• The following shows the statements needed:
void loop()
{
int state = digitalRead(button);
if (state == HIGH)
{
digitalWrite(led, HIGH);
}
else
{ digitalWrite(led, LOW);
}
} Anurag College of Engineering 81
Compiling and Uploading
• Once the sketch is written, test the compilation using
the Compile button in the upper-left corner of the IDE.
• Fix any compilation errors that appear in the message
window.
• Typical errors include misspellings or case changes (the
compiler is case sensitive) for variables or methods.
• After fixing any compilation errors, click the Upload
button.
• The IDE compiles the sketch and uploads the compiled
sketch to the Arduino board.
• Track the progress via the progress bar at lower right,
above the message window. When the compiled sketch
is uploaded, the progress bar disappears.
Anurag College of Engineering
82
Testing the Sensor
• Once the upload is complete, what do we see on our Arduino?
• If we have done everything right, the answer is nothing. It’s
just staring back at with that one dark.
• Now, press the pushbutton. Did the LED illuminate? If so,
congratulations: you are an Arduino programmer!
• If the LED did not illuminate, hold the button down for a
second or two. If that does not work, check all of your
connections to make sure you are plugged in to the correct
runs on the breadboard and that your LED is properly seated
with the longer leg connected to the resistor, which is
connected to pin 13. On the other hand, if the LED stays
illuminated, try reorienting your pushbutton 90 degrees. We
may have set the pushbutton in the wrong orientation.
83
Anurag College of Engineering
Anurag College of Engineering 84
Anurag College of Engineering 85

You might also like