Professional Documents
Culture Documents
Embedded Systems
CS3237
BOYD ANDERSON
SCHOOL OF COMPUTING
Housekeeping
2
Housekeeping
§ You should start thinking about what you would like to do for your
project!
§ Labs start next week on Monday!
§ You will be collecting your hardware (your thing for your internet
of things!) and sensors next week in your lab session.
§ After lecture Quiz will open at around 3:30 pm today.
§ You can interrupt me at any time and ask questions, or ask them in
chat, or put up your hand.
3
Embedded Systems
4
What is an Embedded System?
Embedded systems are special-purpose dedicated computing
systems embedded in other systems or environments. They
commonly take the form of SoCs (System-on-a-Chip). And they are
everywhere!
5 5
In all shapes and sizes…
Arduino mini WeMos D1 Nordic nRF52832 SoC Broadcom/pi-0 SoC
Example Board
Comms External 802.11 b/g/n BLE 5.3, Zigbee BLE 4.2, 802.11 b/g/n
Idle (@ 3.7V) / Normal Use <1μA / 10mA <1μA / 10mA <1μA / 10 mA 80mA / 200mA
GPIO 14 I/O, 6 A in 11 I/O, 1 A in 32 I/O, 4 A in 40 I/O
OS Bare-metal/RTOS Bare-metal/RTOS Bare-metal/SoftDevice Linux
6 6
By contrast:
Arduino mini WeMos D1 Nordic nRF52832 SoC Broadcom/pi-0 SoC
Example Board
Comms External 802.11 b/g/n BLE 5.3, Zigbee BLE 4.2, 802.11 b/g/n
Idle (@ 3.7V) / Normal Use <1μA / 10mA <1μA / 10mA <1μA / 10 mA 80mA / 200mA
GPIO 14 I/O, 6 A in 11 I/O, 1 A in 32 I/O, 4 A in 40 I/O
OS Bare-metal/RTOS Bare-metal/RTOS Bare-metal/SoftDevice Linux
7 7
In all shapes and sizes…
Arduino mini WeMos D1 Nordic nRF52832 SoC Broadcom/pi-0 SoC
Example Board
Comms External 802.11 b/g/n BLE 5.3, Zigbee BLE 4.2, 802.11 b/g/n
Idle (@ 3.7V) / Normal Use <1μA / 10mA <1μA / 10mA <1μA / 10 mA 80mA / 200mA
GPIO 14 I/O, 6 A in 11 I/O, 1 A in 32 I/O, 4 A in 40 I/O
OS Bare-metal/RTOS Bare-metal/RTOS Bare-metal/SoftDevice Linux
8 8
Processor Limitations
Arduino mini WeMos D1 Nordic nRF52832 SoC Broadcom/pi-0 SoC
Example Board
Comms External 802.11 b/g/n BLE 5.3, Zigbee BLE 4.2, 802.11 b/g/n
Idle (@ 3.7V) / Normal Use <1μA / 10mA <1μA / 10mA <1μA / 10 mA 80mA / 200mA
GPIO 14 I/O, 6 A in 11 I/O, 1 A in 32 I/O, 4 A in 40 I/O
OS Bare-metal/RTOS Bare-metal/RTOS Bare-metal/SoftDevice Linux
9 9
Memory Limitations
Arduino mini WeMos D1 Nordic nRF52832 SoC Broadcom/pi-0 SoC
Example Board
Comms External 802.11 b/g/n BLE 5.3, Zigbee BLE 4.2, 802.11 b/g/n
Idle (@ 3.7V) / Normal Use <1μA / 10mA <1μA / 10mA <1μA / 10 mA 80mA / 200mA
GPIO 14 I/O, 6 A in 11 I/O, 1 A in 32 I/O, 4 A in 40 I/O
OS Bare-metal/RTOS Bare-metal/RTOS Bare-metal/SoftDevice Linux
10 10
Other Limitations
Arduino mini WeMos D1 Nordic nRF52832 SoC Broadcom/pi-0 SoC
Example Board
Comms External 802.11 b/g/n BLE 5.3, Zigbee BLE 4.2, 802.11 b/g/n
Idle (@ 3.7V) / Normal Use <1μA / 10mA <1μA / 10mA <1μA / 10 mA 80mA / 200mA
GPIO 14 I/O, 6 A in 11 I/O, 1 A in 32 I/O, 4 A in 40 I/O
OS Bare-metal/RTOS Bare-metal/RTOS Bare-metal/SoftDevice Linux
11 11
Development Tools / Language
In this module we will program our microcontrollers using c/c++
Debugging can be quite limited, not what you might be used to in a
modern IDE.
Debugging has a memory/cpu overhead
You can write low or high level code!
More control over the hardware but at the cost of abstraction
Bare-metal or an RTOS.
12 12
Ok fine, I believe you, but how does this relate to IoT?
Our Thing, or Edge Device is:
an embedded system + sensors/actuators + internet connectivity
13 13
The Thing
14
Our Hardware Platform: WeMos D1
Microprocessor ESP8266
Bitsize 32
Clock Speed 80 MHz
Cycles per Instruction Varies 1 to N
RAM 80K
Program Memory External, 4 Mb
15
The Processor: ESP8266
16
IoT Device (Thing): A Simple Embedded System
§ Sensor reads in analog data that is converted to digital form using Analog to
Digital Converter (ADC)
§ Actuator receives digital signal that is converted to electrical or electro-
mechanical signals through Digital to Analog Converter (DAC)
§ Controller contains a processor
§ Controller is connected to the Internet (directly or via Gateway)
17
Output and Inputs and Timers… oh my!
18
General-Purpose Input/Output (GPIO)
Most processors have
“pins” which can be read
(used as an input) or set
(used as an output).
19
How do we use them?
They are memory mapped,
that is, we can write to a
specific register and control
them programmatically.
20
An example hardware layout
§ Components:
• Breadboard
GND • 1 x LED (any color)
Pin
Pin D4 • 1 x 220Ω resistor
digitalWrite(LED, LOW);
delay(100); § delay( ms )
}
• ms = millisecond
22
They are not created equally..
Notice some of the pins
have many different names
or functions?
PWM?
I2C?
Serial Port TXD? RXD?
SPI?
Analog Pin?
23
Timer Programming
Timers are important in real-time systems:
24
Hardware Timers
As such, hardware timers are implemented in many microprocessors
The ESP8266 has two timers but one (timer0) is used by the
WiFi. The other timer (timer1) is a 23-bit timer and because it is a
limited resource, there exist libraries to handle this on the ESP8266.
https://www.arduino.cc/reference/en/libraries/esp8266timerinterrupt/
25
Motivation
§ Imagine your Processor is running some code:
• However, if the external emergency stop button is pressed, you are supposed
to stop processing immediately
§ Possible solution:
• Keep checking whether the event occurs, BUT this is not very efficient (time
is wasted)
26
Interrupt: What is it?
§ Most processors provide alternative mechanisms to handle these
events:
• e.g. interrupt
27
Interrupt: WeMos D1
§ Interrupt handler in Arduino Language is known
as Interrupt Service Routine (ISR)
§ The digital pins each given a specific interrupt
number:
RISING
Triggers when the pin goes
from low to high
FALLING
Triggers when the pin goes
from high to low
LOW
Triggers when the pin is low CHANGE
Triggers when the pin
changes value
29
Writing ISR: Rules of Thumb
1. Make ISR as short as possible
• Common trick: Set a flag / update value in the ISR and let the main loop to
do the time consuming part
2. Do not use delay() in ISR
3. Do not use Serial read/write in ISR
4. If a variable is shared between ISR and other code, declare it as
volatile:
• Ensure correct compiler handling
§ Caution: When an ISR is executing, all other interrupts have to
wait!
• May miss important event!
30
Sensors and Actuators
31
Sensors / Actuators
1 x Infrared sensor receiver module
1 x Temperature and humidity sensor module
1 x Infrared emission sensor module
1 x Detect heartbeat module
1 x Microphone module
1 x Flame sensor module
…
32
Digital Input and Sensors
Digital Input
• Example: On/off switch
• Push-button switch: reverse for
normally closed switch
• Push on/off switch: must be
depressed again to release the
connection
33
Contact Bounce of Mechanical Switch
§ Contact bounce: Mechanical switches do not make clean transition
between on/off positions; makes/breaks contact multiple times
for tens of milliseconds
§ Micro-controller is fast and can recognize each switch bounce
§ De-bouncing solutions:
• Hardware de-bounce circuit
• Software technique: insert 30 ~ 50 ms delay in the function responding to
the port pin changes
Contact bounce
34
Analog Sensor
§ Provides a DC voltage that is proportional to the physical parameter being
measured
§ Examples
• Temperature sensor uses the fact that as temperature increases, the voltage
across a diode changes at a known rate
• Flex sensor provides a change in resistance for a change in sensor flexure
35
Digital Sensors measuring an Analogue World
We live in an analogue world… but computers are discrete and finite
36 36
Analog and Digital
• Computers can only handle digital signals.
• The conversion between analog and digital signals are needed.
• ADC performs analog to digital conversion while DAC performs the opposite.
Sensors Actuators
Edge Device
ADC DAC
37
Continuous Time Signal
§ A continuous time signal is defined over all instances of time
§ A function of time (the real number) to the co-domain X (the range
of possible values)
38
Discrete Signals
§ A discrete signal or discrete-time signal is a sequence or series of
signal values defined in discrete points of time
§ The distance in time between each point of time is the time-step
39
Sampling
§ Discrete signal can be acquired by sampling an analog signal (e.g.,
temperature, pressure) at constant or variable rate
40
Digital Signal: Quantization
§ A digital signal is a discrete signal for which not only the time but
also the amplitude has been made discrete
§ Analog-digital converter (ADC) samples and quantizes an analog
signal to a digital signal
41
Analog-to-Digital Conversion
§ Analog signal may be first pre-processed by external analog
hardware such that it falls within the reference voltage range of
the conversion system.
§ Analog voltage is then converted to corresponding binary
representation using Analog-to-Digital Converter (ADC)
42
ADC: Quantization and Encoding
§ Input voltage signals are typically mapped in the range 0-5 volts
§ b bit allows to divide the input signal range into 2b different
quantization levels
• Increased quantization level improves the accuracy
• Quantized signal is encoded, i.e., quantization level is represented in binary number
§ Resolution: voltage distance between two adjacent quantization levels
43
Light Sensor: Light Dependent Resistor
§ Basic idea:
• Luminosity (Brightness)
changes the resistance è
voltage change
44
Light Sensor: Connection
SIG è
Pin A0
any 5v supply
pin
45
Light Sensor: An example program
#define LDR_PIN A0
§ analogRead( pin ) void setup() {
• pin = pin number Serial.begin(9600);
}
• Returns a value from 0 to 1023
void loop() {
int lightLevel;
§ WeMos has only 1 analog lightLevel = analogRead(LDR_PIN);
input Pin (A0) Serial.println(lightLevel);
delay(500);
}
46
Interfacing with Components
§ WeMos can interface with many electronic components
§ Common steps:
1. Connect the electronic components to the correct pin(s)
• Usually wiring diagram is provided
47
Output Devices and Actuators
§ Digital Output
• Example: Light Emitting Diodes (LEDs)
• Informs the presence of logic 0 or logic 1 at a specific pin of a micro-
controller
§ Analog Actuators
• DC motor
• Rotates when DC power supply of suitable current rating is applied
• Speed of motor proportional to applied voltage
• Servo motor
• Control angular rotation for an applied pulse
width modulation (PWM) duty cycle
• Used to change mechanical positions,
e.g., steering angle of wheel
48
Pulse Width Modulation (PWM)
§ Method to create an analog signal from a digital processor
§ The main idea is to keep the signal high for an amount of time
proportional to the amplitude of the required analog signal during
each period of the digital pulse
§ Most micro-controllers can generate PWM signals using timers
49
PWM concepts
§ Switch output signal between ON and OFF periodically
§ Period (P)
• Time it takes for a signal to complete an ON-OFF cycle
§ Pulse width (T)
• Time the signal is active per period
§ Duty cycle (D)
• Percentage of a period in which the signal is ON or active
!
• D = " ×100%
50
PWM Illustration
51
PWM Usage – Dimming light, DC motor
PWM for LED dimming to 25% brightness PWM for DC motor to control speed
52
First lab: Getting started with your WeMOS
§ Labs start next week on Monday!
• Start programming your WeMOS
• Set up some simple circuits
• Control an actuator!
• Use some sensors!
§ You will be collecting your hardware (your thing for your internet
of things!) and sensors next week in your lab session.
§ After lecture Quiz will open at around 3:30 pm today.
53
What’s next week?
More Sensors and Actuators
Serial communication
RTOS
54
Summary
§ IoT devices consists of sensors, actuators, and a micro-controller
§ Micro-controllers have “pins” whose status can be set by software
§ Sensors and actuators connect the cyber world to the physical
world
§ ADC and DAC perform transformation between analog physical
world and digital cyber world
§ Our micro-controller can communicate/control our
sensors/actuators using these pins (we have only seen a simple
example)
55