IoT Chapitre2
IoT Chapitre2
IoT Chapitre2
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
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.
8
The components of an IoT application
IoT on the Edge
9
The components of an IoT application
IoT Security
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.
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.
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
12
A deeper dive into Microcontrollers
CPU
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
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.
16
A deeper dive into Microcontrollers
Real Time OS
• 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
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.
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.
On a HIGH state the GPIO pins can output a maximum current that
depends on the MCU:
=> 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
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
I = 3.2V/220ohm =14.5mA
26
A deeper dive into Microcontrollers
What to do when we need more current from an output pin?
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
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 NodeMCU:
o HIGH: voltage over 2.4V
o LOW: voltage below 0.8V
=> 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 simplest digital sensor is a button or switch. This is a sensor with two
states, on or off:
34
A deeper dive into Microcontrollers
Pull-up and pull-down resistance
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:
More advanced digital sensors read analog values, then convert them
using on-board Analog to Digital Converters (ADC) to digital signals.
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.
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.
• 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