You are on page 1of 41

Arduino and the Internet

of Things

INTRODUCTION

Internet of Things
Lets deconstruct it term by term:

A thing is any object or device besides a traditional computer, that has some
features and performs some function, for example, a refrigerator or air
conditioner.

To this thing, we add computational intelligence to improve its functionality.

The computational intelligence could be of any type, typically a microcontroller that


runs some code.

We then add network connectivity internet connection to enhance its


function further.

This device is called an IoT device, a thing that doesnt look like a computer,
but has a computer hidden within it.

Cloud

Thing

Sensors

Actuators

Thing
Sensors

Actuators

An intelligent device vs IoT


device

An intelligent air conditioner might be able to perform tasks like

Using sensors to detect human presence in the room, and switching on or off on
that basis.

Beeping or hooting when it requires servicing or repair.

Thus an intelligent device uses local sensors to collect physical data or requires
locally stored data to enhance the device functionality without network
connectivity

An IoT air conditioner has access to the internet and hence can:

Acquire information about when you come home via GPS and switch on a few
minutes before you arrive so that the temperature is set to an ambient value.

Monitor external weather conditions from weather data sites to improve its own
efficiency and to determine the inside temperature.

Book an appointment for servicing and repair online, instead of just indicating
need for repair.

Give your information to sites that market to you.

Another example : Scanning and


Logistics

Logistics and tagging: From 1970s, barcode scanning came into picture
to keep track of boxes and products.

RFID tag is an electronic version of barcode.

It can store not only a unique identity tag for a box, but also data
related to when and where the box moved over time.

A barcode cant change over time while an RFID can have flash
memory that can change.

Can be easily network connected.

How is IoT device different from a


computer ?
Computer

IoT device

A computer executes program to perform computation.

Has a main function apart from


computation, and uses computation only
to improve that main functionality.

A computer is general-purpose. Not particularly efficient for


a particular operation but generally good at everything.

You can use a phone to capture a photo as well as make


phone calls, but a dedicated DSLR can do the job of
capturing a photo better.

For example, a car is meant to drive


people around, and this function is
enhanced by computational intelligence.
IoT devices are special-purpose thingssoftware and hardware are efficient for
one task but inefficient for others.

Technological trends leading to IoT

Reduction in cost - computational technology is lot cheaper

Reduction in size smaller, lighter technology that can be easily


incorporated in several things

Increased computational ability computers can do more and more


things they werent able to few years back, and at higher speed.

Improved internet access (also wireless internet access)

Risks : Privacy and Security

Dependency on devices, not people can lead to social isolation.

Power outage, device failure or network failure causes problems.

Bugs in software can be dangerous as people depend on devices like


a pacemaker for their lives.

Privacy : IoT devices continuously monitor, observe and record our


data from location to shopping preferences, from purchases to call
history.

Security : The data acquired is stored in cloud servers and potentially


sensitive data may be accessed by unwanted people or organizations if
it is not securely encrypted.

INTERNET OF
THINGS

EMBEDDED SYSTEMS

Building an IoT device

An IoT device is a thing with computational technology embedded


within it, which improves its function without increasing complexity.

User friendly interface; without adding complexity to the thing or


device

The device interacts with the physical world through sensors, like
temperature sensors, humidity sensors, piezoelectric sensors, vibration
sensors, RFID sensors etc.

The device also has actuators that accordingly change the state of
the physical world, such as lights, motors etc.

An IoT device is hence an embedded system.

Embedded Systems

Computer based systems that do not appear as computers they perform


complex computing but have a simple interface.

IoT devices are typically embedded systems.

One difference is that IoT devices are almost always connected to the
internet, whereas a general embedded system may not necessarily be.

Sometimes embedded systems dont directly interact with the user they
interact with another device, for example disk drives, anti-lock braking
systems in cars, pen-drives etc.

Unlike a computer which does multiple tasks, embedded systems are


application-specific i.e. they perform one task or set of tasks.

Hardware and software are co-designed. This makes design process more
efficient.

Efficiency is paramount

Embedded systems are not designed to only perform a task, but to do


it at high speed, with low power consumption and greater efficiency.

This is because most embedded products are

Cost-critical : manufacturing cost, design cost, time-to-market

Life-critical : medical or safety devices on which life of user depends, such


as pacemaker, car-reverse system etc. Performance and power key
constraints.

GENERIC
EMBEDDED
SYSTEM
STRUCTURE

Thermistor : reports
temperature
Photoresistor : light
intensity

CMOS camera :
captures images

IP (Intellectual
property) core
A special purpose IC
that performs a
specific task.
Cheap in high volume
manufacture
Useful for common
tasks like network
controllers
Most interact with the
microcontroller

LEDs
LCD displays
Seven- segment display
Servo motors
dc motors

Field Programmable
Gate Arrays

Hardware than can be


reconfigured via RAM
Hardware design using
programs
No fabrication needed.

What is a microcontroller ?

A microcontroller is a small computer on a single chip; it includes a


processor, memory as well as input/output. It sends commands and
receives data.

It is usually embedded inside a device that it controls.

Compared to a microprocessor (an intel or amd), a microcontroller has


less memory and is slower, although its efficiency is great for the
specific task it has to perform.

A microcontroller is connected to other hardware components.

It needs to be programmed the code is written on a host


machine(laptop or desktop) and transferred to the microcontrollers flash
memory.

ADC

Most physical phenomenon detected or measured by sensors are


analog signal or continuous signals that are needed to be converted to
discrete digital states that a microcontroller can use.

Analog to digital convertors are hence used to interface the


microcontroller with analog sensors.

Similarly, DAC( Digital To Analog) conversion is used to interface with


analog actuators such as speakers which convert digital output to
analog.

Arduino :
Introduction

Development Board

A development board is a PCB that includes, along with the


microcontroller, hardware components needed for programming the
board using host data.

Arduino, Raspberry Pi, Beagle bone are examples of development


boards.

The Arduino

Arduino is an open-source (with respect to both hardware and


software) platform for prototyping hardware using the Atmel ATmega
series of microcontrollers.

It uses its own coding environment, the Arduino IDE, and the programs
written for Arduino are called sketches.

Sketch

Two default methods that are part of any Arduino sketch are:

void setup( ) //runs first time as code is executed, and only once

void loop( ) //runs iteratively until program is terminated

Pins on the Arduino can be used as both input and output, specified by
the function pinMode( ) in setup.

Other functions like digitalWrite( ) delay( ) etc.


have been put together in a table below.
SYNTAX

USE

void pinMode( pinNumber,


OUTPUT/INPUT)

To declare a pin as an input/output pin in


setup()

boolean digitalRead( pinNumber)

To read the output of a pin as HIGH or LOW

void delay( milliseconds)

Waits for specified milliseconds before


executing consequent line of code

void digitalWrite( pinNumber,


HIGH/LOW)
void delayMicroseconds(museconds)

Writes a HIGH/LOW to specified pin


If delay time is to be specified in
microseconds

LED blinking on and of

Arduino
2

BUTTONS, PWM AND


FUNCTIONS

PWM

PWM (Pulse Width Modulation) is used for Analog to digital conversion,


because the Arduino can only work with digital data. It digitises a
continuous signal into discrete levels or states. Built-in PWM circuits are
available on pins 3, 5,6,9,10,11 which are labelled with ~.

The discrete states can be from 0 to 1023(210 =1024 states, Uno uses
10 bits). Analog outputs have states only from 0 to 255 ( 2 8 = 256
states).

analogRead(pin No) returns an integer b/n 0 to 255 corresponding to


the voltage level.

// Keeping LED on when button is held down


int switchPin = 8;
int ledPin = 13;
void setup( )
{

pinMode( switchPin , INPUT);


pinMode( ledPin, OUTPUT);

}
void loop( )
{

if( digitalRead(switchPin) = = HIGH)


digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);

Switch Bounce

Switch bounce: The signal which the pin receives as HIGH or LOW
oscillates during the initial pressing of a button which is called bouncing
in the signal till a stable output is reached.

Bounce can cause anomalous output, but that can easily be prevented
by a debounce, implemented in either hardware or software. A
debounce code leading to normal switching operation on successive
button presses is as follows:

// LED turning on/off on successive button presses, debounced


int switchPin = 8;
int ledPin = 13;
boolean lastButton = LOW;
//boolean variables can take values LOW/HIGH 1/0 or true/false
boolean currentButton = LOW;
// to use with debounce function
boolean ledOn = false;
void setup( )
{
pinMode( switchPin , INPUT);
pinMode( ledPin, OUTPUT);
}
boolean debounce (boolean last)
{
boolean current = digitalRead( switchPin);
if ( last != current)
{
delay(5);
// waiting for the switch to stop bouncing
current = digitalRead(switchPin);
}
return current;
}
void loop( )
{
currentButton = debounce( LastButton);
if(lastButton = = LOW && currentButton = = HIGH)
ledOn = !ledOn;
lastButton = digitalRead(switchPin);
digitalWrite( ledPin, ledOn);
}
//end of sketch

Analog Outputs

In order to change brightness levels of LED or other operations


involving more discrete states than the two binary offers , analog inputs
and outputs are required.

To deal with these the analogRead() and analogWrite( ) functions are


provided

SYNTAX

USE

int analogRead(pin No)

Returns integer value corresponding to


discrete state of output pin

void analogWrite( pinNumber,


value)

Value is the duty cycle, an integer b/n 0


and 255
(128 for a 50% duty cycle)

Arduino
3

ANALOG INPUTS

Analog Inputs and Outputs

Most physical phenomenon detected or measured by sensors are analog


signal or continuous signals that are needed to be converted to discrete
digital states that a microcontroller can use.

Analog to digital convertors are hence used to interface the microcontroller


with analog sensors.

Most boards, including UNO, use 10 bit ADCs i.e. for a 5V reference voltage,
0V corresponds to 0, and 5V corresponds to state 1023, with 2 10 =1024
discrete states in between.

Analog outputs, however, have states only from 0 to 255 (2 8 = 256 states).

This 5V reference voltage is default. It can be specified by


analogReference(DEFAULT); in setup function, though this isnt necessary.

Analog Inputs and Outputs

The 5 V reference can be changed by connecting the AREF pin ( Analog


reference) to an external reference voltage, say, 3.3 V or 1.1 V, which
can give different or better points of resolution for lower voltage
requiring circuits.

The Arduino Uno has 6 analog inputs, labelled A0 to A5.

An analog input can be taken from analog premade sensors, or from the
variable resistor- potential divider combination.


Referring to potentiometer diagram, if R2 is fixed and R1 is
the photo resistor, then
If R2 is the photo resistance while R1 is fixed, then
The photoresistance varies inversely with incident light, i.e.
as light intensity increases the phtoresistance decreases.
Taking the first potential divider combination, output
voltage decreases as incident light decreases.

Syntax

Use

Serial.begin(9600)

Sends data serially to computer at


specified speed; 9600 is a commonly
used value
Prints to the serial monitor, prints in
the same line when repeatedly
executed
Prints to the serial monitor, always
ends execution with a newline

Serial.print()
Serial.println()
Serial.read()
map( value, a , b , 255, 0)

Reads from the serial monitor


Linearly maps values between a and b
to states b/n 0 and 255.

Analog potentiometer reading

Serial Monitor

Arduino IDE

Serial Communication

Compiling a sketch converts the program into bits (machine-level)


which is transmitted serially via USB cable to the Arduino board, in
other words, uploading to the board.

When Arduino is receiving data the LED marked RX blinks, while the
one marked TX blinks while transmitting data.

You might also like