ARDUINO
By Kiran Hart
GETTING STARTED
Learn how to setup the Arduino IDE
STEP 1: PLUG IN YOUR ARDUINO
When you plug your Arduino board into the computer, verify that
the Arduino lights up. The led next to the `UNO` writing should turn
on.
You should be This light should
connecting your be on once you
Arduino using this port. plug it in.
STEP 2: OPEN THE ARDUINO IDE
Once you plug it in, open the Arduino IDE, before
we can program it, we need to make sure that it’s
reading the Arduino correctly.
1. Click tools, go to board and select
Arduino/Genuino Uno
2. After that go to `Port` under tools and select
the COM which contains the (Arduino/Genuino
Uno) text as seen in the top picture.
3. That’s all for setting it up, we can now get into
the programming.
PROGRAMMING
In this section you’ll learn how to program the Arduino board.
SETUP() AND LOOP()
What are they? Well their names pretty much explain them. Within
the setup method you put all the code you want to run ONCE, this is
usually setting up all the Pin Modes, initializing the serial monitor,
pretty much anything that only needs to be ran once.
All the code you write within the loop() {} function will be ran over
and over until it’s told to stop or the program closes.
All the code is ran from top to bottom, even
within the setup method, so it’s important
that you initialize things in order to prevent
any errors. Once the program reaches the
end of the loop, it will simply go straight to
the top and repeat the entire process
again.
DATA TYPES
A data type just means the way you wish to store a value within a
variable, there a bunch, but here are some of the most commons
you will always be using.
1. bool – This means that the variable can either hold a true or
false value or 1 / 0
2. int – This will hold a whole number like 3 or 5000
3. String – You can create sentences with spaces or any character,
the value must be within “ “.
4. float - Unlike an int of which holds a whole number, floats can
store a number that has a decimal point like 25.7
VARIABLES
Variables are things that store values that
you set. To create a variable:
1. Specify which datatype you wish to
use (see previous slide for common
datatypes)
2. Give it a name, this can be anything
you want, when creating a multi-word
variable use lowerCamelCase.
3. Give it a value by adding an equal
sign writing a value after it.
4. End the statement using a semi colon.
Remember Strings must be within quotes,
and you can use either 1 or 0 for a bool
instead of true/false.
PIN MODES
In every Arduino program you will need to specify what
each pin should do, should it be an input or should it be
an output? This will only be done once within the
setup() method. Here’s an example:
The pinMode() function takes in two values, the first
being a whole number (# of the pin) and the second
being either OUTPUT or INPUT.
In this example I’m using my variables which are int’s
that store the pin numbers, then I pass them into the
pinMode function within the setup and specify whether
the Arduino needs to OUTPUT to the pin or it needs to
read an INPUT.
DIGITAL WRITE
The digtalWrite() function is a simple one, all it does is either send a
LOW or HIGH value to the specified pin, when sending a HIGH value
it’s sending 5 volts to the pin.
1. Let’s try to use it, in the setup() set pin 13 to an
OUPUT using the pinMode function. (Pin 13 is the
on board LED)
2. Inside the loop() function use the digitalWrite()
function, the first value should be whichever pin
you want to write to, in our case it’s 13. Since
we want to turn the onboard LED on we will send
a HIGH value to it
Once you do that, upload the code to the Arduino. If
you did everything right, the onboard LED should
DIGITAL READ
Unlike the digitalWrite() function the
digitalRead() is meant to read a value
from a specific pin. It also only requires
one value passed into the ().
In this example, we’re setting pin #2 to
an INPUT and then constantly updating
the value of `buttonValue` by using
digitalRead(buttonPin) which is reading
the value of pin #2.
SERIAL MONITOR
The Serial Monitor is a very useful tool as it allows us to send
messages to a console helping us debug our program. It’s quite
simple to use, here’s how you set it up.
DELAY
The delay function is probably the most simplest one out of all of
them, all it does is pause the program for x amount of time. Here’s
how to use it.
Just write delay(); and put a
number in the parameters, the
number represents milliseconds,
so in the picture I said 1000
milliseconds or 1 second. So it’s
reading the button value,
pausing for 1 second, printing the
value to the serial monitor then
pauses for another second before
repeating the loop.
ARITHMETIC OPERATORS
There’s no escaping math, in programming you will always be using
math even if it’s just some simple addition. Each programming
language has math operators that you can use. NOTE: Don’t
forget about your order of operations.
1. % - Using this will get the remainder. Ex. (x = 7 % 5) x will be 2
since 5 can only go into 7 once, leaving 2 remainder.
2. * - Can multiply numbers. Ex (x = 5 * 5) x will be 25, since 5
times 5 is 25.
3. + and - You can add / subtract numbers. Ex (x = (5 + 5) – 2 ) x
will be 8 since 5 + 5 is 10 – 2 which equals 8.
4. / - You can divide using this. Ex. (x = 10 / 5) x will be 2 since 10
divided by 5 is 2.
COMPARISON OPERATORS
These just let you check if things are equal to each other or if
numbers or not equal, greater than, less than another.
1. != This means not equal to Ex. If (5 != 4) {do something}
2. < This means less than Ex. If (5 < 6) {do something}
3. > This means greater than Ex. If (10 > 9) {do something}
4. <= This is less than or equal to Ex. If (4 <= 5) {do something}
5. >= This means greater than or equal to Ex. If (5 >= 5) {do
something}
6. == This means both values are equal Ex. If (100 == 100) {do
something}
AND, OR, NOT
What are these? Well they’re Booleans operators. They’re very
simple to use. But what do they mean?
&& (and) This means that (statement one) && (statement two)
must be true
|| (or) This means that (statement one) || (statement two) must
be true
! (not) This means ! (statement) is not, it reverses true/false. So !
true will be false and !false will be true, so if the statement is true,
it’s now false, vice versa.
IF / ELSE STATEMENTS
IF / ELSE statements are used to check if
something is “true” if so run some specific
instructions “ELSE” if it’s not true, run another
set of instructions. Here’s an Example:
int age = 19
if (age >= 18) {
Run the code
} else if (age >= 13 && age <= 17) {
Run the code
} else {
Run the code
}
CONDITIONAL OPERATORS (?)
Conditional operators are quite cool. They allow you do something if
the given expression is true, otherwise do another. They’re like if / else
statements but shorter. These are usually used when assigning variable
data. Here’s how to use them.
PROJECTS
Now that you know the basics of programming, let’s make some projects
1. Blink an LED ON/OFF
2. Fade an LED ON/OFF
3. Read a push button input PROJECT LIST
4. Turn an LED on using a push
#1
button
5. Read the temperature using
an LM35
6. Ultra Sonic Sensor distance
check
BEFORE WE
START
Before we can start, we
need to connect our
Arduino to our
breadboard, now this is
quite easy as the
Arduino has 3 Ground
connections, a 5V and
3.3V, follow this image.
Now I’m using the GND
next to the 5V but you
can use whichever one
you want.
Blink an LED ON and
OFF
Items Needed:
• An Arduino
• A Breadboard
PROJECT #1
• An LED
• Jumper Wires
PROJECT #1 - WIRING
1. Connect your GND/5V from the
Arduino to the breadboard.
2. Connect the LED cathode to
ground
3. Connect the LED anode to a
digital pin, I chose 1 in this
example.
PROJECT #1 - PROGRAMMING
1. First I create an int called `ledPin`
and assign it a value of 1 since we
connected our LED to digital pin #1
2. Within the setup method I set the pin
mode of digital pin #1 to OUTPUT.
3. Within the loop method I use
digitalWrite to set the `ledPin` to
HIGH (turn the led on)
4. I delay the process by 1 second
5. Using the digitalWrite once more I
set `ledPin` or digital pin #1 to LOW
(turn the led off)
6. I delay it by a second once more
before letting the loop restart.
Fade an LED ON and
OFF
Items Needed:
• An Arduino
• A Breadboard
PROJECT #2
• An LED
• A 220 Ohm resistor
• Jumper Wires
PROJECT #2 -
WIRING
1. Connect your GND/5V from
the Arduino to the
breadboard.
2. Connect the LED cathode to
ground
3. Connect the 220 Ohm
resistor to the anode end of
the LED
4. Connect the other end of the
resistor to a digital pin, in
my case I chose digital pin
#3
5. I chose 3 because we will be
using analogWrite() instead
of digitalWrite(). AnalogWrite
works best on pins 3, 5, 6, 9,
10 and 11.
PROJECT #2 - PROGRAMMING
1. First I create 3 int variables, one two store the
pin the LED in connected to, a brightness
counter, and the increment.
2. I then set the pinMode to OUTPUT on ledPin
(digital pin #3)
3. Within the loop I use analogWrite() instead of
digitalWrite since it allows to change the
brightness of an LED.
4. I then increment the brightness by the
increment. (+= is a shorter way to adding to a
#
5. Then I check if the brightness is equal to || (or)
255 then revert the increment, by setting it to
negative of itself.
6. I then delay the loop by 30 milliseconds, this
number can be changed around, but 30 seems
Read a push button
input
Items Needed:
• An Arduino
• A Breadboard PROJECT #3
• A 10k resistor
• Jumper Wires
• A push button
PROJECT #3 -
WIRING
1. Connect your GND/5V from
the Arduino to the
breadboard.
2. Connect the button left pin
of the push button to +5v
3. Using a 10k resistor
connect the bottom right
pin of the push button to
ground.
4. Now connect the top right
pin of the push button to a
digital pin, I chose digital
pin #1 for this example.
PROJECT #3 - PROGRAMMING
1. I first created two int variables, one to store
which pin the button is connected to, and
one to store the value of the button.
2. In the setup I set the pinMode of the
`buttonPin` to an INPUT since we’re going to
be reading data from the button. I also start
the Serial Monitor (see slide 12) so we can
output the buttonValue.
3. Within the loop I set the buttonValue variable
to the value of the button by using the
digitalRead method, this will read a value on
a specific pin (pin 1 in our case)
4. Finally I print the value of `buttonValue` to
the serial monitor, when the button isn’t
being pressed it should print 0, when it’s
being pressed it should print 1.
Read a push button
input
Items Needed:
• An Arduino
• A Breadboard PROJECT #4
• A 10k resistor
• Jumper Wires
• A push button
• An LED
PROJECT #4 -
WIRING
1. Connect your GND/5V from
the Arduino to the
breadboard.
2. Connect the bottom left pin of
the push button to +5v
3. Using a 10k resistor connect
the bottom right pin of the
push button to ground.
4. Now connect the top right pin
of the push button to a digital
pin, I chose digital pin #1 for
this example.
5. Connect the LED Cathode to
ground.
6. Connect the LED Anode to a
digital pin, I chose digital pin
#2
PROJECT #4 - PROGRAMMING
1. Building off the last project, I create a new
int variable named `ledPin` and assign it to
2.
2. I then setup the pinMode for the `ledPin` to
OUTPUT, since we’re going to be sending
data to that pin #.
3. Within the loop, I use and if / else statement
to check if the button is being pressed (if
buttonValue is 1) if so, digitalWrite a HIGH
value to the pin the led is connected to,
otherwise, send a LOW value causing it to
turn off.
4. At the very bottom I show you how to use a
conditional operator to either send a HIGH /
LOW value to a digital pin. So if (buttonValue
equals 1) ? Use digitalWrite to send a HIGH
Read the temperature
using an LM35 Chip
Items Needed:
• An Arduino
• A Breadboard
PROJECT #5
• Jumper Wires
• An LM35 Chip
LM35 TEMPERATURE CHIP
Before we get into the wiring, we’ve got to know which pins are which on the LM35.
Unless stated otherwise, the standalone chip will have the same vcc/gnd/vout pins.
In some cases if the sensor is attached to a board, the manufacture will state on the
board which pin does. Note the flat side of the sensor is the front, so keep that in
mind when wiring it. The standalone is on the left, board on the right.
As you can see in this case,
the data pin / signal is all the
way on the right compared to
being in the middle on the
+5 standalone sensor.
Data
v
pin
Groun
d
PROJECT #5 -
WIRING
1. Connect your GND/5V from
the Arduino to the
breadboard.
2. Connect the GND pin on
the LM35 to ground on the
breadboard.
3. Connect the power pin on
the LM35 to +5v
4. Connect the data pin
(middle one) on the LM35
to an analog in pin, I chose
analog pin 0 (A0)
PROJECT #5 - PROGRAMMING
1. First I create an int variable storing the
analogPin (A0) since that’s where the sensor
is connected to. Then I create a float (since
we’re going to be using decimal numbers)
and set it to zero by default.
2. In the setup I set the temperature pin (A0)
to an INPUT since we’re going to be reading
data. I also start the serial monitor on 9600.
3. In the loop I update the `temperature`
variable by using analogRead, I passed in
the pin that it should read from
`temperaturePin` or A0.
4. Now we update the `temperature` using the
formula to convert Fahrenheit to Celsius. So
we set it to itself minus 32 then we multiply
it by 5 divided by 9
Ultrasonic sensor
distance check.
Items Needed:
• An Arduino
• A Breadboard
PROJECT #6
• Jumper Wires
• An HC-SR04
Ultrasonic sensor
PROJECT #6 -
WIRING
1. Connect your GND/5V from
the Arduino to the
breadboard.
2. Connect the GND pin on
the sensor to ground on
the breadboard.
3. Connect the power pin on
the sensor to +5v
4. Connect the Trig and Echo
pin to a digital pin, I chose
pin 4 for the echo pin and
pin 3 for the Trig pin.
PROJECT #5 - PROGRAMMING
1. First create two int variables that store the pin which you
connected the trig and echo pin to.
2. Create a long variable for the duration of the sensor, it doesn’t
need any values right now, also create another int variable to
store the distance.
3. Within the setup, set the trigPin variable to OUTPUT and the
echoPin to INPUT and start the serial monitor on a 9600 bit data
rate.
4. Within the loop we’re going to use a new delay method called
`delayMicroseconds` it’s same the as delay() but works in
microseconds. So send a low value to the trig pin, delay for 2
microseconds, send a high value to the trig pin, delay for another
10 microseconds then send low value back to the trig pin.
5. Now assign a value to the duration variable using pulseIn(),
passing in the echoPin variable as the first parameter and giving it
a HIGH value for the 2nd parameter.
7. Binary Counter (1 - 7
Segment)
PROJECT LIST
8. IR Sensor (Black/White
#2
detection)
Binary Counter (One 7
Segment display)
Items Needed:
• An Arduino
• A Breadboard
PROJECT #7
• Jumper Wires
• A common anode 7
segment display
7 SEGMENT DISPLAY (COMMON
ANODE)
We will be using the Common Anode 7 segment
display, before we can start the project you
should know how it’s wired. More specifically
which pins represent each letters / segments.
There are 7 segments, each as their own pin
that can be used to either turn it on and off.
PROJECT #7 -
WIRING
1. Connect your GND/5V from the
Arduino to the breadboard.
2. Connect a 100 Ohm resistor
from pin 3 of the 7 segment
display to ground.
E to digital pin #6
D to digital pin #5
C to digital pin #4
B to digital pin #3
A to digital pin #2
F to digital pin #7
G to digital pin #8
PROJECT #7 - PROGRAMMING
1. First we create a variable to store the current number it
should display.
2. Then we make a multi-dimensional array to store the
number values. 1 meaning high , 0 meaning low. For
example nums[0] will give us the array containing the
values to print zero.
3. We then setup our pins from 2 to 8 as an OUTPUT
4. Then we make a method called printNumber that will
take in a int parameter, this will loop through the
specified index of nums and digitalWrite either high or
low based on each of the 7 values that it finds in the
array index.
5. Within the loop we simply call the printNumber
function, passing in the current number it should print.
Then we just increment the number if it’s less than ten,
if it’s 10, reset it to zero.
PROJECT 7 - HELP
Code shown in PowerPoint:
[Link]
no
Long code/Easy Code:
[Link]
ino
Multi-dimensional Arrays:
[Link]
Function/Methods:
[Link]
IR Sensor
Items Needed:
• An Arduino PROJECT #8
• A Breadboard
• Jumper Wires
• QTI Sensor
PROJECT #8 -
WIRING
1. Connect your GND/5V from the
Arduino to the breadboard.
2. Connect pin B from the sensor to
negative
3. Connect pin W from the sensor
to positive
4. Connect pin R from the sensor to
a digital pin on the Arduino.
Pin R will be sending the data, so
make sure you know which digital
pin you connect it to, for future use
in the code.
PROJECT #8 - PROGRAMMING
1. First we create a int variable to store the pin our sensor
is connected to called `sensor` and assign it a value of
4
2. We then setup the serial monitor on a 9600 bit data
rate.
3. In the loop, we need to charge the capacitor, so we set
the pinMode to OUTPUT on the sensor pin, then
digitalWrite a HIGH value to the sensor, then delay for 1
millisecond.
4. Now we discharge the capacitor, but instead of OUTPUT
and HIGH we will use INPUT and LOW to release it.
Delay for another 1 millisecond.
5. Now lets create a variable called `sensorValue` and
assign it to digitalRead(sensor) which reads the value of
the sensor. We used a byte since the number should
only be a zero or one.
6. Now we check if the sensorValue is equal to zero, if it is
THE END
Congratulations you’ve reached the end.
All the code & Images used throughout this presentation can be
found at:
[Link]