IoT Chapitre2

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

UNIVERSITE INTERNATIONALE DE RABAT

Academic year: 2023-2024


The components of an IoT application
The two components of an IoT application are the Internet and the thing. Let's
look at these two components in a bit more detail.
The Thing

The Thing part of IoT refers to a device that can interact with the physical
world. These devices are usually small, low-priced computers, running at low
speeds and using low power - for example, simple microcontrollers with
kilobytes of RAM (as opposed to gigabytes in a PC) running at only a few
hundred megahertz (as opposed to gigahertz in a PC), but consuming
sometimes so little power they can run for weeks, months or even years on
batteries.

2
The components of an IoT application
The Thing

The devices interact with the physical world, either by using sensors to
gather data from their surroundings or by controlling outputs or
actuators to make physical changes. The typical example of this is a smart
thermostat - a device that has a temperature sensor, a means to set a
desired temperature such as a dial or touchscreen, and a connection to a
heating or cooling system that can be turned on when the temperature
detected is outside the desired range. The temperature sensor detects that
the room is too cold and an actuator turns the heating on.

3
The components of an IoT application
The Thing

There are a huge range of different things that can act as IoT devices,
from dedicated hardware that senses one thing, to general purpose
devices, even your smartphone! A smartphone can use sensors to detect
the world around it, and actuators to interact with the world - for example
using a GPS sensor to detect your location and a speaker to give you
navigation instructions to a destination.

4
The components of an IoT application
The Internet

The Internet side of an IoT application consists of applications that the IoT
device can connect to to send and receive data, as well as other
applications that can process the data from the IoT device and help make
decisions on what requests to send to the IoT devices actuators.

One typical setup would be having some kind of cloud service that the IoT
device connects to, and this cloud service handles things like security, as
well as receiving messages from the IoT device, and sending
messages back to the device. This cloud service would then connect to
other applications that can process or store sensor data, or use the
sensor data with data from other systems to make decisions.

Devices also don't always connect directly to the Internet themselves via
WiFi or wired connections. Some devices use mesh networking to talk to
each other over technologies such as Bluetooth, connecting via a hub
device (e.g., a gateway) that has an Internet connection.

5
The components of an IoT application
The Internet

With the example of a smart thermostat, the thermostat would connect


using home WiFi to a cloud service running in the cloud. It would send the
temperature data to this cloud service, and from there it will be written to a
database of some kind allowing the homeowner to check the current and
past temperatures using a phone app. Another service in the cloud would
know what temperature the homeowner wants, and send messages back to
the IoT device via the cloud service to tell the heating system to turn on or
off.

6
The components of an IoT application
The Internet

An even smarter version could use AI in the cloud with data from other
sensors connected to other IoT devices such as occupancy sensors
that detect what rooms are in use, as well as data such as weather and
even your calendar, to make decisions on how to set the temperature in a
smart fashion. For example, it could turn your heating off if it reads from
your calendar you are on vacation, or turn off the heating on a room by
room basis depending on what rooms you use, learning from the data to be
more and more accurate over time.

7
The components of an IoT application
IoT on the Edge

Although the I in IoT stands for Internet, the devices don't have to connect
to the Internet. In some cases, devices can connect to 'edge' devices -
gateway devices that run on your local network meaning you can process
data without making a call over the Internet.

This can be faster when you have a lot of data or a slow Internet
connection, it allows you to run offline where Internet connectivity is not
possible such as on a ship or in a disaster area when responding to a
humanitarian crisis, and allows you to keep data private.

Some devices will contain processing code created using cloud tools
and run this locally to gather and respond to data without using an Internet
connection to make a decision.

=> This is referred to as Cloud offloading

8
The components of an IoT application
IoT on the Edge

One example of this is a smart home device such as an Apple HomePod,


Amazon Alexa, or Google Home, which will listen to your voice using AI
models trained in the cloud, but running locally on the device. These
devices will 'wake up' when a certain word or phrase is spoken, and only
then send your speech over the Internet for processing. The device will stop
sending speech at an appropriate point such as when it detects a pause in
your speech. Everything you say before waking up the device with the wake
word, and everything you say after the device has stopped listening will not
be sent over the internet to the device provider, and therefore will be
private.

9
The components of an IoT application
IoT Security

With any Internet connection, security is an important consideration. There


is an old joke that 'the S in IoT stands for Security' - there is no 'S' in IoT,
implying it is not secure.

IoT devices connect to a cloud service, and therefore are only as secure as
that cloud service - if your cloud service allows any device to connect then
malicious data can be sent, or virus attacks can take place. This can
have very real world consequences as IoT devices interact and control
other devices. For example, the Stuxnet worm manipulated valves in
centrifuges to damage them. Hackers have also taken advantage of poor
security to access baby monitors and other home surveillance devices.

Sometimes IoT devices and edge devices run on a network completely


isolated from the Internet to keep the data private and secure. This is
known as air-gapping.

10
A deeper dive into Microcontrollers
CPU

The CPU is the 'brain' of the microcontroller. It is the processor that runs
your code and can send data to and receive data from any connected
devices. CPUs can contain one or more cores - essentially one or more
CPUs that can work together to run your code.

CPUs rely on a clock to tick many millions or billions of times a second.


Each tick, or cycle, synchronizes the actions that the CPU can take. With
each tick, the CPU can execute an instruction from a program, such as to
retrieve data from an external device or perform a mathematical calculation.
This regular cycle allows for all actions to be completed before the next
instruction is processed.

The faster the clock cycle, the more instructions that can be
processed each second, and therefore the faster the CPU. CPU speeds
are measured in Hertz (Hz), a standard unit where 1 Hz means one cycle or
clock tick per second.

11
A deeper dive into Microcontrollers
CPU

CPUs execute programs using the fetch-decode-execute cycle. For every


clock tick, the CPU will fetch the next instruction from memory, decode it,
then execute it such as using an arithmetic logic unit (ALU). Some
executions will take multiple ticks to run, so the next cycle will run at the
next tick after the instruction has completed.

12
A deeper dive into Microcontrollers
CPU

Microcontrollers have much lower clock speeds than desktop or laptop


computers, or even most smartphones. The Arduino Uno for example has a
CPU that runs at 16MHz or 16,000,000 cycles per second.

An average PC or Mac has a CPU with multiple cores running at multiple


GigaHertz, meaning the clock ticks billions of times a second. Your PC can
thus run more than a hundred times faster than a microcontroller.

Each clock cycle draws power and generates heat. The faster the ticks,
the more power consumed and more heat generated. PC's have heat sinks
and fans to remove heat, without which they would overheat and shut
down within seconds. Microcontrollers often have neither as they run much
cooler and therefore much slower. PC's run off main power or large
batteries for a few hours, microcontrollers can run for days, months, or
even years off small batteries. Microcontrollers can also have cores that
run at different speeds, switching to slower low power cores when the
demand on the CPU is low to reduce power consumption.
13
A deeper dive into Microcontrollers
Memory

Microcontrollers usually have two types of memory - program memory and


random-access memory (RAM).

Program memory is non-volatile, which means whatever is written to it


stays when there is no power to the device. This is the memory that stores
your program code. It is usually flash storage.

RAM is the memory used by the program to run, containing variables


allocated by your program and data gathered from peripherals. RAM is
volatile, when the power goes out the contents are lost, effectively
resetting your program.

Like with the CPU, the memory on a microcontroller is orders of magnitude


smaller than a PC or Mac. A typical PC might have 8 Gigabytes (GB) of
RAM, or 8,000,000,000 bytes. A microcontroller would have only Kilobytes
(KB) of RAM. The Arduino Uno has only 32KB of Flash memory and 2KB of
RAM. That is more than 100,000 times LESS physical memory than a low-
end PC! 14
A deeper dive into Microcontrollers
Physical size

Microcontrollers are typically small in size, with the smallest, a Freescale


Kinetis KL03 MCU is small enough to fit in the dimple of a golf ball. Just
the CPU in a PC can measure 40mm x 40mm, and that's not including the
heat sinks and fans needed to ensure the CPU can run for more than a few
seconds without overheating, substantially larger than a complete
microcontroller. The Wio terminal developer kit with a microcontroller, case,
screen and a range of components isn't much bigger than a bare Intel i9
CPU, and substantially smaller than the CPU with a heat sink and fan!

Device Size
Freescale Kinetis KL03 1.6mm x 2mm x 1mm
Wio terminal 72mm x 57mm x 12mm
Intel i9 CPU, Heat sink and fan 136mm x 145mm x 103mm

15
A deeper dive into Microcontrollers
Frameworks and operating systems

Due to their low speed and memory size, microcontrollers don't run an
operating system (OS) in the desktop sense of the word. The operating
system that makes your computer run (Windows, Linux or macOS) needs a
lot of memory and processing power to run tasks that are completely
unnecessary for a microcontroller. Remember that microcontrollers are
usually programmed to perform one or more very specific tasks, unlike a
general purpose computer like a PC or Mac that needs to support a user
interface, play music or movies, provide tools to write documents or code,
play games, or browse the Internet.

To program a microcontroller without an OS you do need some tooling


to allow you to build your code in a way that the microcontroller can run,
using APIs that can talk to any peripherals. Each microcontroller is different,
so manufacturers normally support standard frameworks which allow you
to follow a standard 'recipe' to build your code and have it run on any
microcontroller that supports that framework.

16
A deeper dive into Microcontrollers
Real Time OS

You can also program microcontrollers using a specific OS - often referred


to as a real-time operating system (RTOS), as these are designed to
handle sending data to and from peripherals in real time. These operating
systems are very lightweight and provide features such as:

• Multi-threading, allowing your code to run more than one block of code
at the same time, either on multiple cores or by taking turns on one core

• Networking to allow communicating over the Internet securely

• Graphical user interface (GUI) components for building user interfaces


(UI) on devices that have screens

 The most important usage of a Real Time OS is to run multiple


programs, referred to as tasks, in a single MCU: it is the multi-threading
feature

 Some RTOS are Azure RTOS, Free RTOS, and Zephyr 17


A deeper dive into Microcontrollers
The Arduino Framework

Arduino is probably the most popular microcontroller framework,


especially among students, hobbyists and makers. Arduino is an open
source electronics platform combining software and hardware. You can buy
Arduino compatible boards from Arduino themselves or from other
manufacturers, then code using the Arduino framework.

Arduino boards are coded in C or C++. Using C/C++ allows your code to
be compiled very small and run fast, something needed on a constrained
device such as a microcontroller. The core of an Arduino application is
referred to as a sketch and is C/C++ code with 2 functions - setup and
loop. When the board starts up, the Arduino framework code will run the
setup function once, then it will run the loop function again and again,
running it continuously until the power is powered off.

18
A deeper dive into Microcontrollers
The Arduino Framework

You write your setup code in the setup function, such as connecting to
WiFi and cloud services or initializing pins for input and output. Your
loop code would then contain processing code, such as reading from a
sensor and sending the value to the cloud. You would normally include a
delay in each loop using the delay(durationMillis) function, for example,
if you only want sensor data to be sent every 10 seconds you would add a
delay of 10 seconds at the end of the loop so the microcontroller can
sleep, saving power, then run the loop again when needed 10 seconds
later.

=>This program architecture is known as an event loop or message loop.


Many applications use this under the hood and is the standard for most
desktop applications that run on OSes like Windows, macOS or Linux 19
A deeper dive into Microcontrollers
Input - Output

Microcontrollers need input and output (I/O) connections to read data


from sensors and send control signals to actuators. They usually contain
a number of general-purpose input/output (GPIO) pins. These pins can
be configured in software to be input (that is they receive a signal), or
output (they send a signal).

🧠⬅️ Input pins are used to read values from sensors

🧠➡️ Output pins send instructions to actuators

=> We use the pinMode function of the Arduino framework to configure a


GPIO pin

20
A deeper dive into Microcontrollers
Pinout of the Arduino UNO

I2C interface

SPI interface

Analog to
Digital
Converters Serial interface

21
A deeper dive into Microcontrollers
Pinout of the NodeMCU ESP8266

Analog to
Digital I2C interface
Converter

SD Card
Interface
SPI interface

Serial interface

22
A deeper dive into Microcontrollers
NodeMCU ESP8266 Digital Pins Guide

23
A deeper dive into Microcontrollers
GPIO Pins

The GPIO pins are digital pins that can be either input or output pins.

As output, they can either be on a HIGH or LOW voltage level:

• HIGH correspond to 5V for the Arduino UNO, or 3.3V for the


NodeMCU

• LOW corresponds to 0V, i.e. the ground voltage

On a HIGH state the GPIO pins can output a maximum current that
depends on the MCU:

• Max 40mA for the Arduino UNO

• Max 12mA for the NodeMCU

=> We have to ensure these max currents are not exceeded using
resistances to not damage the GPIO pin! 24
A deeper dive into Microcontrollers
GPIO output to control digital actuator

Digital actuators either have two states controlled by a high or low


voltage or have a Digital to Analog Converter built in so can convert a
digital signal to an analog one.

One simple digital actuator is an LED. When a device sends a digital signal
of 1, a high voltage is sent that lights the LED. When a digital signal of 0 is
sent, the voltage drops to 0V and the LED turns off.

More advanced digital actuators, such as screens require the digital data
to be sent in certain formats. They usually come with libraries that make it
easier to send the correct data to control them.

25
A deeper dive into Microcontrollers
Example of a current limiting resistance

Digital Output

1. The Digital Output pin is at 5V when in HIGH.


2. The RED LED has a voltage drop of 1.8V
3. The Resistor is thus subjected to 3.2V
4. The RED LED needs a current below 20mA
5. The Resistor will limit the current using Ohm’s law: I=U/R

 I = 3.2V/220ohm =14.5mA
26
A deeper dive into Microcontrollers
What to do when we need more current from an output pin?

Example of a DC motor that needs at least 350mA:

Solution: connect the pin to a MOSFET transistor, or a relay to control an


external voltage source connected to the motor!

27
A deeper dive into Microcontrollers
Analog actuators

Analog actuators take an analog signal and convert it into some kind of
interaction, where the interaction changes based off the voltage
supplied.

One example is a dimmable light, such as the ones you might have in your
house. The amount of voltage supplied to the light determines how bright it
is.

However, the actual IoT device works on digital signals, not analog.
This means to send an analog signal, the IoT device needs a digital to
analog converter (DAC), either on the IoT device directly, or on a
connector board. This will convert the 0s and 1s from the IoT device to an
analog voltage that the actuator can use.

=> The Arduino UNO and the NodeMCU don’t have a built-in DAC! 28
A deeper dive into Microcontrollers
PWM as an alternative to DAC for some analog actuators

Another option for converting digital signals from an IoT device to an analog
signal is pulse-width modulation. This involves sending lots of short
digital pulses that act as if it was an analog signal. For example, you can
use PWM to control the speed of a motor.

Imagine you are controlling a motor with a 5V supply. You send a short
pulse to your motor, switching the voltage to high (5V) for two hundredths of
a second (0.02s). In that time your motor can rotate one tenth of a rotation,
or 36°. The signal then pauses for two hundredths of a second (0.02s),
sending a low signal (0V). Each cycle of on then off lasts 0.04s. The cycle
then repeats:
• 25 pulses per second x 0.1 rotations per pulse = 2.5 rotations per second
• 2.5 rotations per second x 60 seconds in a minute = 150rpm

29
A deeper dive into Microcontrollers
PWM as an alternative to DAC for some analog actuators

You can change the motor speed by changing the size of the pulses. For
example, with the same motor you can keep the same cycle time of
0.04s, with the on pulse halved to 0.01s, and the off pulse increasing to
0.03s. A half length pulse only turns the motor one twentieth of a
rotation, and at 25 pulses a second will complete 1.25 rotations per second
or 75rpm.

When a PWM signal is on for half the time, and off for half it is referred to as
a 50% duty cycle. Duty cycles are measured as the percentage time the
signal is in the on state compared to the off state.

=> Only certain pins support PWM in the Arduino UNO: pins D3, D5, D6,
D9, D10, and D11 30
A deeper dive into Microcontrollers
Overview of Arduino functions for output GPIO pins

• pinMode(pinName, OUTPUT): configures the pin called pinName as an


output pin

• digitalWrite(pinName, HIGH): put the pin pinName to the HIGH state

• digitalWrite(pinName, LOW): put the pin pinName to the LOW state

• analogWrite(pinName, value): puts a PWM value (between 0, always


LOW, and 255, always HIGH) on pin pinName. 0

• tone(pinName, frequency, duration): outputs a square wave of a


certain frequency (in Hz) for a specific duration (in ms) The duration is
optional.

• noTone(pinName): stop the square wave generated by the tone function


on the pin pinName

31
A deeper dive into Microcontrollers
GPIO input

When GPIO pins are configured as inputs, they can measure a HIGH or
LOW signal as follows depending on the MCU:

• For Arduino UNO:


o HIGH: voltage over 2.4V
o LOW: voltage below 1.5V

• For NodeMCU:
o HIGH: voltage over 2.4V
o LOW: voltage below 0.8V

When configured as input, GPIO pins also have a high internal


resistance: almost no current flows through them to minimize any
interference with the measured voltage.

=> You have to be very careful with the voltage! You shouldn’t input more
than 5V in the Arduino UNO, or 3.3V in the NodeMCU!
32
A deeper dive into Microcontrollers
The voltage divider

In some cases, you have to convert the voltage of the HIGH level at an
input to that accepted by the MCU. This is especially the case if you use a
3.3V MCU like the NodeMCU with 5V devices. The simplest solution is to
use a voltage divider:

Vin Vout

R1

R2

The formula is: Vout = Vin * R2 / (R1+R2)


=> We use relatively high resistances to limit the current and thus the power
consumption. 33
A deeper dive into Microcontrollers
Digital sensors

The simplest digital sensor is a button or switch. This is a sensor with two
states, on or off:

GPIO pins on IoT devices can measure this signal directly as a 0 or 1.


If the voltage sent is the same as the voltage returned, the value read is 1
or HIGH, otherwise the value read is 0 or LOW. This is how we connect a
switch to an MCU:

Why this resistance?

34
A deeper dive into Microcontrollers
Pull-up and pull-down resistance

Why this resistance?

Imagine there is no resistance. When the switch is off, a problem may


happen: the digital input pin is connected to nothing, and so may read a
random garbage value that could be either HIGH or LOW.

We usually want to force the input value to a specific value when the pin
is disconnected: in this example, we want the value to be LOW when the
switch is off.

This is the function of the resistance: when the switch is off, the input pin is
connected to the ground, and so the voltage is forced to 0V. It’s a pull-
down resistance! 35
A deeper dive into Microcontrollers
Pull-up and pull-down resistance

We can also switch the position of the resistance and the button:

In this case, the input value will be LOW when the switch is on as the
input will be connected directly to the ground.

Here the resistance will force the input value to 5V, i.e. HIGH, when the
switch is off. It’s a pull-up resistance!

=> We use a relatively high resistance to limit the current used when the
switch is on. 10K is a value that works well for our voltage levels.
36
A deeper dive into Microcontrollers
Interrupts

Instead of always reading the value of the input during the loop, we can use
interrupts:

We can program the MCU to automatically call a routine when the state
of the input changes from LOW to HIGH (referred to as RISING), or from
HIGH to LOW (referred to as FALLING).

=> Only certain pins can be used for interrupts in the Arduino UNO: pins D2
and D3 37
A deeper dive into Microcontrollers
Debouncing switches and buttons

Real switches and buttons always have a certain amount of noise when
changing state:

This noise may cause the interrupts to be triggered multiple times.

=> The solution is called debouncing: we verify in the beginning of the


interrupt routine the last time the interrupt was called, if it is too close to the
current time (i.e., the duration is below a certain threshold), we ignore the
interrupt. We can use the millis() function to get the current time. 38
A deeper dive into Microcontrollers
Advanced digital sensors

More advanced digital sensors read analog values, then convert them
using on-board Analog to Digital Converters (ADC) to digital signals.

For example, a digital temperature sensor will use a thermocouple and


will measure the change in voltage caused by the resistance of the
thermocouple at the current temperature. Instead of returning an analog
value and relying on the device to convert to a digital signal, an ADC built
into the sensor will convert the value and send it as a series of 0s and 1s
to the IoT device. These 0s and 1s are sent in the same way as the digital
signal for a button with 1 being full voltage and 0 being 0v.

=> We usually interact with digital sensors using libraries 39


A deeper dive into Microcontrollers
Analog sensors

Some of the most basic sensors are analog sensors. These sensors
receive a voltage from the IoT device, the sensor components adjust
this voltage, and the voltage that is returned from the sensor is measured
to give the sensor value. One example of this is a potentiometer. This is a
dial that you can rotate between two positions and the sensor measures the
rotation.

The IoT device will send an electrical signal to the potentiometer at a


voltage, such as 5 volts (5V). As the potentiometer is adjusted it changes
the voltage that comes out of the other side. Imagine you have a
potentiometer labelled as a dial that goes from 0 to 11, such as a volume
knob. When the potentiometer is in the full off position (0) then 0V will
come out. When it is in the full on position (11), 5V will come out. 40
A deeper dive into Microcontrollers
Analog to digital conversion

IoT devices are digital - they can't work with analog values, they only work
with 0s and 1s. This means that analog sensor values need to be
converted to a digital signal before they can be processed. Many IoT
devices have analog-to-digital converters (ADCs) to convert analog
inputs to digital representations of their value.

Imagine you have an analog light sensor connected to an IoT device that
uses 3.3V and is returning a value of 1V. This 1V doesn't mean anything
in the digital world, so needs to be converted. The voltage will be
converted to an analog value using a scale depending on the device and
sensor. One example is the Arduino ADC which outputs values from 0 to
1023. For this case, running at 3.3V, a 1V output would be a value of 300.

The highest value of the ADC (corresponding to 1023) is called the


analog reference, and depends on the ADC: for the Arduino UNO it is 5V,
for the NodeMCU, it is 3.3V. It can also be changed to another value using
the analogReference function (as long as it is below the max voltage of the
pins!!!) 41
A deeper dive into Microcontrollers
Overview of Arduino functions for input pins

• pinMode(pinName, INPUT): configures the pin called pinName as an


input pin

• pinMode(pinName, INPUT_PULLUP): configures the pin called


pinName as an input pin with an internal pull-up resistance (it means you
don’t need to add a pull-up resistance).

• digitalRead(pinName): read and return the value (HIGH or LOW) from


the pin pinName

• attachInterrupt(digitalPinToInterrupt(pinName), routine, mode):


attach the interrupt function routine (has no arguments and returns
nothing) to the pin pinName. The interrupt is triggered based on the
mode value: RISING, FALLING, CHANGE

• analogRead(pinName): read and return the analog value (between 0


and 1023, 0 corresponding to 0V, and 1023 corresponding to the analog
reference) 42
Summary

• We saw:
o The different components of an IoT application
o The characteristics of IoT devices
o IoT devices contain a programmable microcontroller
o How the Arduino framework works
o Digital actuators in Arduino:
▪ GPIO outputs
▪ Current limiting resistance
o Analog actuators in Arduino
▪ PWM
o Digital sensors in Arduino:
▪ GPIO input
▪ Voltage divider
▪ Pull-up or pull-down resistance
▪ Interrupts
▪ Button debouncing
o Analog sensors in Arduino:
▪ Analog-to-digital conversion
43

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy