Intro Embedded Sys
Intro Embedded Sys
1
Introduction to Microcontrollers
MCUs vs CPUs
3
Features of Embedded Systems
4
Evolution of Microcontrollers in Embedded
Systems
1. Early Beginnings
•Embedded systems originated in the mid-20th century for real-time control in
military and industrial applications.
•Initial implementations involved discrete logic components and dedicated
hardware.
5. Arduino Mega
6. STM32
4. Arduino Nano
9
Applications of MCUs
• Automotive Control Systems
• Internet of Things (IoT) Devices
• Industrial Automation
• Medical Devices
• Consumer Electronics
10
11
SECTION 2: MCUs vs
CPUs
• Microcontrollers (MCUs) and Central Processing
Units (CPUs) are both types of processors, but they
serve different purposes and are designed for
different applications.
• CPUs are the primary components of a computer
responsible for executing instructions and
performing calculations. They act as the brain of a
system, carrying out tasks ranging from basic
arithmetic operations to running complex software
applications.
12
Feature Comparison between MCUs and CPUs
Feature Microcontrollers (MCUs) CPUs
Integration Integrated circuits combining CPU, Typically consist of a standalone
memory, and peripherals on a single CPU requiring external
chip. components for memory and
peripherals.
Specialised Designed for specific tasks, often General-purpose processors
Functions operating in real-time capable of running a wide range
environments. of applications.
External Integrated peripherals like timers, May require external components (e.g.,
Peripherals communication interfaces, and ADC on separate memory modules, peripheral
the same chip. controllers) for expanded functionality.
14
Why MCUs are preferred in Embedded
Developments
Low Power Consumption: Many MCUs are optimized for low power
consumption, making them ideal for battery-powered and energy-
efficient embedded devices, including IoT applications.
15
Why MCUs are preferred in Embedded
Developments
17
Why C++ ???
Organization and Reusability: C++ supports a way of
organizing code that's like building with LEGO bricks, making it
easier to manage and reuse pieces of code in embedded
systems.
Efficiency: C++ allows developers to fine-tune how the
computer's memory is used, which is important for embedded
systems that often have limited resources. This helps make the
system work faster and use less power.
Speed: C++ is known for its speed. It allows developers to write
code that runs quickly, making it suitable for applications where
fast responses are crucial, such as in control systems or real-
time operations.
18
Why C++ ???
Size Optimization: C++ gives developers control over the size
of the code they write. In embedded systems, where storage
space may be limited, this ability to optimize code size is
valuable.
Ready-Made Tools: There are many tools available for C++
that make the job of programming embedded systems easier.
These tools help catch mistakes and speed up the process of
writing code.
Sharing and Compatibility: C++ code can often be used on
different types of embedded systems without much change.
This is handy because different systems might need the same
code to do similar tasks.
19
Why C++ ???
Community Help: Many people use C++, so there's a big
community that shares tips, advice, and ready-made pieces of
code. This helps programmers learn and solve problems faster.
Safety Features: C++ has some built-in safety features that
help catch mistakes before they cause problems. This is
important in situations where errors could be serious, like in a
car or a medical device.
Widespread Use: C++ has been used successfully in many
types of embedded systems, like in cars, gadgets, and industrial
machines. This means there's a lot of experience and knowledge
about how to use C++ effectively in these kinds of projects.
20
SECTION 4: INTRODUCTION TO
ARDUINO IDE AND
SIMULATIONS
We’ll discuss
Installation of the Arduino IDE
Setup for ESP32
Features and Shortcuts
Simulating Embedded system development using
TinkerCad 21
ARDUINO IDE
22
INSTALLATION AND SETUP
The installation files will be shared in the class group, alternatively it can be downloaded at
https://www.arduino.cc/en/software#future-version-of-the-arduino-ide
The Arduino MCU board will be used throughout this course which is automatically installed
Now you have the IDE ready for programming!
In the board selector Type “Arduino UNO” and select it to be your default board
23
ARDUINO IDE SETUP
24
SIMULATION WITH TINKERCAD
Visit https://www.tinkercad.com/joinclass/K3DCYQ9EB
Login using your student ID name as your Nickname
You can optionally sign up with a personal account for your personal practices and projects
25
SECTION 5: BASIC ARDUINO
COMMANDS / FUNCTIONS
A Brief Evolution of Arduino
26
A Brief Evolution of Arduino
• Open Source Community (2005 onward): Arduino fosters a collaborative
community, with users contributing libraries, tutorials, and projects, creating a
vibrant ecosystem.
• Board Evolution: Arduino undergoes continuous development, releasing models
like Arduino Uno, Mega, and Nano, each offering improved features and
capabilities.
• Widespread Adoption: Arduino gains popularity among hobbyists, makers, and
professionals, finding applications in interactive art, home automation, and
robotics.
• Diverse Impact (2010s): Arduino's versatility leads to adoption in industrial
automation and IoT, making it a standard tool in education and showcasing its
relevance in various technological fields. 27
Arduino Terminologies
• Serial Monitor: A tool within the Arduino IDE that allows
communication between the Arduino board and the computer
via serial communication. It is commonly used for debugging and
real-time data monitoring.
• Library: Pre-written code modules that provide ready-to-use
functions for specific tasks. E.g. The <iostream>, <fstream> …
• Board: Refers to the specific model of Microcontroller being
used (e.g., Arduino Uno, Arduino Nano, ESP32). The choice of
board determines the hardware features and capabilities
available.
• Port: The communication port through which the Arduino board
connects to the computer. Users select the appropriate port in
the Arduino IDE before uploading code.
28
Arduino Terminologies
• Digital Pin: Input or output pins on an Arduino board that can be used
to read digital signals (HIGH or LOW) or send digital signals to
connected components.
• Analog Pin: Input pins that can read analog signals, providing a range
of values. Useful for interfacing with analog sensors or reading analog
voltages.
• PWM (Pulse Width Modulation): A technique used to simulate analog
output using digital pins. It allows users to control the intensity of an
output signal, like the brightness of an LED.
• Upload: The process of transferring the compiled Arduino sketch from
the computer to the Arduino microcontroller.
• Verify: Verifying the code ensures that there are no syntax errors in
the sketch. The Verify process checks the code for correctness and
highlights any errors or warnings without actually uploading the code
to the Arduino board. 29
The Arduino Program
30
The Setup Function
Purpose: The setup function is used for one-time setup tasks. It is executed
once when the Arduino board is powered on or reset.
Syntax:
void setup() {
// Initialization code goes here
}
Common Usage:
• Configuring pin modes (e.g., setting a pin as an input or output).
• Initializing serial communication.
• Setting up initial conditions for variables.
• Configuring external libraries or devices.
31
The Loop Function
Purpose: The loop function contains the main program logic and is executed
continuously in a loop after the setup function completes. It forms the core of
the Arduino sketch.
Syntax:
void loop() {
// Main program logic goes here
}
Common Usage:
• Reading sensor data.
• Controlling actuators.
• Responding to external events or inputs.
• Implementing iterative tasks. 32
pinMode(pin, mode)
digitalWrite(pin, value)
digitalRead(pin)
Functions analogWrite(pin, value)
You should analogRead(pin)
know delay(ms)
Serial.begin(baudRate)
Serial.print(value)
33
ARDUINO BASIC FUNCTIONS
1. pinMode(pin, mode):
Purpose: Configures a specified pin to behave as either an INPUT or OUTPUT.
Example: pinMode(13, OUTPUT); sets pin 13 as an output. And if mode =
INPUT, pin 13 becomes an input component
2. digitalWrite(pin, value):
Purpose: Writes a HIGH or LOW value to a digital pin.
Example: digitalWrite(13, HIGH); sets pin 13 to HIGH voltage(on). And to set
an off the value should be LOW
3. digitalRead(pin):
Purpose: Reads the digital value (HIGH or LOW) from a specified digital pin.
Example: int buttonState = digitalRead(2); reads the state of a button
connected to pin 2. 34
ARDUINO BASIC FUNCTIONS
4. analogWrite(pin, value):
Purpose: Writes an analog value (PWM) to a PWM-capable pin (usually
denoted by a ~ symbol).
Example: analogWrite(9, 128); sets a PWM value of 128 on pin 9.
5. analogRead(pin):
Purpose: Reads an analog value (0 to 1023) from an analog pin.
Example: float sensorValue = analogRead(A0); reads the analog value from
pin A0.
6. delay(ms):
Purpose: Pauses the execution of the program for the specified number of
milliseconds.
Example: delay(1000); pauses the program for one second. Since 1s = 1000ms 35
ARDUINO BASIC FUNCTIONS
7. Serial.begin(baudRate):
Purpose: Initializes serial communication with the specified baud rate.
Example: Serial.begin(9600); initializes serial communication with a baud rate
of 9600. This serves as a pathway for information exchange.
8. Serial.print(value), Serial.println(value):
Purpose: Outputs data to the serial monitor, works the same as cout in C++
Example: Serial.print("Hello "); Serial.println("Arduino!"); prints a message to
the serial monitor.
36
SECTION 6: THE UNO BOARD
AND WORKING WITH LEDs
37
FEATURES OF THE UNO BOARD
• Memory:
32 KB Flash memory.
2 KB SRAM.
1 KB EEPROM.
• Voltage Regulator:
Onboard voltage regulator for 7-12V DC input or USB (5V) power.
38
THE UNO PINOUT
39
THE UNO PINOUT
1. Digital Pins (D0-D13): These pins can be used for both input and
output. They support digital signals, meaning they can be either HIGH
(5V) or LOW (0V).
2. Analog Pins (A0-A5): These pins can read analog signals, providing
a range of values between 0 and 1023. They are often used for
reading sensor values or other analog inputs.
3. Power Pins:
1. 5V: This pin provides a regulated 5V output, typically used to
power external components.
2. 3.3V: A 3.3V regulated output for lower voltage components.
3. GND (Ground): Ground pins are used as a reference point for
electrical potential. 40
THE UNO PINOUT
PWM Pins (D3, D5, D6, D9, D10, D11): These pins support Pulse
Width Modulation, allowing for analog-like control of devices such
as LEDs or motor speed.
Serial Communication Pins (RX, TX): These pins are used for serial
communication (UART) and are crucial for programming the
Arduino and interfacing with other devices.
External Interrupt Pins (D2, D3): These pins can be used to trigger
interrupts based on external events, allowing the microcontroller
to respond quickly to specific signals.
41
Your First Arduino Program
1. void setup() {
2. Serial.begin(9600);
3.
4. Serial.print("Hello Arduino World!!!\n");
5. } //Anything here runs just once
6.
7.
8. void loop() {
9.
10. Serial.print("Hello to everyone, many times");
11.
12. } // This function runs multiple times
42
What it all means
Line 1 : Starts the program with the setup function, which is mainly used for
initializations
Line 8 : Begins the loop function which will iterate till the mcu is disconnected
or turned off
43
Now let’s work with LEDs
• In this section we’ll be using our TinkerCad circuit simulation so join the classroom page an let’s get it started
• Use this link https://www.tinkercad.com/dashboard?type=circuits&collection=designs
• And choose Create > Circuit
• We will try out a basic breadboard connection with our simulator
• Now choose a breadboard from the right panel by dragging it to the workspace
45
First blinking program
46
Traffic Lights Control Program
Now let’s apply the led blinking concepts to control 3 LEDs; Red, yellow and
green in the form of traffic lights
Steps:
1. Turn the red Led on
2. Wait for some seconds and turn it off
3. Turn the yellow Led on
4. Wait for some seconds and turn it off
5. Turn the green Led on
6. Wait for some seconds and turn it off
7. Repeat step 1
47
Traffic Lights Control Program (Cont’d)
• Each led has it own variable to hold a pin number, which is then
initialized as output using the pinMode function.
• This program simulates a simple traffic light sequence where the red
light is on for 5 seconds, followed by the yellow light for 2 seconds, and
then the green light for 5 seconds.
• Modify the program in order to get an alert in the serial monitor
whenever a led goes on or off
• The blinking script can be placed in a function to make the code much
cleaner
48
A Basic Binary Counter Program
50
SECTION 7: ANALOG AND
DIGITAL SIGNALS AND THE
SERIAL PORT
• Digital signals are binary, meaning they can only have
two states: HIGH (1) or LOW (0). In Arduino, digital pins
are primarily used for digital signals.
• Analog signals represent a continuous range of values. In
Arduino, analog signals are typically read from sensors
like potentiometers, temperature sensors, or light
sensors.
• Analog-to-Digital Conversion (ADC): Arduino boards have
built-in Analog-to-Digital Converters (ADC) that convert
analog signals to discrete digital values ranging from 0 to
1023. The analogRead function is used to read analog
signals from sensors connected to analog pins.
51
Measuring Voltage and Current
52
Measuring Voltage and Current
53
What it all means
The program uses an analog pin to measure the voltage of the component
its connected to using the analogRead().
The map function on line 18. This function is used to scale the raw analog
value (which ranges from 0 to 1023) to the desired voltage range (0 to 5.0).
At a threshold the led will turn to show that there’s voltage in the pin.
The Pin can be reinserted anywhere to get the voltage at that point. A
voltmeter is used to verification.
54
Measuring Voltage and Current
Here is an example code measuring the current (I)
55
Pulse Width Modulation (PWM)
• PWM stands for Pulse Width Modulation. It's a technique used in electronics to generate
analog-like signals with digital devices, such as microcontrollers like the Arduino.
• PWM is widely used for tasks like controlling the speed of motors, adjusting the brightness
of LEDs, and creating sound in electronic projects.
• Only pins with the ‘~’ sign can use this property i.e. pins {3, 5, 6, 9, 10 and 11}
• Commonly used Arduino functions for PWM include analogWrite().
• PWM involves rapidly turning a signal (typically a square wave) on and off at varying
intervals.
• The average voltage over a given period corresponds to the desired analog value.
• As analog signals is from (0-1023) , the signal range for pwm is (0-255)
56
Pulse Width Modulation (PWM) Cont’d
Here is an example code of using pwm to change the brightness of an Led.
Make sure your pin is connected to a pwm enabled pin!!
Observe what happens in the Serial monitor
57
Serial Monitor and Plotter
• The Serial Monitor is a crucial tool in Arduino programming, providing a simple interface to
communicate with and monitor the Arduino board.
• It allows the Arduino to send text or numerical data back to the computer, facilitating debugging
and interaction with the code in real-time.
• Programmers use the Serial.begin() function to initialize serial communication and utilize
commands like Serial.print() and Serial.println() to send information to the Serial Monitor.
• This tool aids in understanding program behavior, verifying sensor readings, and debugging
code during development.
• The baud rate of the device needs to be the same as the one in the activation function
Serial.begin(baudRate) before data transfer can be observed in Arduino Uno it is 9600.
58
Reading values from the Serial Monitor
In this section we will learn how to send values into our Serial Monitor. It is a similar
operation as using cin or getline in C++
To concatenate a value with a string in the print/println function place it in the String()
function eg. Serial.println(“Your age is: “ + String(age));
59
Reading values from the Serial Monitor
An example of taking an int value from serial monitor input
60
Serial Plotter
• The Serial Plotter is an extension of the Serial Monitor, specifically designed for
visualizing numerical data as graphs. It enables real-time plotting of data sent from
the Arduino, helping to analyze trends, patterns, or changes over time.
• Programmers can use the Serial.print() command to send numerical values, and the
Serial Plotter plots these values graphically.
61
Serial Plotter
Visualizing a plot of sine and cosine
62
Exercise
Design a program that takes brightness values from a user (In percentages) to control
the brightness of an LED.
Hint:
Map percentage values to pwm ranged values
63
SECTION 8: WORKING WITH
OUTPUT COMPONENTS
• RGB LEDs:
RGB LEDs go beyond simple illumination; they are miniature canvases of
color. With individual red, green, and blue diodes, they create a spectrum
of hues. Perfect for mood lighting or eye-catching displays, RGB LEDs turn
code into a visual masterpiece.
• Active Buzzers:
Active buzzers are the audible storytellers. They emit a continuous tone
when powered, making them ideal for alarms, alerts, or interactive sound
effects. Easy to integrate into Arduino projects.
• Passive Buzzers:
Passive buzzers bring melody to the mix. Requiring an external oscillating
signal, they act as musical transducers. Perfect for generating specific
tones or tunes, passive buzzers are the choice for projects with a melodic
touch. 64
Working with RGB LEDs
• You can produce almost any color by combining those three colors.
• With an RGB LED you can, of course, produce red, green, and blue light,
and by configuring the intensity of each LED, you can produce other
colors as well.
• To produce other colors, you can combine the three colors in different
intensities. To adjust the intensity of each LED you can use a PWM signal.
• To have an idea on how to combine the colors, take a look at the following
chart. This is the simplest color mixing chart, but gives you an idea how it
works and how to produce different colors.
65
Working with RGB LEDs
66
Working with RGB LEDs
Now, let’s look at program where we take user
colours and output it with the RGB.
67
Active Buzzers
Active buzzers typically operate within a specific voltage range, and it's crucial to
adhere to these requirements for optimal performance. Understanding the
buzzer's voltage specifications ensures proper functioning in diverse electronic
setups.
While active buzzers are known for continuous tone generation, their frequency
range might vary. Some models may offer a wider range of frequencies,
providing flexibility in creating diverse sound effects or musical notes in projects.
By sending a digital HIGH signal to the buzzer, you can activate the sound
generation.
69
Active Buzzers
A program to play Jingle bells using just the buzzer.
70
Active vs Passive Buzzers
72
SECTION 8: WORKING WITH
INPUT COMPONENTS
• Pushbuttons:
A pushbutton is a simple, momentary switch that completes or interrupts an
electrical circuit when pressed. When the button is pressed, it closes the circuit,
allowing current to flow, and when released, the circuit opens.
• Potentiometer:
A potentiometer, or pot, is a variable resistor with three terminals, allowing it to
function as an adjustable voltage divider. By turning its shaft, the resistance
between the middle terminal and either of the outer terminals changes, providing a
variable voltage output.
• Photoresistor:
A photoresistor, or light-dependent resistor (LDR), is a component whose resistance
changes in response to the intensity of incident light. As illumination increases, the
resistance decreases, and vice versa. Photoresistors find applications in light-sensing
circuits, where their varying resistance can be utilized to control the behavior
73 of
other components.
Pushbuttons
Pushbuttons, commonly referred to as momentary switches, represent
foundational electronic components integral to user input mechanisms.
These devices facilitate interaction by momentarily altering the electrical
state of a circuit, initiating specific actions contingent upon the transition
between their pressed and released states.
75
Switching an Led with a Pushbutton
In the previous example we can only turn it on whilst we still press it. How about we make about program
remember the state of the button.
76
Led brightness control with a Pushbutton
77
Led brightness control with a Pushbutton CONT’D
80
Changing colours with a Potentiometers
81
Changing colours with a Potentiometers (Cont’d)
82
Photoresistors
Resistance Variation: Photoresistors are made of semiconductor materials that
experience changes in conductivity when exposed to light. This results in a
variable resistance that is inversely proportional to the light intensity.
Applications: Widely used for ambient light sensing, automatic lighting controls,
camera exposure adjustments, and in various electronic circuits requiring light-
dependent responses. 83
Photoresistors Example
This example uses the photoresistor as the main input device together with leds and a buzzer
84
Photoresistors Example (Cont’d)
85
Reminder
You are to make sure all the simulations have been designed/prototyped by
everyone and the expected results have been achieved.
Projects will be assigned to the various groups and every member’s participation
will be required
THANK YOU.
86