Programming With Arduino
Programming With Arduino
Hans-Petter Halvorsen
https://www.halvorsen.blog
|Programming with Arduino|
Hans-Petter Halvorsen
Copyright © 2018
ISBN: 978-82-691106-3-0
Publisher: 978-82-691106
With this textbook you will learn the basics about computers,
basic electronics, sensor and measurement technology and
programming.
https://www.halvorsen.blog
| Programmering with Arduino | 2018.10.16
https://www.halvorsen.blog
|Hans-Petter Halvorsen |
Table of Contents
1. Introduction to Arduino
2. Basic Electronics
3. Arduino Development Environment
4. Arduino Programming
5. Arduino Examples
#1. Electrical Circuit , #2. Blinking LED, #3. Switch,
#4. Potentiometer, #5. Temperature, #6. Light Sensor, #7. Thermistor
Hans-Petter Halvorsen 1
| Programmering with Arduino | 2018.10.16
Equipment Resistors
Temperature sensor
Switch Arduino
Potentiometer Light sensor USB cable
LEDs
Breadboard wires
Multimeter
Thermistor
Hans-Petter Halvorsen 2
| Programmering with Arduino | 2018.10.16
https://www.halvorsen.blog
Introduction to Arduino
|Hans-Petter Halvorsen |
USB for PC
2
connection
5V, GND 4 5
Hans-Petter Halvorsen 3
| Programmering with Arduino | 2018.10.16
PC
Arduino
Hans-Petter Halvorsen 4
| Programmering with Arduino | 2018.10.16
https://www.halvorsen.blog
Electronics Foundation
|Hans-Petter Halvorsen |
Electrical Circuit
Here you see a basic Electrical Circuit:
Light
Light
+
Power Supply Current
Battery
- +
-
Power Supply Battery
Hans-Petter Halvorsen 5
| Programmering with Arduino | 2018.10.16
Switch
Light
Light
+ On/Off
Bryter
Battery
- +
-
Battery
[http://www.fritzing.org]
Hans-Petter Halvorsen 6
| Programmering with Arduino | 2018.10.16
Short Circuit
• We must never connect positive and negative side to a
power source without having an electrical component
in between.
• If you do, it is called a short circuit.
• For example, if you short circuit a battery, the battery
will get very hot and the battery will run out very
quickly. Short Circuit!!
• Some batteries may also start to burn.
• When it starts to smoke from electrical components, it
happens because it has become too hot.
• In most cases, it means that the component is broken. - +
Power Supply
Ohms Law
Electrical Circuit
This is Ohms Law:
- +
! = #$
! !
! – Voltage [&] #= $=
# – Resistance [Ω]
$ – Current [A]
$ #
Hans-Petter Halvorsen 7
| Programmering with Arduino | 2018.10.16
Ohms Law
! = #$
Multimeter
https://learn.sparkfun.com/tutorials/how-to-use-a-multimeter
Hans-Petter Halvorsen 8
| Programmering with Arduino | 2018.10.16
[Wikipedia]
Resistors
Resistance is measured in Ohm (Ω)
Hans-Petter Halvorsen 9
| Programmering with Arduino | 2018.10.16
Hans-Petter Halvorsen 10
| Programmering with Arduino | 2018.10.16
http://www.allaboutcircuits.com/tools/resistor-color-code-calculator/
The total resistance of resistors connected in series is the sum of their individual resistance values.
When we have resistors in series, the sum of the sub-voltages is equal to the voltage of the voltage source
Resistors in Parallel :
... 1 1 1 1
= + + +⋯
!" !# !$ ! ! !" !# !$
Hans-Petter Halvorsen 11
| Programmering with Arduino | 2018.10.16
0" 0# 0$
Kirchhoff’s Voltage Law:
(
." .# .$
% .) = 0
&'" .
. = ." + .# + .$ + ⋯
https://en.wikipedia.org/wiki/Kirchhoff%27s_circuit_laws
Switch
A switch breaks the flow of current through a
circuit when open. When closed, the current will
flow unobstructed through the circuit.
Light
Switch
- +
Hans-Petter Halvorsen 12
| Programmering with Arduino | 2018.10.16
Breadboard
A breadboard is used to wire
electric components together
Hans-Petter Halvorsen 13
| Programmering with Arduino | 2018.10.16
https://www.halvorsen.blog
Arduino Development
Environment
|Hans-Petter Halvorsen |
Hans-Petter Halvorsen 14
| Programmering with Arduino | 2018.10.16
Editor Preferences
Hans-Petter Halvorsen 15
| Programmering with Arduino | 2018.10.16
TRY IT OUT!
Blinking LED Example
Arduino UNO has a
built-in LED that is
connected to Port 13
void setup()
{
pinMode(13, OUTPUT);
}
Make a Program that makes
the built-in LED blinking void loop()
{
Turn ON LED digitalWrite(13, HIGH);
Wait 1 Second delay(1000);
Turn OFF LED digitalWrite(13, LOW);
delay(1000);
Wait 1 Second
}
void loop()
{
digitalWrite(13, HIGH); Try to change from
delay(1000); 1000 to 100
digitalWrite(13, LOW); – What happens then?
delay(1000);
}
Hans-Petter Halvorsen 16
| Programmering with Arduino | 2018.10.16
You use the Serial Monitor when Debugging Arduino programs or when you
want to show data or values from your program.
You need to have Arduino connected to your PC in order to use the Serial
Monitor. void setup()
{
Serial.begin(9600);
HelloWorldHelloWorldHelloWorld }
void loop()
{
Serial.print(”Hello World");
delay(1000);
}
Hans-Petter Halvorsen 17
| Programmering with Arduino | 2018.10.16
int myValue = 0;
The Value is: 73
The Value is: 63 void setup()
The Value is: 36 {
The Value is: 77 Serial.begin(9600);
The Value is: 54 }
void loop()
{
myValue = random(100);
Serial.print(”The Value is: ");
Here you see how we can write a value to Serial.println(myValue);
the Serial Monitor. This can be a value delay(1000);
from a sensor, e.g., a temperature sensor. }
Hans-Petter Halvorsen 18
| Programmering with Arduino | 2018.10.16
Arduino Programmering
Hans-Petter Halvorsen
Arduino Programs
All Arduino programs must follow the following main structure:
// Initialization, define variables, etc.
void setup()
{
// Initialization
...
}
void loop()
{
//Main Program
...
}
Hans-Petter Halvorsen 19
| Programmering with Arduino | 2018.10.16
void loop()
{
digitalWrite(11, HIGH); // Turn on the LED
delay(1000); // Wait for one second
digitalWrite(11, LOW); // Turn off the LED
delay(1000); // Wait for one second
}
void loop()
{
digitalWrite(11, HIGH); // Turn on the LED
/*
... This will not be executed by the program because
it is a comment...
*/
}
Hans-Petter Halvorsen 20
| Programmering with Arduino | 2018.10.16
void setup()
{
}
void loop()
{
z = calculate(2,3); Using the Function
}
TRY IT OUT!
Hans-Petter Halvorsen 21
| Programmering with Arduino | 2018.10.16
void setup()
{
Create the following Serial.begin(9600);
program:
Serial.println("Hello, world!");
Open the ”Serial }
Monitor” in order to se void loop()
the output {
void setup()
{
Create the following
Serial.begin(9600);
program: }
Hans-Petter Halvorsen 22
| Programmering with Arduino | 2018.10.16
TRY IT OUT!
Creating Functions
Create a function that calculates the area of a
circle with a given radius.
Area
Hans-Petter Halvorsen 23
| Programmering with Arduino | 2018.10.16
void setup()
{
float area; TRY IT OUT!
Serial.begin(9600);
// calculate the area of a circle with radius of 9.2
float r=9,2;
area = CircleArea(r);
Serial.print("Area of circle is: ");
// print area to 4 decimal places
Solution
Serial.println(area, 4);
}
void loop()
{
return result;
}
Hans-Petter Halvorsen 24
| Programmering with Arduino | 2018.10.16
Arrays
const int arraysize = 100;
TRY IT OUT!
int x;
int sum = 0;
float average = 0;
int myarray[arraysize];
void setup()
{
Serial.begin(9600);
}
void loop()
Here we shall use arrays in the {
sum = 0;
Arduino program
for (int i = 0; i < arraysize; i++)
{
x = random(200);
myarray[i] = x;
}
sum = calculateSum(myarray);
Create this program from average = sum / 100;
Serial.print(" Sum = ");
Arduino Programming
https://www.arduino.cc/en/Reference/HomePage
Hans-Petter Halvorsen 25
| Programmering with Arduino | 2018.10.16
https://www.halvorsen.blog
Arduino Examples
|Hans-Petter Halvorsen |
Hans-Petter Halvorsen 26
| Programmering with Arduino | 2018.10.16
Arduino Examples
On the next pages you will find some Examples/Tasks
that you should do step by step as stated in the text.
Hans-Petter Halvorsen 27
| Programmering with Arduino | 2018.10.16
Breadboard Example
Electrical
Components
Breadboard
Breadboard Wiring
Hans-Petter Halvorsen 28
| Programmering with Arduino | 2018.10.16
Analog Inputs
PWM - Pulse Width Modulation
Hans-Petter Halvorsen 29
| Programmering with Arduino | 2018.10.16
Push Button
Temperature Sensor Motor
Light Sensor
Buzzer
Potentiometer
Sensors Actuators
http://en.wikipedia.org/wiki/Sensor http://en.wikipedia.org/wiki/Actuator
Hans-Petter Halvorsen 30
| Programmering with Arduino | 2018.10.16
use.
Examples
1. Electrical Circuit Example
2. Blinking LED Example
3. Switch Example
4. Potentiometer Example
5. Temperature Example
6. Light Sensor Example
7. Thermistor Example
Hans-Petter Halvorsen 31
| Programmering with Arduino | 2018.10.16
Electrical Circuits
Hans-Petter Halvorsen 32
| Programmering with Arduino | 2018.10.16
Electrical Cuicits
Lets create the following cuircit:
Light
+
Instead of using a Battery we
will use the Arduino board as a Power Supply
Power Supply (5V)
-
Example 1
[Wikipedia]
Hans-Petter Halvorsen 33
| Programmering with Arduino | 2018.10.16
Introduction
GND = Ground
Example 1
Hans-Petter Halvorsen 34
| Programmering with Arduino | 2018.10.16
Example 1
Hans-Petter Halvorsen 35
| Programmering with Arduino | 2018.10.16
Wiring
TRY IT OUT!
Example 1
Hans-Petter Halvorsen 36
| Programmering with Arduino | 2018.10.16
Hans-Petter Halvorsen 37
| Programmering with Arduino | 2018.10.16
Blinking LED
[Wikipedia]
Hans-Petter Halvorsen 38
| Programmering with Arduino | 2018.10.16
Introduction
We will make a program that makes the LED start
blinking.
How-To Do it:
1. Wire the cuircuit and components
2. Make the Arduino program
Example 2
Equipment
• Arduino UNO
• Breadboard
• LED
• Resistor, ! = 270Ω
• Wires (Jumper Wires)
Example 2
Hans-Petter Halvorsen 39
| Programmering with Arduino | 2018.10.16
Example 2
Programming
Program Structure You need to use the following:
A Digital Pin can either
//Globale variable Which Pin (0, 1, 3, ...) are you using? be an INPUT or an
... OUTPUT. Since we shall
use it to turn-on a LED,
void setup() pinMode(pin, mode); ww set it to OUTPUT.
{
//Initialization
Turn-on Turn-off
} digitalWrite(pin, value); LED LED
void loop() A Digital PIn can have 2 values, either HIGH or LOW
{
//Main Program The delay() fuction make a small pause
delay(ms); in milliseconds (ms), e.g,. delay(1000)
}
pause the program for 1 second
Example 2 Arduino Language Reference: https://www.arduino.cc/en/Reference
Hans-Petter Halvorsen 40
| Programmering with Arduino | 2018.10.16
{
pinMode(8, OUTPUT);
}
void loop()
{
Example 2
TRY IT OUT!
int ledPin = 8;
Arduino Program
En ørliten forbedring. Vi
bruker en variabel til å
void setup()
definere pinne-nummeret
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
}
Example 2
Hans-Petter Halvorsen 41
| Programmering with Arduino | 2018.10.16
Hans-Petter Halvorsen 42
| Programmering with Arduino | 2018.10.16
Switch
Wiring
Example 3
Hans-Petter Halvorsen 43
| Programmering with Arduino | 2018.10.16
Equipment
• Arduino
• Breadboard
• LED
• Switch
• Resistor, ! = 270 Ω
• Some Wires
Example 3
Breadboard
Make sure to place the Switch
correctly on the Breadboard!
Example 3
Avoid short circuit
Hans-Petter Halvorsen 44
| Programmering with Arduino | 2018.10.16
TRY IT OUT!
Wiring
! = 270Ω
int buttonState = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
Example 3
Hans-Petter Halvorsen 45
| Programmering with Arduino | 2018.10.16
Hans-Petter Halvorsen 46
| Programmering with Arduino | 2018.10.16
Potentiometer
Potentiometer
A potentiometer is a simple knob that provides a variable
resistance, which we can read into the Arduino board as an
analog value.
Electrical symbol:
GND Analog 5V
Pin
(0-5V)
Example 4
Hans-Petter Halvorsen 47
| Programmering with Arduino | 2018.10.16
Equipment
• Arduino
• Breadboard
• Potentiometer
• LED
• Resistor, ! = 330Ω
• Wires (Jumper Wires)
Example 4
Breadboard
Example 4
Hans-Petter Halvorsen 48
| Programmering with Arduino | 2018.10.16
Dimmer
In this example we will make a simple dimmer using a
potentiometer that control the intensity of the light.
When the voltage in the circuit increases, the intensity of the LED
will increase.
TRY IT OUT!
Hans-Petter Halvorsen 49
| Programmering with Arduino | 2018.10.16
Hans-Petter Halvorsen 50
| Programmering with Arduino | 2018.10.16
Temperature
Introduction
In this example we will use a small temperature
sensor to read the temperature in the room.
Example 5
Hans-Petter Halvorsen 51
| Programmering with Arduino | 2018.10.16
Example 5 https://learn.adafruit.com/tmp36-temperature-sensor
Example 5 [http://no.rs-online.com/webdocs/14cd/0900766b814cd0a1.pdf]
Hans-Petter Halvorsen 52
| Programmering with Arduino | 2018.10.16
Necessary Equipment
• Arduino
• Breadboard
• TMP36
• Wires (Jumper Wires)
Example 5
TRY IT OUT!
Wiring
Example 5
Hans-Petter Halvorsen 53
| Programmering with Arduino | 2018.10.16
analogRead
Example:
analogRead reads the value from a int sensorPin = 0;
specific analog pin. int sensorValue;
Example 5 https://www.arduino.cc/en/Reference/AnalogRead
Temperature coversion
We want to present the value from the sensor in
degrees Celsius:
1. analogRead() gives a value between 0 and
1023
2. Then we convert this value to 0-5V
3. Finally, we convert to degrees Celsius using
information from the Datasheet presented on
the previous page
Example 5
Hans-Petter Halvorsen 54
| Programmering with Arduino | 2018.10.16
void setup()
{
Serial.begin(9600);
}
void loop()
{
adcValue = analogRead(temperaturePin);
Convert from ADC-value (0-
voltage = (adcValue*5)/1023; 1023) to Voltage (0-5V)
degreesC = 100*voltage - 50;
delay(1000);
}
Example 5
Hans-Petter Halvorsen 55
| Programmering with Arduino | 2018.10.16
Light Sensor
Hans-Petter Halvorsen 56
| Programmering with Arduino | 2018.10.16
Introduction
In this example we will use a light sensor to
measure the light intensity of the room.
Example 6
Light Sensor
or
Hans-Petter Halvorsen 57
| Programmering with Arduino | 2018.10.16
Necessary Equipment
Light Sensor
• Arduino or
• Breadboard
• Light Sensor
LED
• LED
• Resistors, ! = 330Ω, ! = 10 'Ω
• Wires (Jumper Wires)
Example 6
TRY IT OUT!
Wiring
! = 10kΩ
or
Example 6
Hans-Petter Halvorsen 58
| Programmering with Arduino | 2018.10.16
int photocellPin = 2;
TRY IT OUT!
Arduino Program
int photocellReading;
void setup(void)
{
Serial.begin(9600);
}
void loop(void)
{
photocellReading = analogRead(photocellPin);
delay(1000);
}
Example 6
! = 330Ω
Wiring
! = 10kΩ
or
Example 6
Hans-Petter Halvorsen 59
| Programmering with Arduino | 2018.10.16
int photocellPin = 0;
int ledPin = 2;
TRY IT OUT!
Arduino Program
int photocellReading;
const float limit = 100;
void setup(void)
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop(void)
{
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading);
delay(1000);
}
Example 6
Hans-Petter Halvorsen 60
| Programmering with Arduino | 2018.10.16
Thermistor
Hans-Petter Halvorsen 61
| Programmering with Arduino | 2018.10.16
Introduction
In this example we will use a small thermistor to
read the temperature in the room.
Example 7
Thermistor
A thermistor is an electronic component that changes
resistance to temperature - so-called Resistance
Temperature Detectors (RTD). It is often used as a
temperature sensor.
Our Thermistor is a so-called NTC (Negative Temperature Coefficient).
In a NTC Thermistor, resistance decreases as the temperature rises.
There is an non-linear relationship between resistance and excitement. To find the
temperature we can use the following equation (Steinhart-Hart equation):
1 [Wikipedia]
= $ + & ln(*) + , ln(*) - where $, &, , are constants given below
"
$ = 0.001129148, & = 0.000234125 789 , = 8.76741< − 08
Example 7
Hans-Petter Halvorsen 62
| Programmering with Arduino | 2018.10.16
Datasheet: https://www.elfadistrelec.no/no/ntc-motstand-kablet-10-kohm-vishay-
ntcle100e3103jb0/p/16026041?q=160-26-041&page=1&origPos=1&origPageSize=50&simi=98.0
Example 7
Equipment
• Arduino
• Breadboard
• Thermistor
• LED
• Resistor 10 kΩ
• Wires (Jumper Wires)
Example 7
Hans-Petter Halvorsen 63
| Programmering with Arduino | 2018.10.16
TRY IT OUT!
Wiring
Example 7
Example 7 [https://en.wikipedia.org/wiki/Voltage_divider]
Hans-Petter Halvorsen 64
| Programmering with Arduino | 2018.10.16
void setup()
{
TRY IT OUT!
Arduino Program
Serial.begin(9600);
}
void loop()
{
int temperature = getTemp();
Serial.print("Temperature Value: ");
Serial.print(temperature);
Serial.println("*C");
delay(1000);
}
double getTemp()
{
// Inputs ADC Value from Thermistor and outputs Temperature in Celsius
Temp = log(Resistance);
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celsius
return Temp; // Return the Temperature
}
Example 7
Hans-Petter Halvorsen 65
| Programmering with Arduino | 2018.10.16
|Slutt på eksemplet|
Hans-Petter Halvorsen 66
| Programmering with Arduino | 2018.10.16
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: hans.p.halvorsen@usn.no
Web: https://www.halvorsen.blog
Hans-Petter Halvorsen 67
|Programming with Arduino|
Hans-Petter Halvorsen
https://www.halvorsen.blog