Arduino Handbook
Arduino Handbook
Abdelrahman Hammad
4
Contents
1 Memo 1
2 Cover Letter 3
3 Preface 7
4 So what is an Arduino? 8
5 Bare Basics 9
5.1 void setup() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.2 void loop() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.3 Analog Versus Digital . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.4 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.4.1 Input Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.4.2 Output Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6 Data types 11
6.1 int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6.1.1 Digital data as integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6.2 float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6.2.1 Analog data as floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6.3 strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6.4 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6.5 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6.6 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
7 Conditionals 13
7.1 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7.2 if......else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7.2.1 if......else if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7.3 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7.4 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
8 Loops 14
8.1 for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
8.2 while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
8.3 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
8.4 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
9 Functions 16
9.1 Function structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
9.1.1 Return type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
9.1.2 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
9.2 Using the Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
9.3 Make your own function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
9.4 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
9.5 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5
10 Good Functions you need 17
10.1 I/O operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
10.1.1 pinMode() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
10.1.2 digitalRead() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
10.1.3 digitalWrite() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
10.1.4 anaolgRead() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
10.1.5 analogWrite() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
10.2 delay() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
10.3 Serial.print() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
10.4 millis() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
11 Processing 19
11.1 How to send data to processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
11.2 How to receive this data ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
11.3 Formatting correctly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
11.4 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
12 Addendum 20
12.1 Surveys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
14 bibliography 23
List of Figures
1 Example of some code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 Example of an arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Every code’s bare minimum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4 Example of variable use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5 Example of an if-else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6 Example of for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
7 Example of function use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
8 Example of Simple Processing Code . . . . . . . . . . . . . . . . . . . . . . . . . 19
9 Coding platforms used by 35 Students before ENME351 . . . . . . . . . . . . . . 20
10 Coding Experience of 35 students before ENME351 . . . . . . . . . . . . . . . . . 21
11 Coding time of 35 students during ENME351 . . . . . . . . . . . . . . . . . . . . 21
12 Opinion of 35 students on manual for ENME351 . . . . . . . . . . . . . . . . . . 21
List of Tables
1 Which pin does what input ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2 Which pin does what output ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6
3 Preface
Figure 1 shows a very basic example of sample code of Arduino.
This is what is expected of students to be able to do to succeed
in the class.
I plan to present the required information as follows:
Present a good summary of all the tools you will have at your
disposal. This will be brief and won’t explain how to use them, I
do that instead throughout the manual. 5 → Explain Data types
in section 6 → Explanation of conditionals and loops in section
7 and 8 → Explanation of functions in section 9 → Explanation
of good function to use in section 10→ Explanation of processing
in section ??.
Furthermore each section will either have good examples or ac-
tivities that tie the concepts used in the electronics lab.
7
4 So what is an Arduino?
The best way to understand an Arduino is to consider a human brain, because in essence it
is a brain too. Your brain constantly receives signals from its environment sent by your senses
all over your body. This data is then analyzed and when necessary the brain then produces
an output in the form of your body moving. For instance when you accidentally touch a hot
stove, your brain receives input from your fingers which causes output in the form of your hand
moving away. If you understood that, then you now understand what an Arduino is.
As seen in figure 2 the Arduino has many different pins, with many different labels. Here is
how they break down:
Input: Those are all the pins with A before them.
Output: Those are all the pins from 0 all the way to 13
Power: All other pins are power related for powering up your circuit
1
According to arduino.cc
8
5 Bare Basics
As seen every Arduino code you will write will always have two blank function. Figure 3
shows an example of what you are given to start. The setup function and the loop function.
void setup () {
Once the Arduino program is set up, this code block is run only once
}
void loop () {
Anything in this code block runs as long as the Arduino is running
}
Digital data is either HIGH or LOW regardless of inputting or outputting a value. Noth-
ing else no in betweens.
Input: 0 or 1 (Which is the same as LOW or HIGH)
Output: 0 or 1 (Which is the same as LOW or HIGH)
Analog data on the other hand is usually a continuous range, and also depends on wether
we are inputting or outputting.
Input: 0 - 1023, ex:2.0, 5.7,999.9,438.8,1023.0,0.0, 1 1034.7, -9.8
Output: 0 - 255 ex:2.0, 5.7, 0.0, 255.0, 277.9, -10.9
9
with which pins serve as outputs and which pins serve as inputs using table 1 and table 2
respectively.
Input
Pins that have Digital input Pins that have Analog input
0,1,2,3,4,5,6,7,8,9,10,11,12,13 A0, A1, A2, A3, A4, A5
Output
Pins that have Digital output Pins that have Analog output
0,1,2,3,4,5,6,7,8,9,10,11,12,13 3,5,6,9,10,11
10
6 Data types
Before learning how handle input or how we make motors move, it is essential to learn how
data is manipulated. There are three important types of data needed for this class: int float
string.These data types are essential and they should be like your name.
6.1 int
An integer is any number x ∈ the set of real integers Z. That is
in other words it has to be a number with no decimal place.
Examples: 5, 6, 7, 8, 1023, 0, -4, -3
6.2 float
A float is any number x ∈ the set of real numbers R. That is in
other words it has to be a number with a decimal place.
Examples: 5.2, 2.6, 7.0, 8.9 , 1023.1, 0.0, -4.2
6.3 strings
A string is a combination of characters such that each char-
acter c ∈ Anything. That is in other words it can be
letters, numbers, strange symbols, anything.
Examples: ”hello”, ”I have 3 cats”, ”I have $3 in my bank account”, ”123”
6.4 Variables
Arguably the best thing about coding: Variables. Variables is something like human memory.
You associate a memory with something. For example, soccer. Soccer is a sport that you like to
play. So your brain would file it in this form; Sport soccer = fun. Sport is the type of activity,
soccer is the activity name, and fun is what your brain remembers about it.
11
(int/float/string) x = ”Hello” → string x = ”Hello”. Your Arduino brain now knows x is ”hello”
6.5 Activity
1. Is 4 an int or a float or a string ( Int / float / string )
6.6 Solutions
1. int
2. float
3. string
4. string
7. Yes
8. float x
9. int x
12
7 Conditionals
7.1 if
The if statement is one of the most powerful control statements
in Arduino. The best way to think of it is as a traffic light.
Imagine you’re driving down route one. You see a traffic light
ahead. Then you do an evaluation, and IF the light is green you
go ahead.
7.2 if......else
If else is probably used when you have many different possible
scenarios and not necessarily just two. Lets use the traffic light
example on more time. You see a traffic light ahead. Then you
do an evaluation, and IF the light is green you go ahead, ELSE
stop.
7.2.1 if......else if
Figure 5: Example of an if-
A more advanced control statement is when you nest an if inside else
an else to cover more cases. Once again lets use the traffic light
example. You see a traffic light ahead. Then you do an evalua-
tion, and IF the light is green you go ahead, ELSE IF the light
is yellow then be careful ELSE Stop. You have no covered all
the cases of a traffic light. Refer to figure 5 to see how the code should look like.
7.3 Activity
1. Assume that I have a int variable called input. This input has a value of HIGH. I run
this code on it:
String answer;
if (input == LOW) { Note: == Checks for equality
answer = ”Output 1”;
} else {
answer = ”Output 2” ;{
}
2. Assume that I have a float variable called input. This input has a value of 45.7. I run this
code on it:
String answer;
if (input > 49) { Note: == Checks for equality
answer = ”Output 1”;
} else if (input ¡ 40){
answer = ”Output 2” ;{
} else{
answer = ”Output 3” ;
}
13
7.4 Solutions
1. Output 1
2. Output 3
8 Loops
Loops in a nutshell is like the gym teacher who mad you run lap every time he saw you. The
best way to explain this is to model the coach’s orders in an Arduino loop.
First lets implement the track example where the coach wants
you to run 10 laps in a basic english format.
for( Current lap is 0; Current lap is less than 10; Increase current
lap by 1 ){
Sweat a lot in between
} Figure 6: Example of for
loop
Now we need a checklist to convert this english statement into
executable Arduino Code:
1. We need to identify our variable type. Since we know laps is a discrete number (i.e.
0,1,2,3) we should use an (int / float / string) variable. int CurrentLap = 0
2. We need to convert our condition to an expression for Arduino to evaluate. You need to
use one or more of the following <, =, > operators to achieve this. CurrentLap < 10
3. We also need to convert our final the change we want the Arduino to make to our variable
to an expression that can be evaluated. CurrentLap = CurrentLap + 1
14
while(expression ){
}
Now let’s do our example in a while loop format. While loops are a lot simpler because you
only place the expression in as a parameter
int CurrentLap = 0 ;
while(CurrentLap < 10 ){
CurrentLap = CurrentLap + 1;
}
8.3 Activity
1. If you know you want to iterate over a list a certain number of times which is better ?
(For Loop/ While Loop)
8.4 Solutions
1. For, because while are usually used when we don’t know how many iterations are gonna
occur before our condition is met
3. Never stops working, prints hello infinite times because were not increasing our counter
4. 5 times
15
9 Functions
Functions play an extremely important role in making Arduino coding very easy.
3. ()→ Since there is nothing in the parentheses, this means that this function takes no
parameters
9.1.2 Parameters
Parameters is just what you pass in to a function as its inputs. A prime example is if you try
to make cereal. I am 99 percent positive you can’t make cereal without milk and the actual
cereal. This specific task requires milk and cereal to make. Using everything we’ve discussed
lets do an example in simple english format.
Bowl Of Cereal cooking (Dairy x, Cereal y)
mix x and y
return Bowl of Captain Crunch
Note that parameters are first entered as variables (refer to section 6.4 for variables).
16
9.4 Activity
1. What is the return type of the following function ? What about its list of parameters ?
String testfunction () {
2. What is the return type of the following function ? What about its list of parameters ?
float testfunction (float a) {
3. Given the following signature : int testfunction (int a){ . Would the following calls be
valid ?
int y = testfunction(4.2) (Valid / Invalid)
float y = testfunction(4) (Valid / Invalid)
int y = testfunction(4) (Valid / Invalid)
9.5 Solutions
1. Returns a string, and has an empty list of parameters.
2. Returns a float, and takes one float as a parameter.
3. Invalid, Invalid, Valid
17
10.1.2 digitalRead()
digitalRead() → input → Reads HIGH or LOW → Return type of int (Refer to section 9.2)→
requires pin number as parameter. Example: int value = digitalRead(11)
10.1.3 digitalWrite()
digitalWrite() → output → Send HIGH or LOW → Return type of Void (Refer to section 9.2)→
requires pin number as parameter, and whether you want to send HIGH or LOW. Example:
digitalWrite(11,HIGH)
10.1.4 anaolgRead()
analogRead() → input → Reads between 0 and 1023 → Return type of float (Refer to section
9.2)→ requires pin number as parameter. Example: float value = anaolgRead(11)
10.1.5 analogWrite()
analogWrite() → output → Send HIGH or LOW → Return type of Void (Refer to section 9.2)→
requires pin number as parameter, and the value you want to send which has to be between 0
and 255. Example: anaolgWrite(11,144.4)
10.2 delay()
delay() is probably one of the most important functions you will use. It has a return type of
void. It has a parameter list of one integer. Remember how we said in section 5.2 that the
loop() function just keeps on going and going forever. Well sometimes you want to stop taking
reading or stop outputting readings. delay() is your tool. It holds the program on pause for as
many millseconds as you input it. So let’s say I want a 5 sec pause in my program then I can
simply just say delay(5000);. Once the inputted integer must be in terms of milliseconds and
not seconds.
Key things to note:
1. No need for a variable to receive anything since there is no return (Refer to section 9.2).
2. Input should be an int in the form of milliseconds
3. Program is suspended for the inputted value of microseconds
10.3 Serial.print()
Serial.println() helps you visualise values that you are getting. Its your built in, unlimited ink
HP printer. This prints values as strings to the serial monitor to see results of the code and
hardware you set up. This is usually a good place to extract information after processing.
10.4 millis()
Perhaps the neatest but trickiest function you have in your toolset. If you’ve ever watched soccer,
basketball, or almost any sport, you’ll notice they all have time keepers. millis() is your time
keeper. What you ask is: How long has my Arduino been running for ? The answer you get: A
value representing how long the Arduino since you first ran your program. Key things to note:
1. Should include a variable to receive anything since millis() returns an int value (Refer to
section 9.2).
18
2. List of parameters is empty
3. WHY ? Because you can do an action at certain time frequency (Every second
print this, or every 5 seconds read this) without using delay() and pausing your
program
11 Processing
What is processing ?
Processing is a flexible software sketchbook and a language for learning how to
code within the context of the visual arts. Since 2001, Processing has promoted
software literacy within the visual arts and visual literacy within technology. There
are tens of thousands of students, artists, designers, researchers, and hobbyists who
use Processing for learning and prototyping. 2
Basically processing helps you visualise data. In our case, it will help us plot our information.
To help explain how processing works I will use a piece of code posted in one of Dr.Don Devoe’s
lab manuals.
19
11.3 Formatting correctly
Usually what happens is we send 3 or 4 values through Arduino to processing split by a comma
or a tab. How can we extract those values and make them useful to use ? Through the use of
a function called split().
This is used in the example above as follows:
xyRaw = splitTokens(inString, ”,”);
It takes our input of 4 or 5 elements and it splits them into an array data structure. What does
it split them by though ? By the comma.
For example an input of ”3, 4, 5” would be put in an array such that the first element with
index 0 is 3, second element with index 1 is 4, and third element with index 2 is 5.
What if I wanted to split them based on the letter a ? The code would change to the following:
xyRaw = splitTokens(inString, ”a”);
11.4 Plotting
Finally plotting. After we extract out values from the array. We can now use the ellipse function
to plot our numbers on a nice graph. Line of code would look like this:
ellipse(x, y, 3, 3);
12 Addendum
12.1 Surveys
The following are the surveys I have collected regarding the manual.
20