Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf ·...

40
Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016

Transcript of Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf ·...

Page 1: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

Introduction to

Arduino

By: Karim El-Rayes

By: Karim El-Rayes, 2016

Page 2: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 3: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 4: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 5: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 6: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

Microcontroller basic architecture

ROM

RAM

CPU

I/O

interfa

ce m

odule

I/O

pins

By: Karim El-Rayes, 2016

Page 7: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 8: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

How MCU look like??

By: Karim El-Rayes, 2016

Page 9: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 10: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 11: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 12: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 13: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 14: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 15: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 16: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 17: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 18: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 19: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 20: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 21: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 22: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 23: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 24: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 25: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 26: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 27: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 28: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 29: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 30: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 31: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 32: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 33: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 34: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 35: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

“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

Page 36: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 37: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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.

Page 38: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

Example

By: Karim El-Rayes, 2016

Page 39: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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

Page 40: Introduction to Arduino - files.meetup.comfiles.meetup.com/18605576/Intro to Arduino_v1.2.pdf · Introduction to Arduino By: Karim El-Rayes By: Karim El-Rayes, 2016 . Binary System:

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