Programming With Arduino
Programming With Arduino
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
1
| Programmering with Arduino |
Equipment Resistors
Temperature sensor
Switch Arduino
Potentiometer Light sensor USB cable
LEDs
Breadboard wires
Multimeter
Thermistor
- 2
| Programmering with Arduino |
Introduction to Arduino
USB for PC
2
connection
5V, GND 4 5
3
| Programmering with Arduino |
PC
Arduino
4
| Programmering with Arduino |
Switch
Light
Light
+ On/Off
Bryter
Battery
- +
-
Battery
[http://www.fritzing.org]
6
| Programmering with Arduino |
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]
$ #
7
| Programmering with Arduino |
Ohms Law
! = #$
Multimeter
https://learn.sparkfun.com/tutorials/how-to-use-a-multimeter
8
| Programmering with Arduino |
[Wikipedia]
Resistors
Resistance is measured in Ohm (Ω)
- 9
| Programmering with Arduino |
10
| Programmering with Arduino |
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
= + + +⋯
!" !# !$ ! ! !" !# !$
11
| Programmering with Arduino |
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
- +
12
| Programmering with Arduino |
Breadboard
A breadboard is used to wire
electric components together
13
| Programmering with Arduino |
Arduino Software
Open existing Code
Upload Code to Arduino Board
Save
Editor Preferences
15
| Programmering with Arduino |
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);
}
16
| Programmering with Arduino |
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);
}
17
| Programmering with Arduino |
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. }
18
| Programmering with Arduino |
Arduino Programmering
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 |
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 |
void setup()
{
}
void loop()
{
z = calculate(2,3); Using the Function
}
TRY IT OUT!
21
| Programmering with Arduino |
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: }
22
| Programmering with Arduino |
TRY IT OUT!
Creating Functions
Create a function that calculates the area of a
circle with a given radius.
Area
23
| Programmering with Arduino |
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;
}
24
| Programmering with Arduino |
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 = ");
25
| Programmering with Arduino |
Arduino Examples
On the next pages you will find some Examples/Tasks
that you should do step by step as stated in the text.
27
| Programmering with Arduino |
Breadboard Example
Electrical
Components
Breadboard
Breadboard Wiring
28
| Programmering with Arduino |
Analog Inputs
PWM - Pulse Width Modulation
29
| Programmering with Arduino |
Push Button
Temperature Sensor Motor
Light Sensor
Buzzer
Potentiometer
Sensors Actuators
http://en.wikipedia.org/wiki/Sensor http://en.wikipedia.org/wiki/Actuator
30
| Programmering with Arduino |
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
31
| Programmering with Arduino |
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]
33
| Programmering with Arduino |
Introduction
GND = Ground
Example 1
34
| Programmering with Arduino |
Example 1
35
| Programmering with Arduino |
Wiring
TRY IT OUT!
Example 1
36
| Programmering with Arduino |
Blinking LED
[Wikipedia]
38
| Programmering with Arduino |
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
39
| Programmering with Arduino |
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
40
| Programmering with Arduino |
{
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
41
| Programmering with Arduino |
Switch
Wiring
Example 3
43
| Programmering with Arduino |
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
44
| Programmering with Arduino |
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
45
| Programmering with Arduino |
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
47
| Programmering with Arduino |
Equipment
• Arduino
• Breadboard
• Potentiometer
• LED
• Resistor, ! = 330Ω
• Wires (Jumper Wires)
Example 4
Breadboard
Example 4
48
| Programmering with Arduino |
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!
49
| Programmering with Arduino |
Temperature
Introduction
In this example we will use a small temperature
sensor to read the temperature in the room.
Example 5
51
| Programmering with Arduino |
Example 5 https://learn.adafruit.com/tmp36-temperature-sensor
Example 5 [http://no.rs-online.com/webdocs/14cd/0900766b814cd0a1.pdf]
52
| Programmering with Arduino |
Necessary Equipment
• Arduino
• Breadboard
• TMP36
• Wires (Jumper Wires)
Example 5
TRY IT OUT!
Wiring
Example 5
53
| Programmering with Arduino |
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
54
| Programmering with Arduino |
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
Light Sensor
55
| Programmering with Arduino |
Introduction
In this example we will use a light sensor to
measure the light intensity of the room.
Example 6
Light Sensor
or
57
| Programmering with Arduino |
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
58
| Programmering with Arduino |
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
59
| Programmering with Arduino |
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
Thermistor
60
| Programmering with Arduino |
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
62
| Programmering with Arduino |
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
63
| Programmering with Arduino |
TRY IT OUT!
Wiring
Example 7
Example 7 [https://en.wikipedia.org/wiki/Voltage_divider]
64
| Programmering with Arduino |
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
65