0% found this document useful (0 votes)
18 views57 pages

تصميم رقمي

Uploaded by

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

تصميم رقمي

Uploaded by

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

Assistant lecturer : Abrar ghaleb

Introduction to arduino
Arduino is an electronic development board consisting of a computer-programmed microcontroller
electronic circuit, designed to facilitate the use of interactive electronics in multidisciplinary projects.
Arduino is mainly used in the design of interactive electronic projects or projects that aim to build different
environmental sensors such as temperature, wind, light, pressure, etc.

One of the things that helped to spread quickly is that it is open source, and we mean here that all the
technical, design and software details about Arduino and all the projects it does have been published to
enable anyone to access it and modify it .

Arduino is programmed in Arduino C. It has been derived from the C language, which is basically one of the
modern programming languages and one of the most powerful programming languages

As s is tant lecturer : Abrar ghaleb


Arduino Features :
1 . Cheap price.
2. Easy to handle.
3. S implicity of programming language.
4. Availability of many accessories for it.
5. Availability of libraries for most of its accessories.
6. Open source, which helps to speed up its development.
7. We can link it to powerful programming languages like MATLAB,
JAVA , VB.NET.

As s is tant lecturer : Abrar ghaleb


Arduino software
Software programs, called sketches, are created on a computer using the Arduino integrated development
environment (IDE). The IDE enables you to write and edit code and convert this code into instructions that
Arduino hardware understands. The IDE also transfers those instructions to the Arduino board (a process
called uploading).

Arduino hardware
The Arduino board is where the code you write is executed. The board can only control and respond to
electricity, so specific components are attached to it to enable it to interact with the real world.
These components can be sensors, which convert some aspect of the physical world to electricity so that
the board can sense it, or actuators, which get electricity from the board and convert it into something that
changes the world. Examples of sensors include switches, accelerometers, and ultrasound distance
sensors. actuators are things like lights and LEDs, speakers, motors, and displays.

Assistant lecturer : Abrar ghaleb


Arduino types
1- arduino UNO
microcontroller AT mega328p

Operating voltage 5V

Input voltage 7-12 V


(recommended)

Input voltage (limit) 6-20 V

Digital I/ O pins 14 (of which 6 provide PWM


output)

PWM digital I/ O pins 6

Analog input pins 6

DC current per I/ O pin 20 mA

DC Current for 3.3V Pin 50 mA

Flash Memory 32 KB (ATmega328P) of which


0.5 KB used by bootloader

SRAM 2 KB (ATmega328P)

EEPROM 1 KB (ATmega328P)

Clock Speed 16 MHz

LED_BUILTIN 13

Length 68.6 mm

width 53.4 mm Assistant lecturer : Abrar ghaleb

Weight 25 g
Atmel ATmega 1 68 or ATmega
Microcontroller
328
2-Arduino Nano Operating Voltage
5V
(logic level)

Input Voltage
7-12 V
(recommended)

Input Voltage (limits) 6-20 V

1 4 (of which 6 provide PWM


Digital I/O Pins
output)

Analog Input Pins 8

DC Current per I/O


40 mA
Pin

16 K B (ATmega1 68) or 32 K B
Flash Memory (ATmega328) of which 2 KB used
by bootloader

1 KB (ATmega1 68) or 2 KB
SRAM
(ATmega328)

51 2 bytes (ATmega1 68) or 1 K B


EEPROM
Clock Speed (ATmega328)
16 MHz

Dimensions 0.73" x 1.70"

Length 45 mm

Width 1 8 mm

Weigth 5g As s is tant lecturer : Abrar ghaleb


3- Arduino Mega 2560 microcontroller ATmega 2560

Operating Voltage 5V

Input Voltage 7-12 V


(recommended)

Input Voltage (limit) 6-20 V

Digital I/O Pins 54 (of which 1 5 provide


PWM output)

Analog Input Pins 16

DC Current per I/O Pin 20 mA

DC Current for 3.3V Pin 50 mA

Flash Memory 256 K B of which 8 K B used


by bootloader

SRAM 8 KB

EEPROM 4 KB

Clock Speed 16 MHz

LED_BUILTIN 13

Length 101 .52 mm

Width 53.3 mm

Weight 37 g
As s is tant lecturer : Abrar ghaleb
Thank you

As s is tant lecturer : Abrar ghaleb


As s is tant lecturer : Abrar ghaleb

12
Arduino programming language
structure
the basic structure of the arduino programming language is fairly simple and runs at least two parts .
these two required parts , functions , enclose blocks of statement
void setup ( )
{
statements ;
}

void loop ( )
{
statement ;
}

where setup ( ) is the preparation , loop ( ) is the execution both functions are required for the program to work
the setup function declaration of any variable at the very beginning of the program , and is used to set pinMode .
the loop function follows next and includes the code to be execute continuously - reading imputs , triggering outputs etc ..
this function is the core of all arduino programs and does the bulk of the work
Assistant lecturer : Abrar ghaleb
Setup ( )
the set up ( ) function is called once when your program srarts . use it to initialize pin mode or begin serial . it must be
included in a program even if there are no statements to run

void s etup ( )
{
pinMode(pin, OUTPUT); // s ets the 'pin' as output
}

after calling the setup ( ) function , the loop ( ) function precisely what its name suggests, and loops consecutively, allowing
the program to change, respond, and control the arduino board

void loop ( )
{
digitalWrite(pin, HIGH): // turns 'pin' on
delay(1 000); // paus es for one s econd
digitalWrite(pin, L OW ); // turns 'pin’ off
delay(1 000); // paus es for one s econd
}
As s is tant lecturer : Abrar ghaleb
functions
custom functions can be written to perform repetitive tasks and reduce clutter in a program.
this is the type of value to be returned by the function such as 'int' for an integer type function. if no value is to be returned
the function type would be void.

the following integer type function delayVal( ) is used to set a delay value in a program by reading the value of a
potentiometer. it first declares a local variable v . sets v to the value of the potentiometer which gives a number between 0-
1023 then divides that value by 4 for a final value between 0-255, and finally returns that value back to the main program
int delayVal( )
{
int v; // create temporary variable 'v'
v = analogR ead(pot); // read potentiometer value
v /= 4; // converts 0-1 023 to 0-255
return v; // return final value
}

As s is tant lecturer : Abrar ghaleb


Curly braces
curly braces (also referres to as just "braces" or "curly brackets") define the beginning and end of function blocks and
statement blocks such as the void loop ( ) function and the for and if statement

Type function( )
{
statement;
}
an opening curly brace (must always be followed closing curly brace ). this is often referred to as the braces being balanced.
unbalanced beaces can often lead to cryptic, impenetrable compiler errors that can sometimes be hard to track down in a
large program

Semicolon
a semicolon must be used to end a statement and separate elements of the pragram . a semicolon is also used to separate
elements in a for loop

* forgetting to end a line in a semicolon will result in a compiler error. the error text may be obvious, and refer to a missing
semicolon, or it may not. if an impenetrable or seemingly illogical compiler error comes up, one of the first things to check is a
missing semicolon, near the line where the compiler complained
Assistant lecturer : Abrar ghaleb
/*…*/ block comments
block comments, or multi-line comments, are areas of text ignored by the program and are used for large text descriptions of
code or comments that help others understand parts of the program. they begin with /* and end with*/ and can span multiple
lines

/* this is an enclosed block comment don't forget the closing


comment - they have to be balanced
*/
because comments are ignored by the program and take no memory space they should be used generously
* while it is possible to enclose single line comments whithin a block comment, enclosing a second block comment is not allowed

// line comments
single line comments begin with // and end with the next line of code. like block comments, they are ignored by the program
and take no memory space.

// this is a single line comment

single line comments are often used after a valid statement to provide more information about what the statement
accomplishes or to provide a future reminder Assistant lecturer : Abrar ghaleb
Variables
Variables declaration
all variables have to be declared before they can be used. declaring a variable means defining its value type, as in int, long,
float, etc ., setting a specified name, and optionally assigning an initial value. this only needs to be done once in a program
but the value can be changed at any time using arithmetic and various assignments

int inputVariable=0;
example for a simple assignment
a variable
e can be declared in a number of locations throughout the program and where this definition takes place determines
what parts of the program can use the variable

variables : a variable is a way of naming and storing a numerical value for later use by the program. as their namesake
suggests, variables are numbers that can be continually changed as opposed to constants whose value never changes.

a variable needs to be declared and optionally assigned to the value needing to be stored.

Assistant lecturer : Abrar ghaleb


the following code declares a variable called inputVariable and then assigns it the
value obtained on analog input pin 2

int inputVariable=0; // declares a variable and assigns value of 0


inputVariable = analogRead (2); // set variable to value of analog pin 2

'inputVariable' is the variable itself. the first line declares that it will contain an int, short for integer. the second line sets the
variable to the value at analog pin 2. this makes the value of pin 2 accessible elsewhere in the code

as an example to illustrate three useful operations with variables, the following code tests whether the inputVariable is less
than 100, if true it assigns the value 100 to inputVariable, and then sets a delay based on imputVariable which is now a
minimum of 100:

if (inputVariable>100) // tests variable if less than 100


{
inputVariable = 100; // if true assigns value of 100
}
delay(inputVariable); // uses variable as delay

Assistant lecturer : Abrar ghaleb


* variable should be given descriptive names, to make the code more readable. variable names like tiltSensor or pushButton
help the programmer and anyone else reading the code to understand what the variable represents.

*a variable can be named any word that is not already one of the keywords in the arduino language

Variable scope
The variable can be declared before ( ) and it can also be inside the function, and in this way we benefit from specifying the
scope of the variable to benefit from it.

a global variable is one that can be seen and used by every function and statement in a program.

a local variable is one that is defined inside a function or as part of a for loop, it is only visible and can only be used inside the
function in which it was declared. it is therefore possible to have two or more variables of the same name in different parts of
the same program that contain defferent value.

ensuring that only one function has access to its variables simplifies the program and reduces the potential for programming
errors.

Assistant lecturer : Abrar ghaleb


the following example shows how to declare a few different types of variables and demonstrates each variable's visibility

int value; // 'value' is vis ible


void s etup( ) // to any function
{
// no s etup needed
}

void loop( )
{
for (int i=0; i<20;) // 'i' is only vis ible
{ // ins ide the for - loop
i++;
}
float f; // 'f' is only vis ible
} // ins ide loop

Ass is tant lecturer : Abrar ghaleb


Thank you

As s is tant lecturer : Abrar ghaleb


3

13
e
Thank you

Assistant lecturer : Abrar ghaleb


3

14
e
Thank you

As s is tant lecturer : Abrar ghaleb


3

15
e
changing the range of value
Problem You want to change the range of a value, such as the value from
analogR ead obtained by connecting a potentiometer or other device that provides
a variable voltage. For example,
suppose you want to display the position of a potentiometer knob as a
percentage from 0 percent to 100 percent.

/*
Map sketch map the range of analog values from a pot to scale from 0 to 100 resulting in an LE D blink
rate ranging from 0 to 100 milliseconds and Pot rotation percent is written to the serial port
*/
const int potPin = 0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LE D
void setup()
{
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600);
}
void loop() {

int val; // The value coming from the sensor

int percent; // The mapped value

val = analogR ead(potPin); // read the voltage on the pot (val ranges from 0 to 1023)

percent = map(val,0,1023,0,100); // percent will range from 0 to 100.

digitalWrite(ledPin, HIGH); // turn the ledPin on

delay(percent); // On time given by percent value

digitalWrite(ledPin, LOW); // turn the ledPin off

delay(100 - percent); // Off time is 1 00 minus On time

}
Displaying Voltages Up to 5V
Problem
You want to monitor and display the value of a voltage between 0 and 5 volts. For example, suppose you
want to display the voltage of a single 1.5V cell on the Serial Monitor.
Solution
Use analogRead to measure the voltage on an analog pin. Convert the reading to a voltage by using the
ratio of the reading to the reference voltage (5 volts), as shown in Figure below.
/*
Display 5v Or less sketch
prints the voltage on analog pin to the serial port
Warning - do not connect more than 5 volts directly to an Arduino pin.
*/
const int referenceVolts = 5; // the default reference on a 5-volt board
const int batteryPin = 0; // battery is connected to analog pin 0
void setup()
{
Serial.begin(9600);
}
void loop()
{
int val = analogRead(batteryPin); // read the value from the sensor
float volts = (val / 1023) * referenceVolts; // calculate the ratio
Serial.println(volts); // print the value in volts
}
Thank you

Assistant lecturer : Abrar ghaleb

You might also like