Introduction to
Arduino
By: Karim El-Rayes
By: Karim El-Rayes, 2016
Binary System: about 0’s and 1’s
Computers don’t understand our human languages and voices,
they have their own language and protocols, for them anything is
represented by a number of 0’s and 1’s, what we call a “Binary
System”.
Simply a “1” means there is electricity flow from one end to
another, and “0” means no electricity flow.
By: Karim El-Rayes, 2016
Sources:
http://www.technologystudent.com/elec1/bulb1.htm
http://www.technologystudent.com/elec1/dig2.htm
What about computers !
We all know that we can use computers to do anything like
process control or data ciphering, but to have a computer we need
a CPU, a main memory unit represented in a RAM module, a
secondary storage element represented in Hard Disk Drive
(HDD), some input devices to give instructions/orders like
keyboard and a mouse, and some other output devices like a
monitor and printer to check the system state, and maybe some
communication devices to communicate with external world like
Ethernet card, USB ports and wireless interface cards (Wi-Fi).
By: Karim El-Rayes, 2016
Main
Memory
(RAM & cache)
I/O interface
module
CPU
Control unit
ALU Registers
Input
devices
Output
devices
Bus
Bus
BIOS
L1 Cache memory
Computers basic architecture
Keyboard Mouse
Monitor
RAM HDD
Motherboard
CPU
Wi-Fi card
By: Karim El-Rayes, 2016
Is it good enough?
Previously described system is very good and will be
very efficient, featuring a very high computing
capabilities BUT:
1. Big in size, you need a space for such system.
2. Very power hungry system, you can run on small
batteries or solar cell in a rural place.
3. Expensive, let’s try to calculate the prices of Intel
Core2Due processor, 1GB RAM slot, 80GB HDD, a
monitor, a keyboard and finally a mouse !!!
By: Karim El-Rayes, 2016
Microcontroller basic architecture
ROM
RAM
CPU
I/O
interfa
ce m
odule
I/O
pins
By: Karim El-Rayes, 2016
Function of….
ROM: To store the program or the code the developer
write for the μC to be used.
RAM: the program stored in the ROM is transferred
to the RAM when the μC is turned On.
CPU: the unit responsible processing the program
uploaded in the RAM transferred from the ROM.
I/O interface module: responsible for data transfer
between the outside world and the CPU in order to take
action either by input or output data.
By: Karim El-Rayes, 2016
How MCU look like??
By: Karim El-Rayes, 2016
CPU and Memory
CPU
Memory
Address
bus
Data
bus
Clock
With every clock tick
the CPU executes one
instruction, this
instruction can be a data
transfer to memory or
an arithmetic operation
or anything else.
By: Karim El-Rayes, 2016
Microcontroller I/O Modules
ROM
RAM
CPU
I/O p
ins
ADC
DAC
Timers
RS-232
Serial
interface
PWM
USB or
Ethernet
interface
I
/O p
ins
I/O in
terface
By: Karim El-Rayes, 2016
Digital Input / Output
Since our microcontroller is a digital system, our basic
inputs and outputs are 1’s and 0’s, between “3.5v” and
5v” for the logic “1” and “Ground” or “0v” for logic
“0”.
Microcontroller
Dig
ital
Inp
uts
Dig
ital
ou
tpu
ts
By: Karim El-Rayes, 2016
Analog to Digital converter
Analog to digital converter is an IC responsible of
converting analog signal continuous in amplitude and time
to digital signals (binary numbers), but under certain
conditions, for a PC or an ECU like a microcontroller or a
PLC can understand in order to be processed and stored by
these systems (the PC, ECU or PLC).
Analog to
Digital
Converter
1 1 1 1 0 0 0
V
t
Analog
signal Binary
code
By: Karim El-Rayes, 2016
Analog to Digital Conversion
Anti aliasing filter
(low pass filter)
Signal sampling
and hold circuit
Quantization
process
Binary output
buffers
Analog
Input
Binary
output
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
t
V
t
V
t
Analog signal Sampled signal
Quantizing sampled signal
By: Karim El-Rayes, 2016
010001
110011
Pulse Width Modulation (PWM)
This method is based
on that the motor
speed is affected on
the input pulse
durations, as duration
of the supplied
voltage increase the
speed increase and
vice versa.
By: Karim El-Rayes, 2016
PWM (cont.)
The speed of the motor using PWM system is determined
using a simple Methodology: which is the pulse duration percentage ratio to the whole duration is equivalent to the same ratio of the supplied voltage to the motor to the main voltage supply.
Example: if a motor is supplied with 10 volts and
the PWM circuit has an output pulse duration 10% from the whole pulse period then the output voltage is 10% the main supply voltage = 10% x 10 volts = 1 volt.
This mean that the motor is actually supplied with only 1 volt.
PWM is a relation between the average voltage that will be
supplied to the motor and the pulse duration.
By: Karim El-Rayes, 2016
Serial vs. parallel transmission
0
0
0
1
1
1 1 1 1 0 0 0 Parallel data lines
(multi-parallel lines for
data transmission, bits
are sent parallel in the
same time)
Serial data line (single data line
used in transmission bit by bit at a
time)
Parallel data
transmission Serial data transmission
By: Karim El-Rayes, 2016
Serial Module (UART)
By: Karim El-Rayes, 2016
TX (Transmit) Pin
RX (Receive) Pin
1 1 1 1 0 0 0
Serial communication uses a single
data line to transmit or receive data
serially bit after another.
1 1 1 1 0 0 0
UART
Module
Mic
roco
ntr
oll
er
0
Going Practical:
How we can program a microcontroller
You can program it using “C” or Assembly languages (C for
sure easier).
You first develop your application on PC then after compilation
of your application code we download it to the microcontroller
through special device called “Programmer”.
In-Circuit
Programmer
By: Karim El-Rayes, 2016
Arduino Uno
Reset switch
USB port
Atmel Atmega328P
microcontroller
General Purpose Inputs/Outputs
(Digital Inputs/Outputs or Analog Inputs)
Digital Inputs/Outputs
By: Karim El-Rayes, 2016
Ardunio Uno Pinout
Pin # Functionality
0 DIO/Serial Tx
1 DIO/Serial Rx
2 DIO
3 DIO
4 DIO
5 DIO
6 DIO
7 DIO
8 DIO
By: Karim El-Rayes, 2016
Pin # Functionality
A0 DIO/Analog input
A1 DIO/Analog input
A2 DIO/Analog input
A3 DIO/Analog input
A4 DIO/Analog input
A5 DIO/Analog input
5v 5v supply pin
3.3v 3.3v supply pin
GND Ground
DIO: Digital Input/Output, Tx: Transmit, Rx: Receive
Texas Instruments MSP430 LaunchPad
By: Karim El-Rayes, 2016
USB Connector
Reset switch Texas Instruments
MSP430G2553 microcontroller
An
alo
g a
nd
Dig
ital
Inp
uts
/Ou
tpu
ts A
nalo
g a
nd
Dig
ital In
pu
ts/Ou
tpu
ts
NodeMCU (Arduino Compatible)
Pin map:
D0 = 16
D1 = 5
D2 = 4
D3 = 0
D4 = 2
D5 = 14
D6 = 12
D7 = 13
D8 = 15
D9 = 3
D10 = 1
A0 = 17
By: Karim El-Rayes, 2016
ESP8266 CPU with Wi-Fi
Notes on Arduino Uno Board
• General Purpose Inputs/Outputs are the input & output
interface pins between the Arduino board and outside world.
• Any pin can be configured as either digital input or digital
output.
• The output of digital pins is either 1 (+5v) or 0 (0v).
• Some pins can be configured to be “Analog Inputs”, i.e. you
can input analog signals on these pins to the analog-to-digital
converter module.
• Analog signal: they can take any value between 0v and +5v,
the Arduino board converts this analog voltage value to a
number, such pins are commonly used with sensors.
By: Karim El-Rayes, 2016
Introduction to Arduino Syntax
Coding style.
Data types.
I/O: digital read/write, analog read/write.
Serial interface.
If-else.
For loops.
By: Karim El-Rayes, 2016
Arduino Coding Style
#include Header files (optional)
void setup() setup() function
{
//Setting up Arduino module to be used
}
Void loop() loop() function
{
//your application code
//This function will keep repeating forever
}
MyFunction() Other user defined functions (optional)
{
//Other functions
}
By: Karim El-Rayes, 2016
Basic Data Types
Data type Description Size (in bits)
Char Signed character 8
unsigned char Unsigned character 8
Int Signed integer 32
unsigned int Unsigned integer 32
float Floating number
(Decimal/real number)
single precision
32
bool Boolean number (binary,
i.e. either true or false)
8
By: Karim El-Rayes, 2016
Declaring a variable
char letter = ‘a’; char type variable.
int Num = 0; int type variable.
float MyFloatNum = 1.2; float type variable.
double MyFloatNum = 1.2; double type variable.
bool MyBoolVariable = false; bool type variable.
By: Karim El-Rayes, 2016
Arrays
Array is a list of variables of the same type, instead of
declaring every variable separately they are declared in the
form list or “Array”.
Example:
int Numbers[10]; Array of 10 integers.
char MyArray[20]; Array of 20 character
Note: a string in C is just an array of characters.
By: Karim El-Rayes, 2016
Digital Write
Syntax:
digitalWrite(pin, value);
pin: designated digital I/O pin.
value: HIGH (1) or LOW (0);
Example:
digitalWrite(5, HIGH); //Write “1” to pin 5
digitalWrite(3, LOW); //Write “0” to pin 3
By: Karim El-Rayes, 2016
Digital Read
Syntax:
digitalRead(pin);
pin: designated digital I/O pin.
Example:
bool PinValue;
PinValue = digitalRead(9);
//Read digital pin 9 and store the value in variable
//“PinValue”
By: Karim El-Rayes, 2016
Example
void setup()
{
//Configure pin first as either input or output
pinMode(13, OUTPUT); // sets the digital pin as output
}
void loop()
{
digitalWrite(13, HIGH); // sets pin 13 to 1
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets pin 13 to 0
delay(1000); // waits for a second
}
By: Karim El-Rayes, 2016
Analog Read
Syntax:
analogRead(pin);
Pin: designated analog input pin, analog pin names start
with “A” e.g. A3, A5…
Example:
int AnalogValue;
value = analogRead(A5);
//Read analog pin A5 and store the value in
//variable “AnalogValue”
By: Karim El-Rayes, 2016
Analog Write (PWM)
Syntax:
analogWrite(pin, value);
pin: PWM designated pin.
Value: PWM duty cycle value, has to be between 0-255.
Example:
analogWrite(7, 125);
By: Karim El-Rayes, 2016
Serial Output
void setup()
{
// Configure serial first: open the serial port at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// read the analog input on pin 0:
analogValue = analogRead(0);
// print it out in many formats:
Serial.println(analogValue); // print as an ASCII-encoded decimal
delay(1000); //delay for 1 second
}
By: Karim El-Rayes, 2016
“if – else” statement
“if-else” is a conditional statement to check if a condition is true or not:
if(i == 1)
{
//do something
}
else if(i == 2)
{
//do something else
}
else
{
//do something else if the previous conditions aren’t true
}
By: Karim El-Rayes, 2016
Loops: “for” Loop
To repeat some action(s) several times:
for (initial condition; stop condition; increment/decrement)
{ //start
//do something here
} //end
Example:
for(i = 0;i<10;i++)
{
//Your code here
}
“trying for loop” will be printed on screen 10 times
By: Karim El-Rayes, 2016
Breadboard
By: Karim El-Rayes, 2016
Breadboard is used for the purpose of prototyping
and testing electronic circuits.
Vertical line dots are all connected together
Every five
horizontal
points are
connected.
Example
By: Karim El-Rayes, 2016
Example 2: Light Sensor Circuit
By: Karim El-Rayes, 2016
Sources:
http://www.doctronics.co.uk/meter.htm#circuit 3
http://www.ikalogic.com/resistors-volt-and-current/
MSP430
LaunchPad Board
Ground
P1_0
To pin
P1_0
List of acronyms & abbreviations
ADC: Analogue to Digital Converter.
ALU: Arithmetic & Logical Unit.
BIOS: Basic Input/Output System
CPU: Central Processing Unit.
DAC: Digital to Analogue Converter.
GPM (μP): General Purpose Microprocessor.
I/O: Input/Output.
MCU (μC): Microcontroller unit.
PWM: Pulse Width Modulation.
RAM: Random Access Memory.
ROM: Read Only Memory.
Acronym of Serial Interfaces
UART: Universal Asynchronous
Receiver Transmitter.
SPI: Serial Peripheral Interface.
I2C: Inter-Integrated Circuit
USB: Universal Serial Bus.
By: Karim El-Rayes, 2016