0% found this document useful (0 votes)
51 views51 pages

Chapter 3 - Embedded

7844

Uploaded by

seyoum shimels
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views51 pages

Chapter 3 - Embedded

7844

Uploaded by

seyoum shimels
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Embedded/

microcontroller
programming (Based
on Arduino)
CHAPTER OBJECTIVES
Introducing arduino
Identifying the pins and their function
of arduino
Writing a code for arduino
CONTENT
Introduction to Arduino
 What is arduino
 Types of arduino

Arduino hardware
Arduino project examples
Arduino development environment
Arduino programming
 Program elements: data types, structures,
modifiers statements, loops and pointers
with examples
1. INTRODUCTION TO ARDUINO
 What is Arduino?
 Arduino is an open source programmable
circuit board that can be integrated into a
wide variety of projects both simple and
complex.
 This board contains a microcontroller which
is able to be programmed to sense and
control objects in the physical world.
 By responding to sensors and inputs, the
Arduino is able to interact with a large array of
outputs such as LEDs, motors and
displays.
 Because of it’s flexibility and low cost,
Arduino has become a very popular choice for
students/teachers and project developers.
INTRODUCTION TO ARDUINO
 What is Arduino? (cont.)
 Arduino was introduced back in 2005 in Italy
by Massimo Banzi as a way for non-
engineers to have access to a low cost,
simple tool for creating hardware projects.
 Since the board is open-source, it is released
under a Creative Commons license which
allows anyone to produce their own board.
 If you search the web, you will find there are
hundreds of Arduino compatible clones and
variations available but the only official boards
have Arduino in its name.
INTRODUCTION TO ARDUINO
 Types of Arduino Boards
 Arduino is a great platform for prototyping
projects and inventions but can be
confusing when having to choose the right
board.
 If you’re brand new to this, you might have
always thought that there was just one
“Arduino” board and that’s it.
 In reality, there are many variations of the
official Arduino boards and then there are
hundreds more from competitors who
offer clones.
INTRODUCTION TO ARDUINO
 Typesof Arduino Boards (cont.)
In the next slide are a few examples of
the different types of Arduino boards out
there.
The boards with the name Arduino on
them are the official boards but there are
also a lot of really great clones on the
market as well.
One of the best reasons to buy a clone
is the fact they are generally less
expensive than their official counterpart
but still have the same quality of the
originals.
INTRODUCTION TO ARDUINO
 Types of Arduino Boards (cont.)
A factor to consider when choosing a board is the
type of project you are looking to do.
 For example, if you want to create a wearable
electronic project, you might want to consider
the LilyPad board from Sparkfun.
 The LilyPad is designed to be easily sewn into
e-textiles and wearable projects.
 If your project has a small form factor, you
might want to use the Arduino Pro Mini which
has a very small footprint compared to other
boards.
INTRODUCTION TO ARDUINO
 Types of Arduino Boards (cont.)
2. ARDUINO HARDWARE
 Arduino Uno
 One of the most popular Arduino boards
out there is the Arduino Uno.
 While it was not actually the first board to be
released, it remains to be the most actively
used and most widely documented on
the market.
 Because of its extreme popularity, the
Arduino Uno has a ton of project tutorials
and forums around the web that can help
you get started or out of a jam.
 There are big fans of the Uno because of its
great features and ease of use.
ARDUINO HARDWARE
 Basic features of the hardware of Arduino
Uno
LED connected to
Port 13
ARDUINO HARDWARE
ARDUINO HARDWARE
 Here are the components that make up an
Arduino board and what each of their
functions are
1. Reset Button – This will restart any code that is
loaded to the Arduino board
2. AREF – Stands for “Analog Reference” and is used
to set an external reference voltage
3. Ground Pin – There are a few ground pins on the
Arduino and they all work the same
4. Digital Input/Output – Pins 0-13 can be used
for digital input or output
5. PWM – The pins marked with the (~) symbol can
simulate analog output
6. USB Connection – Used for powering up your
Arduino and uploading sketches
7. TX/RX – Transmit and receive data indication
ARDUINO HARDWARE
 Here are the components that make up an
Arduino board and what each of their
functions are: (cont.)
8. ATmega Microcontroller – This is the brains and
is where the programs are stored
9. Power LED Indicator – This LED lights up anytime
the board is plugged in a power source
10.Voltage Regulator – This controls the amount of
voltage going into the Arduino board
11.DC Power Barrel Jack – This is used for powering
your Arduino with a power supply
12.3.3V Pin – This pin supplies 3.3 volts of power to
your projects
13.5V Pin – This pin supplies 5 volts of power to your
projects
14.Ground Pins – There are a few ground pins on the
Arduino and they all work the same
ARDUINO HARDWARE
 Arduino Breadboard
 Another very important item when working
with Arduino is a solderless breadboard.
 This device allows you to prototype your
Arduino project without having to
permanently solder the circuit together.
 Using a breadboard allows you to create
temporary prototypes and experiment with
different circuit designs.
 Inside the holes (tie points) of the plastic
housing, are metal clips which are connected
to each other by strips of conductive material.
ARDUINO HARDWARE
 Arduino Breadboard
ARDUINO HARDWARE
 Jumper wires
 The breadboard is not powered on its own and
needs power bring to it from the Arduino board
using jumper wires.
 These wires are also used to form the circuit by
connecting resistors, switches and other components
together.
ARDUINO HARDWARE
 Components needed to construct Arduino based
projects
ARDUINO HARDWARE
 Arduino Sensors
 If you want your Arduino to sense the world around it, you
will need to add a sensor. There are a wide range of
sensors to choose from and they each have a specific
purpose. Below you will find some of the commonly used
sensors in projects.
 Distance Ranging Sensor

 PIR Motion Sensor

 Light Sensor

 Degree of Flex Sensor

 Pressure Sensor

 Proximity Sensor

 Acceleration Sensor

 Sound Detecting Sensor

 RGB and Gesture Sensor

 Humidity and Temperature Sensor


ARDUINO HARDWARE
 Tools required- the most common tools you will
need when working with Arduino projects
 Needle-nose
Pliers
 Wire Strippers
 Precision
Screwdriver
Set
 Flush Cutters
 Fine Tip
Straight
Tweezers
 Digital
Multimeter
 Soldering Iron
 Panavise Jr
ARDUINO HARDWARE
 Arduino Shields-
 If you want to add a very specific
functionality to your Arduino, you will need to
use a shield.
 Arduino shields plug into the top of the Arduino
board and can add capabilities such as
 WiFi Shield

 LCD Shield

 GPS Logger Shield

 MP3 Music Maker Shield

 Ethernet Shield

 Motor/Stepper/Servo Shield and much

more.
 There are literally hundreds of shields to choose
ARDUINO HARDWARE
 Arduino Shields- GPS Shield Plugged into
Arduino Uno
ARDUINO HARDWARE
 An arduino circuit example
 Here is a visual of what a completed Arduino circuit
looks like when connected to a breadboard.
3. ARDUINO PROJECTS (EXAMPLES)
 You may be wondering what an Arduino
board can do besides blink an LED.
 In the slides followed some example

projects which help to showcase how truly


amazing this board is and the capabilities
of it.
ARDUINO PROJECTS (EXAMPLES)
 Arduino Light Following Robot
ARDUINO PROJECTS (EXAMPLES)
 Arduino Drone
ARDUINO PROJECTS (EXAMPLES)
 LED Cube
ARDUINO PROJECTS (EXAMPLES)
 Control A Door-lock Using Arduino and Bluetooth
4. ARDUINO DEVELOPMENT ENVIRONMENT
 Software requirement to build an arduino
based project
ARDUINO DEVELOPMENT
ENVIRONMENT(CONT.)
 Arduino Software Editor Window
ARDUINO DEVELOPMENT
ENVIRONMENT (CONT.)
 Blinking LED Example
ARDUINO DEVELOPMENT
ENVIRONMENT (CONT.)
 Blinking LED Example

void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
ARDUINO DEVELOPMENT
ENVIRONMENT (CONT.….)
 Preference- used to set your preferences
ARDUINO DEVELOPMENT
ENVIRONMENT (CONT.….)
 Serial monitor -
used
 when debugging
Arduino programs
or
 to show data or

values from
program.
void setup()
{  To use it Arduino
should be connected
Serial.begin(9600);
} to PC
void loop()
{
Serial.println("Hello,
world!");
delay(1000);
}
3. ARDUINO PROGRAMMING
 All
Arduino programs must follow the following
main structure:
ARDUINO PROGRAMMING
(CONT.….)
 pinMode() and
digitalWrite() codes:
 pinMode()- Configures
the specified pin to
behave either as an
input or an output
 Syntax

 pinMode(pin, mode)
 digitalWrite ()- Write
a HIGH or a LOW value
to a digital pin.
 Syntax

 digitalWrite(pin,

value)
ARDUINO PROGRAMMING (CONT.….)
 Program with comment
ARDUINO PROGRAMMING (CONT.….)
 Program with comment (cont.)
ARDUINO PROGRAMMING (CONT.….)
 Using serial monitor
 Open the ”Serial Monitor” in order to see the

output

void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Hello, world!");
delay(1000);
}
ARDUINO PROGRAMMING (CONT.….)
 Using serial monitor
 Serial.begin(9600)
 Starts serial communication, so that the Arduino can

send out commands through the USB connection.


 The value 9600 is called the 'baud rate' of the

connection.
 This is how fast the data is to be sent. You can

change this to a higher value, but you will also


have to change the Arduio Serial monitor to the
same value.
 Serial.print()

 Prints data to the serial port as human-readable

ASCII text.
 This command can take many forms.

 Syntax

 Serial.print(val)

 Serial.print(val, format)
ARDUINO PROGRAMMING (CONT.….)
 Using serial monitor
 Serial.println()
 Prints data to the serial port as human-readable

ASCII text followed by a carriage return character


(ASCII 13, or '\r') and a newline character (ASCII 10,
or '\n').
 This command takes the same forms as Serial.print().

 Syntax

• Serial.println(val)
 val- the value to print. Allowed data types: any data
type.
• Serial.println(val, format)
 format: specifies the number base (for integral data
types) or number of decimal places (for floating point
types).
ARDUINO PROGRAMMING (CONT.….)
 Using serial monitor
int z;int a;int b;
void setup()
{
Serial.begin(9600);
}
void loop()
{
a = random(100);
b = random(100);
z = calculate(a,b); //Adding 2 Numbers
//Write Values to Serial Monitor
Serial.print(a);
Serial.print(" + ");
Serial.print(b);
Serial.print(" = ");
Serial.println(z);
delay(1000);
}
float calculate(int x, int y)
{
return (x + y);
}
ARDUINO PROGRAMMING (CONT.….)
 Creating and using functions
int z;int a;int b;
void setup()
{
Serial.begin(9600);

}
void loop()
{
a = 2;
b = 3;
z = calculate(a,b); //using a function
Serial.println(z);
}
float calculate(int x, int y) //creating a
function
{
return (x + y);
}
ARDUINO PROGRAMMING (CONT.….)
 Example: creating and using functions
 Create a function that calculates the area of a

circle with a given radius.


 Write the result to the Serial Monitor.
ARDUINO PROGRAMMING (CONT.….)
 Example: solution
void setup()
{
float area;
Serial.begin(9600);// calculate the area of a circle with radius of 9.2
float r=9.2;
area = CircleArea(r);
Serial.print("Area of circle is: "); // print area to 4 decimal places
Serial.println(area, 4);
}
void loop()
{
}
float CircleArea(float radius) // calculate the area of a circle
{
float result;
const float pi = 3.14;
result = pi * radius * radius;
return result;
}
ARDUINO PROGRAMMING (CONT.….)
 For-Loop
 provides a mechanism to repeat a section of code
depending on the value of a variable
 It can be used in the standard Arduino setup(), and

loop() functions, and within your own functions.


 in using for-loop

 set the initial value of the variable,


 the condition to exit the loop (testing the variable), and

 the action on the variable each time around the loop.

 These three elements are parameters to the for


loop and are separated by semicolons:
for ( <initialiser code> ; <condition test expression> ;
<iterator expression> )
{
<body code>
}
ARDUINO PROGRAMMING (CONT.….)
 Example: For-Loop
 In this program we use a for-Loop to find the

Sum of 100 Random Numbers.


 Then we find the Average.

 The Sum and Average should be written to

the Serial Monitor.


ARDUINO PROGRAMMING (CONT.….)
 Example: Solution
int x; int sum = 0; float gjennomsnitt = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
sum = 0;
for (int i = 0; i<100; i++)
{
x = random(100);
sum = sum + x;
}
average = sum / 100;
Serial.print(" Sum = ");
Serial.print(sum);
Serial.print(" ,
Average = ");
Serial.println(average);
delay(1000);
}
ARDUINO PROGRAMMING (CONT.….)
 Array
 An array is a
collection of
variables that are
accessed with an
index number.
 It is a consecutive

group of memory
locations that are
of the same type.
 To refer to a
particular location
or element in the
array, we specify
the name of the
array and the
position number of
the particular
element in the
ARDUINO PROGRAMMING (CONT.….)
 Example: Here we shall use arrays in the
Arduino program. open the Serial Monitor to
see the result
sum =
const int arraysize = 100;
calculateSum(myarray);
int x;
average = sum / 100;
int sum = 0;
Serial.print(" Sum = ");
float average = 0;
Serial.print(sum);
int myarray[arraysize];
Serial.print(" , Average =
void setup()
");
{
Serial.println(average);
Serial.begin(9600);
delay(1000);
}
}
void loop()
int calculateSum (int
{
sumarray[])
sum = 0;
{
for (int i = 0; i <
for (int i = 0; i <
arraysize; i++)
arraysize; i++)
{
{
x = random(200);
sum = sum + sumarray[i];
myarray[i] = x;
}
}
return sum;
}

You might also like