chapter 2 Microcontroller based embedded system
chapter 2 Microcontroller based embedded system
Chapter -2
Design Microcontrollers based
Embedded system
BEWNET GETACHEW ,
MS.C.
2.1 Introduction
An embedded system is a computer system with a specific, dedicated function that is not
designed so that it should ever need to be reprogrammed (i.e. engine control units,
implantable medical devices, appliances, etc.) The most common type of embedded
system is a microcontroller, which is a small computer system on a single integrated
circuit. Some common examples of this type of embedded system comes in the form of
Arduino or Teensy microcontrollers (Figure 2.1)
Figure 2.1. (left) Arduino Uno Microcontroller (right) Teensy 3.2 Microcontroller
There are countless factors that go into selecting which microcontroller is the most suited
for any specific project. Different microcontrollers have different sizes, different
functionalities, different feature to footprint ratios, different software architectures, varying
number input/output (I/O) pins available for use, different power requirements, different
processing speeds, etc.
The software package Arduino offers an IDE that allows for the user to write C programs
for a microcontroller the same environment can be used for development with the Teensy
3.2 microcontroller. Programs developed in this IDE are known as sketches. Several
example sketches are provided with Arduino that show off the functionality of the
program.
Embedded programs (sketches) have two primary loops: setup() and loop(). It should be
noted that these have types of void, meaning they do not return any values. The setup()
loop runs exactly once upon startup of the board, and the loop() loop then runs in
perpetuity in a loop after completion of the setup() loop.
Variables that are global in scope are declared before the setup() loop (remember,
variables are only accessible within their specific scope, and so a variable declared
before all loops are run can be accessed anywhere in the sketch). The setup() loop is
typically used to declare what the microcontroller pins will do and initialize other aspects
of the program.
A pin is a specific metal interface that allows the electronics on the microcontroller to
interact with the surrounding world.
There are a number of different types of pins, which are summarized below:
1) Digital Pins – pins that can read or write signals that are HIGH or LOW only
2) Analog Pins – pins that can read analog signals to within the resolution of the ADC
3) PWM Pins – pins that can write analog voltages to within the resolution of the DAC.
4) Touch Pins – pins that can sense capacitance
5) Serial Ports – pins that communicate using Serial Protocol (typically UART)
6) I2C Ports – pins that communicate with sensors using the I2C Protocol 7)
7) SPI Ports – pins that communicate with sensors using the SPI Protocol
To actually power a microcontroller, an external power supply can be used (as seen above,
for the Teensy 3.2 this power would be provided to Vin and would range from 3.6 V to 6 V),
or the microcontroller can be powered from the 5 V rail from USB and hooked into a
computer
The simplest program to run on a microcontroller is one that blinks the built in LED (Light
Emitting Diode). As seen on the Teensy 3.2 pinout sheet, the built in LED is located on
digital pin 13. A program that would blink this LED is provided below:
There are a number of functions that appear in this simple code that have not
been addressed. These functions are all part of the Arduino.h library, which is
a library that does not require a compiler directive to include, as it is built into
all sketches made in the Arduino IDE. These functions are outlined below, and
should be remembered as they are used in basically all sketches:
pinMode(pin, mode)
Purpose: Configures the specified pin to behave either as an input or an output. See the
description of digital pins for details on the functionality of the pins
Parameters: pin: the number of the pin whose mode you wish to set mode: INPUT,
OUTPUT, or INPUT_PULLUP
digitalWrite(pin, value)
Purpose: Write a HIGH or a LOW value to a digital pin. If the pin has been configured as an
OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V
on 3.3V boards such as the Teensy 3.2) for HIGH, 0V (ground) for LOW. If the pin is
configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal
pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to
enable the internal pull-up resistor. See the later sections on pullup resistors for more
information.
Parameters: pin: the number of the pin whose mode you wish to set value: HIGH, LOW
digitalRead(pin, value)
Purpose: Reads the value from a specified digital pin, either HIGH or LOW, and returns
this value.
Parameters: pin: the number of the pin whose mode you wish to set value: HIGH, LOW
delay(time)
Purpose: Pauses the program for the amount of time (in milliseconds) specified as
parameter. (There are 1000 milliseconds in a second.) Parameters: time: the number of
milliseconds to pause (unsigned long)
A couple of important notes to make here:
• When reading a digital signal, microcontrollers will consider voltages higher than a
certain threshold HIGH and lower than a certain threshold LOW. Voltages in between
these thresholds will return inconsistent results. There is more on the idea of voltage
later in this document.
• Using delay() with a negative time often crashes the program if it even compiles at all.
Another simple example that uses these functions is one that will set pin 13 (the LED pin)
to the value of whatever pin 7 is (either HIGH or LOW), as shown:
Analog to Digital Conversion (ADC)
An Analog to Digital Converter (ADC) is a very useful feature that converts an analog
voltage on a pin to a digital number. By converting from the analog world to the digital
world, we can begin to use electronics to interface to the analog world around us.
The internal ADC can be used on a microcontroller using the built in Arduino function
analogRead(), described below:
analogRead(pin)
Purpose: Reads the value from the specified analog pin. The Arduino board contains a 6
channel (8 channels on the Mini and Nano, 16 on the Mega), 10-bit analog to digital
converter. This means that it will map input voltages between 0 and 5 V (3.3 V for a Teensy
3.2) into integer values between 0 and 1023. This yields a resolution between readings of:
5 volts / 1024 units or, .0049 volts (4.9 mV) per unit (3.2 mV on the Teensy 3.2). The input
range and resolution can be changed using analogReference().
Parameters: pin: the number of the pin whose mode you wish to read an analog voltage
on (must be an analog pin
An example of using analogRead() would be to read an analog voltage of a device such
as a potentiometer. A potentiometer is any device that can change resistance based on user
input (such as turning of a screw). They typically have three terminals, and the mechanism
in which they work is best illustrated below (Figure 2.2):
analogWrite(pin, value)
An example of using both analogWrite() and analogRead() is presented below in
code that will light up an LED attached to pin 9 with a PWM signal based on the reading a
potentiometer attached to pin 9.
Serial Communication
Serial communication utilizes digital data (HIGH, LOW) and timing to transmit data,
compared to analog sensors that change an analog voltage based on their output. Some
of the more common examples of serial communication to be explored are:
• UART (Universal Asynchronous Receiving and Transmitting)
o Examples: TTL, RS232, RS422
o Universal in potential usages, but slow (approximately 10 Mhz – 35 MHz)
• SPI (Serial Peripheral Interface)
o Very fast (approximately 500 MHz)
o Requires a unique “chip select” wire for each chip
o Functions by allowing a “master” (microcontroller) to handle “slave’ ICs
(integrated circuits).
• I 2C (Inter – Integrated Circuit)
o Fast (slower than SPI, but faster than UART)
o Only requires two wires for up to approximately 63 slave devices.
• CAN (Controller Aided Network)
o Works like I 2C but gives some sensors “priority” over others
• One Wire
o Works similarly to I 2C but data and clock information are combined onto one wire
There are several important functions to discuss when referring to Serial communication,
which are outlined here:
Purpose: Sets the data rate in bits per second (BAUD) for serial data transmission on the
Serial port specified (Serial, Serial1, Serial2, or Serial3). For communicating with the
computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800,
38400, 57600, or 115200. You can, however, specify other rates for interfacing with other
devices. An optional second argument configures the data, parity, and stop bits. The default
is 8 data bits, no parity, one stop bit.
Serial.end()
Purpose: Disables serial communication on the Serial port specified, allowing the RX and
TX pins to be used for general input and output. To re-enable serial communication, call
Serial.begin().
Serial.print(val, format)
Serial.println() behaves the same as Serial.print() except that the printed value
will be printed preceded by a new line character (so that the printed value is on a new
line).
Serial.available()
Purpose: Returns the number of bytes (characters) available for reading from the serial
port specified. This is data that's already arrived and stored in the serial receive buffer
(which holds 64 bytes). Parameters: none
Serial.read()
Purpose: Reads incoming serial data on the specified serial port and returns the first byte
of incoming serial data available (or -1 if no data is available) as an integer. Parameters:
none
Serial.write(val)
Purpose: Writes binary data to the serial port. This data is sent as a byte or series of
bytes; to send the characters representing the digits of a number use the print() function
instead. Parameters: val: a value to send as a single byte or a string to send as a series of
bytes
To actually interface with the Serial monitor (Serial communication from microcontroller to
computer through USB), first specify the COM port in which the USB is connected into the
computer from the Arduino IDE (Tools -> Port -> COMxx)
The Serial Monitor can then be brought up from the Tools menu or the keyboard shortcut
Ctrl + Shift + M. The Serial monitor will appear as the following window that will display text
as its printed:
A very simple example for interfacing with data received from Serial
communication is given below:
Another simple example showing off writing of Serial data is below:
ARDUINO CODE :
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
delay(500);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
delay(500);
}
EXAMPLE 2
ARDUINO PROGRAMMING CODE :
BLINK LED USING SWITCH WITH
ARDUINO int buttonState = 0;
We read in previous topics how to make blinking void setup()
LEDs and control output using input (by switch). {
In this topic we will combine both the logic and pinMode(2, INPUT);
make the led blinking on button pressed / Blink pinMode(13, OUTPUT);}
Led Using Switch with Arduino.
void loop()
CIRCUIT DIAGRAM {
buttonState = digitalRead(2);
if (buttonState == HIGH)
{
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
} else if (buttonState ==
LOW)
{
digitalWrite(13, LOW);
}
}