Arduino Tutorials en
Arduino Tutorials en
Service-Team
This version of our tutorials in english language is a new one (april 2016). Please contact us in
case you notice any mistakes: info@funduino.de
Have fun with our tutorials! Kind regards,
Service Team
Contents
Preface...................................................................................................................................2
1. Preface to the Arduino tutorials.....................................................................................2
2. Hardware and Software.....................................................................................................3
2.1 Hardware.....................................................................................................................3
2.1.2 Description of typical equipment..........................................................................4
2.1.2.1 Breadboard........................................................................................................4
2.1.2.2 LED (light emitting diode)..................................................................................4
2.2 Software.......................................................................................................................5
2.2.1 Installation............................................................................................................5
2.2.1.1 Installation and set up of the Arduino software.................................................5
2.2.1.2 Installation of the USB driver.............................................................................7
3. Programming......................................................................................................................8
Basic structure of a sketch:................................................................................................9
1. Name variable...........................................................................................................9
2. Setup (absolutely necessary for the program)..........................................................9
Tutorials:...............................................................................................................................11
Blinking LED.....................................................................................................................11
Alternately blinking LED...................................................................................................15
Fading LED......................................................................................................................17
Light and sound...............................................................................................................20
Push button and LED.......................................................................................................22
RGB LED.........................................................................................................................25
Motion detector................................................................................................................33
Photo resistor...................................................................................................................37
Potentiometer...................................................................................................................41
Temperature measurement..............................................................................................44
Measurement of distance................................................................................................49
Usage of an infrared remote............................................................................................57
Control a servo.................................................................................................................62
LCD Display.....................................................................................................................63
Relay shield.....................................................................................................................67
Stepper............................................................................................................................69
Moisture sensor...............................................................................................................72
Drop sensor.....................................................................................................................76
RFID Kit...........................................................................................................................80
Tutorials with additional Equipment.....................................................................................90
Keypad shield..................................................................................................................90
IC Display.......................................................................................................................94
Preface
1. Preface to the Arduino tutorials
These tutorials are meant to be an entry to the Arduino basis. Beginners should get an
interesting lead-in the world of Arduino. Our tutorials are all based on practical tasks with
theoretical introductions at the beginning. We really recommend to read the theoretical part
to successfully complete the practical tasks.
These tutorials were created in the context of a teaching unit. They can be used for free to
learn about Arduino, but it's not allowed to copy and use the tutorials without any permission.
These tutorials have been created carefully and are continuously maintained, however we can't
give any warranty about the accuracy and completeness of the tutorials.
For the practical tasks you'll need some technical equipment. On our website
www.funduinoshop.com you can buy especially customized Funduino kits for our tutorials.
The term Arduino ist mostly used for both components. The hardware (Arduino Boards)
and the corresponding software (Arduino).
2.1 Hardware
Beside sensors and actuators you need, as a base for quick and flexible experimental
setups, jumper cable combined with a breadboard. This way you won't need to solder.
Furthermore the LEDs are useful to check the signal output of the board.
2.1.2.1 Breadboard
A Breadboard is a helpful tool to build circuits without any soldering. Certain contacts are
connected with each other. Therefore it is possible to connect many cables with each other
without soldering or screwing them together.
This image below shows in color, which contacts are connected.
With LEDs it is possible to check the results of projects real quick. Because of that they're
useful for almost every Arduino project. On the internet are many information about LEDs.
The most important information:
The electricity can only get through the LED in one direction. So the LED has to be
connected the right way to work. There is a longer and a shorter contact at the LED.
The longer one is the positive (+) and the shorter one is the negative () contact.
The LED is only designed for a specific voltage. If there isn't enough voltage the LED
won't shine as bright as it should. If there's to much voltage for the LED, it will get really
hot (ATTENTION) and burn out.
Typical voltage data for the different colors of LEDs: blue: 3,1V, white: 3,3V, green: 3,7V,
yellow: 2,2V, red: 2,1V. The voltage on the microcontrollerboards is 5V. So the LED
shouldn't be connected to the board directly, but with a resistor between it in the circuit.
Non-committal recommendation for resistors at different LEDs (while connecting to the
5V pins on the microcontroller boards):
LED:
white
red
yellow
green
blue
IR
resistor:
100 Ohm
200 Ohm
200 Ohm
100 Ohm
100 Ohm
100 Ohm
2.2 Software
The software that is used to program the microcontroller, is open-source-software and can
be downloaded for free on www.arduino.cc. With this Arduino software you can write little
programs witch the microcontroller should perform. This programs are called Sketch.
In the end the sketches are transferred to the microcontroller by USB cable.
More on that later on the subject programing.
2.2.1 Installation
Now one after another the Arduino software and the USB driver for the board have to be
installed.
1. Download the Arduino software on www.arduino.cc and install it on the computer (The
microcontroller NOT connected to the PC). After that you open the software file and start
6
b) You have to choose the right Serial-Port, to let the Computer know to which port the
board has been connected. That is only possible if the USB driver has been installed
correctly. It can be checked this way:
At the moment the Arduino isn't connected to the PC. If you now choose Port, under the
field Tool, you will already see one or more ports here (COM1/ COM2/ COM3).
The quantity of the shown ports doesn't depend on the quantity of the USB ports on the
computer. When the board gets connected to the computer, YOU WILL FIND ONE MORE
PORT.
yellow exclamation mark. In this case: Click on the unknown device and choose update
USB driver. Now you can start over with the manual installation.
3. Programming
Now we can start properly. Without to much theoretical information we start directly with
programming. Learning by doing. On the left side you can find the sketches, on the right
the accompanying explanation for the commands in grey. If you work trough the tutorials
with this system, you will soon understand the code and be able to use it by yourself. Later
on you can familiarize yourself with other features. These tutorials are only meant as first
steps to the Arduino world. All possible program features and codes are referred on
www.arduino.cc under reference.
First of all a short explanation for possible error reports that can appear while working with
the Arduino software. The two most common ones are:
1) The board is not installed right or the
wrong board is selected. After uploading
the sketch, there will appear an error report
underneath the sketch. It looks like the
one in the picture on the right. The note
not in sync shows up in the error report.
1. Name variable
In the first part elements of the program are named ( This will be explained in program no.
3). This part is not absolutely necessary.
The setup will be performed only once. Here you are telling the program for example what
Pin (slot for cables) should be an input and what should be an output on the boards.
Defined as Output: The pin should put out a voltage. For example: With this pin a LED is
meant to light up.
10
Defined as an Input: The board should read out a voltage. For example: A switch is
actuated. The board recognized this, because it gets a voltage on the Input pin.
11
Tutorials:
Blinking LED
Circuit:
The meant LED is circled in red on the image above.
You only have to connect the board properly with the computer.
void setup()
Now we are going to write the setup information between the curly brackets.
In this case: pin 13 is supposed to be an output :
pinMode(13, OUTPUT);
}
13
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
14
1.4 Now you have the option to modify the program. Example: You want the LED to
blink really fast. Therefore we will shorten the waiting time ( From 1000ms to 200ms)
void setup()
{
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
The new modified sketch has to be uploaded to the board again. Now if everything has
worked properly the LED should blink faster.
15
16
Code:
void setup()
{
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}
void loop()
{
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
}
//Here at the end of the loop the program starts again from the beginning of
17
Fading LED
The Arduino is a digital microcontroller. It only knows 5 Volt on and 5 Volt off on its
outputs. But to vary the brightness of an LED, it ought be vary the voltage on the outputs.
For example 5 Volts if the LED shines bright, 4 Volts if it shines a bit darker and so on.
THIS DOESN'T WORK ON DIGITAL PINS. But there is an other option. It is called pulse
width modulation (short PWM). PWM lets the 5 V voltage fade. The voltage is turned on
and off for milliseconds. With a really high PWM the 5 V signal nearly gets constant on the
pin. With a low PWM it is the other way around and the 5 V signal is barely there (This is
only a reduced summary, so you should look it up on the internet, if you need more
information). With this PWM it is possible to get nearly the same effect on a LED, as if the
18
voltage would get varied. Not every pin on the board has the PWM function. The pins with
this function are specially labelled, for example with a little wave in front of the pin number
(see image on the bottom of page 22).
Let's go!
Code:
int LED=9;
int brightness=0;
void setup()
{
pinMode(LED, OUTPUT);
}
void loop()
{
analogWrite(LED, brightness);
// the PWM output on the pin with the LED. The value of the PWM is saved under
//the word brightness. In this case it is 0.
brightness=brightness + fading; // Now we modify the value of the PWM output. We
//add the value of the fading to the value of the brightness. In this case:
//brightness = 0+5. The new value that is standing for brightness isn't 0 any
// longer but 5. When the program has ran through the loop part once it will
//start over again. The next pass the value will be 10. After that it will be
// 15... etc.
delay(25); //The LED should only stay bright for a really short time like 25
//milliseconds. If you reduce that time the fading will also get faster.
19
20
Task: A LED and a piezo speaker are supposed to blink or beep continuously.
Required equipment: Microcontroller / one LED / resistor with 200 Ohm / Breadboard /
piezo speaker / cables
Setup:
21
Code:
int LED=4;
//this time we also going to use the first part of the program. Here
//we are going to put in variables. This means that there will be a letter or a
//word standing for a number. In this example the LED is connected to pin 4 and
//the speaker to pin 5, so we rename pin 4 and pin 5, to avoid confusion. The
//word LED now stands for the number 4 and the word beep for the number 5.
int beep=5;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(beep, OUTPUT);
is supposed to be an output
}
void loop()
{
digitalWrite(LED, HIGH);
digitalWrite(beep, HIGH);
delay(1000);
digitalWrite(LED, LOW);
digitalWrite(beep, LOW);
delay(1000);
}
//Here at the end of the loop the program starts again from the beginning of
//the loop. So it will beep and light up again. If you change the break (delay)
//it will be either beep and light up faster or slower.
22
Task: After pushing the button an LED is supposed to light up for 5 seconds.
Required equipment: Arduino / one LED (blue) / one resistor with 100Ohm / one resistor
with 1K Ohm (1000 Ohm) / Breadboard / Cables / Push button
The digital pins of the microcontroller are not only able to put out voltage, they are also
able to read out voltage. We are going to try this with the following program. This time
there is something special in the setup. If we would simply connect the push button with
the microcontroller and push the button, there would be voltage on the pin. You can
imagine it like many electrons floating around the pin. When you now release the button,
there wouldn't get any more electrons to the pin. Now the difficulty: The electrons that are
already floating around the pin are only escaping extremely slow. So the microcontroller
thinks that the button has been pushed longer than it actually has been. The
microcontroller thinks that the button has been pushed until the electrons have escaped
completely from the pin. This problem can be fixed by grounding the pin with a 1000 Ohm
(1K Ohm) resistor. Now the electrons are able to escape from the pin faster and the
microcontroller recognizes that the button only has been pushed briefly. The resistor is
called PULLDOWN- resistor, because the resistors is always pulling down the voltage
to 0V. ATTENTION: If you are using a smaller valued resistor, you can get an electrical
short on the microcontroller while pushing the button.
23
Setup:
Code:
int LEDblue=6;
int button=7;
int buttonstatus=0;
pinMode(LEDblue, OUTPUT);
pinMode(button, INPUT);
24
}
void loop()
{
buttonstatus=digitalRead(button);
digitalWrite(LEDblue, HIGH);
delay(5000);
digitalWrite(LEDblue, LOW);
}
else
{
digitalWrite(LEDblue, LOW);
25
RGB LED
Duration of Output
Sensibility
What is an RGB LED? The RGB LED is an LED that is able to shine in different colours.
RGB stands for the three colours red, green and blue. Inside the RGB LED are three
separate LEDs available, which can be turned on and off individually and shine in three
different colours. That is reason behind the four contacts of the RGB LED. The longest one
can be (depending on the version) the anode(+) or the cathode(-). The other contacts
belong to the different colours.
Version a; Common cathode The longest contact of the RGB LED is -. In this case the
other three contacts are supposed to get positive gate voltage (5V) +.
26
Version b: Common anode The longest contact of the RGB LED is +. This means that
the other contacts are supposed to get negative gate Voltage (GND) -.
It is possible to create much more colours if you temper the colours. For example; you will
get yellow by tempering blue and green.
There is a simple way to find out what RGB LED version you have. Just switch + and -
on the LED. Only if the LED is connected the right way it will work and shine.
27
28
The Arduino is a digital Microcontroller. On its Outputs it only can turn on 5V or turn off
5V. But to create the different colours, the three colours of the LED have to be actuate
more specific. That is the reason to use the Pulse Width Modulation. The PWM can be
used on the pins with the little wave in front of the number.
The PWM lets the voltage pulse from +5V to 0V. So the voltage gets turned off and on for
only milliseconds. With a really high PWM the 5 V signal nearly gets constant on the pin.
With a low PWM it is the other way around and the 5 V signal is barely there (This is only a
reduced summary, so you should look it up on the internet, if you need more information).
With PWM it is possible to get nearly the same effect as if the voltage would vary.
The following codes are working for both RGB versions:
Sketch 1:
With this code you can turn on and off the three different colours one by one.
int LEDblue=3;
int LEDred=5;
//Red colour on 5
int LEDgreen=6;
int b=1000;
29
int brightnessblue=150;
void setup()
{
pinMode(LEDblue, OUTPUT);
pinMode(LEDgreen, OUTPUT);
pinMode(LEDred, OUTPUT);
}
void loop()
{
analogWrite(LEDblue, brightnessblue);
delay(b);
//Break
analogWrite(LEDblue, dark);
analogWrite(LEDred, brightnessred);
delay(b);
//Turn on blue
//Turn on red
//Break
30
analogWrite(LEDgreen, brightnessgreen);
delay(b);
//Turn on green
//Break
analogWrite(LEDgreen, dark);
//Turn off
green
Sketch 2:
With this code always two different colours will be turned on and off together. This way we
are able to create the colours yellow, turquoise and purple.
int LEDblue=3;
int LEDred=5;
//Red colour on 5
int LEDgreen=6;
int b=1000;
int brightnessblue=150;
void setup()
{
31
pinMode(LEDblue, OUTPUT);
pinMode(LEDgreen, OUTPUT);
pinMode(LEDred, OUTPUT);
}
void loop()
{
analogWrite(LEDgreen, brightnessgreen);
analogWrite(LEDred, brightnessred);
delay(b);
analogWrite(LEDgreen, dark);
analogWrite(LEDred, dark);
analogWrite(LEDgreen, brightnessgreen);
analogWrite(LEDblue, brightnessblue);
delay(b);
analogWrite(LEDgreen, dark);
analogWrite(LEDblue, dark);
analogWrite(LEDred, brightnessred);
analogWrite(LEDblue, brightnessblue);
32
delay(b);
analogWrite(LEDred, dark);
analogWrite(LEDblue, dark);
}
33
Motion detector
34
35
Setup:
Code:
int piezo=5;
int movement=7;
int movementstatus=0;
pinMode(piezo, OUTPUT);
//defined as an output.
36
pinMode(movement, INPUT);
//defined as an input.
}
void loop()
{
movementstatus=digitalRead(movement);
digitalWrite(piezo,HIGH);
delay(5000);
digitalWrite(piezo, LOW);
}
else
{
//open else-command
37
Photo resistor
38
Setup:
39
Sketch:
int input=A0;
int LED=10;
int sensorvalue=0;
void setup()
{
Serial.begin(9600);
//need this to get the read out value of the photo resistor on the serial
//monitor.
pinMode (LED,OUTPUT);
40
sensorvalue=analogRead(input);
{
digitalWrite(LED,HIGH);
}
else
{
digitalWrite(LED,LOW);
}
delay(50);
}
//If the sensor value for example at normal brightness only reaches 100 (this
//value depends on the used resistor, the brightness and the current
//direction), it would make sense to use a lower value than 512 (for example
//90), to turn on the LED. You can look up the current sensor value on the
//serial monitor. You //can find it in the Arduino software at tools.
41
42
Potentiometer
43
Sketch:
int input=A0;
int LED=13;
int sensorvalue=0;
void setup()
{
sensorvalue= analogRead(input);
//out and gets saved as a number between 0 and 1023 under sensorvalue
digitalWrite (LED,HIGH);
delay(sensorvalue);
//Turn on LED
//Now the loop part will be restarted. If the read out value of the
//potentiometer has changed, the time between the on and off stage will also
//change. The LED will be blinking faster or slower. The longest possible delay
//in this sketch can be 1023ms (milliseconds) long. If needed longer delays are
//also possible. Therefore you would have to add a litte mathimatical function
44
45
Temperature measurement
Task: Read out the temperature with theTMP36 sensor and display the temperature on the
serial monitor.
Required equipment: Arduino / Breadboard / cables / temperature sensor TMP36 / external
power-supply
The sensor has three terminals. 5V, GND, and the pin for the temperature signal. On this
pin, the sensor puts out a voltage between 0 and 2.0 volts.
0V = -50 C and 2.0V = 150 C.
The sensor is supposed to be quite precise (2C) between -40C and 150C, according
to the manufacturing. The voltage on this pin must be read out by the microcontroller
board and then has to be converted into a temperature value.
- CAUTION: If the sensor is connected incorrectly it gets destroyed.
- Use a external power supply for more sensor accuracy (as possible 9V battery or 9V
power supply).
46
Code:
int TMP36 = A0;
//To get good values we have to read out some values to get the
//mean value out of them. The square brackets [10] create ten different
//variables at one time:temp[0], temp[2], temp[3], till temp[9].So
//with this spelling [10] we will save some space.
int time= 20;
//measurement.
void setup() {
Serial.begin(9600);
//main part
47
//temperature gets read out ten times. In between there is always a little
//break. We will take a closer look on the used command:
//temp[1]=map(analogRead(TMP36),0,410,-50,150);
//temp[1] The name of the first variable.
//map(a,b,c,d,e) - This is a Map command. With this command it is possible
//to change a read out value (a) from one region between (b) and (c) into a
//region between (d) and (e).
//In our case this means: The sensor value gets read out right in the map
//command analogRead(TMP36). The value should be between 0 and 410 (= values
//between 0V and 2V at the analog port). The sensor outputs this voltage values
//if it measures temperature between -50C and 150C. With the map command
//these values get converted into degree values between -50C and 150C.
temperature=(temp[0]+temp[1]+temp[2]+temp[3]+temp[4]+temp[5]+temp[6]+temp[7]+tem
p[8]+temp[9])/10; //Everything in one row! In this line, all ten values get
//summarized and get divided by ten. This average value gets saved under
//temperature.
48
Serial.print(temperature);
void setup() {
Serial.begin(9600);
pinMode (piezo, OUTPUT);
//Pin 5 is an output
}
void loop() {
temp[0] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[1] = map(analogRead(TMP36), 0, 410, -50, 150);
49
//...
temp[9] = map(analogRead(TMP36), 0, 410, -50, 150);
temperature=(temp[0]+temp[1]+temp[2]+temp[3]+temp[4]+temp[5]+temp[6]+temp[7]+tem
p[8]+temp[9])/10; // Everything in one row!
Serial.print(temperature);
Serial.println("degrees");
if (temperature>=30)
{
digitalWrite(piezo,HIGH);
}
else
{
digitalWrite(piezo,LOW);
//...it is quiet.
}
}
50
Measurement of distance
Task: Measure a distance with the HC-SR04 ultrasonic sensor and show the result on the
serial monitor.
Required equipment: microcontroller board / cables / Breadboard / HC-SR04 ultrasonic
sensor
How does the ultrasonic sensor work?
The sensor has four pins.
a) 5V (+)
b) GND (-)
c) echo
d) trigger
The contacts 5V and GND are meant for the power supply. Trough the "trigger" pin the
sensor gets a short signal (5V) from the microcontroller board to create a sound wave. As
soon as the sound wave hits a wall or other objects, it will be reflected and comes back to
the ultrasonic sensor. When the sensor detects this returned sound wave, the sensor will
send a signal to the Arduino microcontroller trough the "echo" pin. The Arduino-board
measures the time between the transmission and the return of the sound wave, and
converts this time into a distance.
Setup:
51
Code:
int trigger=7;
int echo=6;
//trigger on pin 7.
//echo on pin 6.
long time=0;
//The value time will safe the time between transmission and
//start with 0. Instead of int we are using long for this value, to save a
//bigger number.
void setup()
{
Serial.begin (9600);
//data from the arduino board to the computer to show it on the serial monitor.
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
}
void loop()
{
digitalWrite(trigger, LOW);
//clear signal.
delay(5);
//.for 5 milliseconds.
digitalWrite(trigger, HIGH);
delay(10);
//..for 10 milliseconds.
digitalWrite(trigger, LOW);
52
//front of the n) the arduino board measures the time between sending and
//receiving the soundwave.
dist = (time/2) / 29.1;
//the distance in centimeter. (The sound needs 29,1 seconds for one centimeter.
//The time gets divided with two, because we only want to get one distance and
//not the two ways that the soundwave has to take).
if (dist >= 500 || dist <= 0)
//measurement
}
else
//otherwise
{
Serial.print(dist);
Serial.println("cm");
}
delay(1000);
53
int trigger=12;
int echo=13;
long time=0;
long dist=0;
int piezo=5;
void setup()
{
Serial.begin (9600);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(piezo, OUTPUT);
}
void loop()
{
digitalWrite(trigger, LOW);
delay(5);
digitalWrite(trigger, HIGH);
delay(10);
digitalWrite(trigger, LOW);
time = pulseIn(echo, HIGH);
dist = (time/2) / 29.1;
54
Serial.print(dist);
Serial.println("cm");
}
if (dist <= 80)
{
digitalWrite(piezo,HIGH);
}
else
{
digitalWrite(piezo,LOW);
}
delay(1000);
}
55
int trigger=7;
int echo=6;
long time=0;
int LED=12;
long dist=0;
void setup()
{
Serial.begin (9600);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(12, OUTPUT);
}
void loop()
{
digitalWrite(trigger, LOW);
delay(5);
56
digitalWrite(trigger, HIGH);
delay(10);
digitalWrite(trigger, LOW);
time = pulseIn(echo, HIGH);
dist = (time/2) / 29.1;
if (dist >= 500 || dist <= 0)
{
Serial.println("No measurement");
}
else
{
Serial.print(dist);
Serial.println("cm");
}
if (dist <= 40)
{
digitalWrite(LED, HIGH);
delay(dist*3);
digitalWrite(LED, LOW);
delay(dist*3);
}
57
58
59
The sketch is a variation of the sketch IRrecvDemo, and can be downloaded together
with the very important IR-Remote-Library on the following website:
https://github.com/shirriff/Arduino-IRremote
Download the zip-package and copy the files into your libraries directory in the arduino
software. Rename the downloaded directory to "Irremote".
Now you can open the sketch in the sample-files in the arduino-software:
File -> Examples -> IRremote -> IrrecvDemo
Code:
/*
* IRremote: IRrecvDemo - demonstrates
receiving IR codes with IRrecv
* An IR detector/demodulator must be
connected to the input RECV_PIN.
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
60
* http://arcfn.com
*/
#include <Irremote.h>
us a lot of work, with reading out the code of the infrared light.
int RECV_PIN = 11;
//The contact which outputs the data gets connected with pin
11.
IRrecv irrecv(RECV_PIN);
decode_results results;
gets saved
under results.
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (irrecv.decode(&results)) {
61
}
}
Pressing the "1" button on the infrared remote control causes (in this case) the serialmonitor to write the number "16724175". This is the decrypted number code behind this
button.
If you hold the button permanently pressed, the number "4294967295" appears. This is the
code that indicates that a button is pressed continuously. This number does not depend on
which button is pressed.
There can also appear other numbers if a key is pressed only very short or pulsating. In
that case the sensor may not read a unique value.
Extension of the sketch: Switch on a LED by pressing button1 and switch it off with
button2.
#include <Irremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
pinMode (13, OUTPUT);
62
digitalWrite(13, LOW);
irrecv.enableIRIn();
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);
if (results.value == 16724175)
if (results.value == 16718055)
63
Control a servo
Task: A servo has to turn to three different positions. Between the movements there should
be a short break.
Required equipment: Arduino / one servo / three cables
Code:
#include <Servo.h>
Servo servoblue;
void setup()
{
servoblue.attach(8);
}
void loop()
{
64
servoblue.write(0);
delay(3000);
//Wait 3 seconds
servoblue.write(90);
delay(3000);
//Wait 3 seconds
servoblue.write(20);
delay(3000);
//Wait 3 seconds
servoblue.write(180);
delay(3000);
//Wait 3 seconds
LCD Display
65
As you may see on the image, the wiring for this project is not that easy. It is because the
LCD has to be connected with pretty many cables. Another difficulty is that there is no plug
strip already on the LCD. You can either solder a plug strip to the LCD or solder the cable
directly to the contacts of the LCD. If you want to solder the cables directly with the LCD
we recommend to use flat cables (e.g. from an old hard drive or an old CD drive). Without
any soldering it is quite impossible to get good results.
The potentiometer is needed to adjust the contrast. The back light of the LCD is powered
with 5V. Because it is difficult to show the lettering on the LCD, in the image underneath
you should try to count the contacts of the LCD. (Example: The first contact from the right
to the left gets connected to GND. The second contact from the right to the left gets
connected to 5V .). Information: It is easier to use an LCD Keypad shield or an I2C LCD
for more complicated projects because you don't have to take so much time for the wiring
of the LCD. But these two other options are more expensive than the simple LCD module.
Setup:
66
Now if the LCD is successfully connected we are ready to start with the programming.
Like many other components the LCD also needs to revert to a library in the code. This
library is already a component of the arduino software. So you don't have to install
something on your own.
Code:
#include <LiquidCrystal.h>
//the microcontroller get connected to the LCD (Better don't change yet).
void setup() {
lcd.begin(16, 2);
//In the setup we indicate how many signs and how many rows
lcd.setCursor(0, 0);
Extension of the sketch: We want to print alternately text on the first and on the second
row. For this example we will use the words up and down.
67
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("up");
delay (2000);
//Wait 2 seconds
lcd.clear();
lcd.setCursor(5, 1);
lcd.print("down");
delay (2000);
//Wait 2 seconds
lcd.clear();
The LCD module is especially useful to show sensor values or other outputs from the
microcontroller. You can also find help in the arduino software at the example sketches.
There you can find many different examples at LiquidCrystal.
68
Relay shield
The relay needs a permanent power supply trough 5V+ and (In the picture VCC and
GND). With the IN-pin, the switch can be activated by the Arduino board. The three
terminals on the left side are meant to be connected with the electrical component that
needs more power than the Arduino can provide. If the relay doesn't get any signal from
the Arduino on the IN contact, the contacts A and B are connected. Once the relay gets a
signal from the Arduino the contacts B and C get connected.
ATTENTION: There are two types of relay shields. They differ in the way you get them to
switch. Either you have to connect the IN pin to 5V+ on the Arduino, or you have to
connect the IN pin to GND on the Arduino. You can easily find out which type of relay you
got. Try connecting the signal pin with 5V+ or with GND. The version where the relay
switches (you can hear a loud crack) will be the right version.
For testing purpose, you can use the blink-sketch. Instead of the LED, you connect the
output-pin from the Arduino board with the IN pin from the relay. With that sketch, the
relays will switch on and off in a 1 second rhythm.
69
Code:
void setup()
{
pinMode(6, OUTPUT);
}
void loop()
{
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
delay(1000);
//...wait a second
70
Stepper
71
Wiring
PIN2 on Arduino board to IN4 on control board !!! 2 with 4 !!!
PIN3 on Arduino board to IN2 on control board !!! 3 with 2 !!!
PIN4 on Arduino board to IN3 on control board !!! 4 with 3 !!!
PIN5 on Arduino board to IN1 on control board !!! 5 with 1 !!!
PIN GND on Arduino board to GND on control board
PIN 5V on Arduino board to VCC on control board
72
The following Code is an example and lets the stepper do one rotation (2048 steps) back
and forth.
#include <Stepper.h>
software)
int SPMU = 32;
Stepper myStepper(SPMU, 2,3,4,5);
void setup()
{
myStepper.setSpeed(500);
}
void loop() {
myStepper.step(2048);
delay(500);
myStepper.step(-2048);
delay(500);
}
73
Moisture sensor
Task: We want to measure moisture and display the read out value on the serial monitor.
Required equipment: Arduino / moisture sensor / cables
As the name suggests, the moisture sensor is able to measure moisture. That means that
it can measure the directly adjacent moisture, like Skin moisture or ground humidity, but
not the air moisture. One example of use can be the moisture sensor used to measure the
ground humidity of a plant. If the ground of the plant gets to dry, an alarm could set off or
an automatic water pump system could water the plant. The moisture sensor is also
suitable for measuring a water level, in the region of the sensor.
The way of functioning is quite simple. There is a voltage on the two contacts of the
moisture sensor. The higher the level of moisture gets between the contacts, the better the
currant can flow from one contact to the other. This value gets electronically processed
from the moisture sensor and gets transmitted as an analog signal to an analog input of
the board. Since the board, as described in previous tutorials, isn't able to measure
electrical voltage as such, it converts the analog signal into a numerical value. 0V to 12V
74
corresponds to a numerical value from 0 to 1023 (That are 1024 numbers, since the zero
is counted as the first numerical value).
But the upper level of the moisture sensor is around 800, if the sensor gets completely
under water. The accurate calibration depends on the sensor and the type of liquid/
moisture that is measured (e.g. salt water has a better conductivity so the value would be
higher).
Setup:
The programming of the moisture sensor isn't that complicated and is very similar to the
programming of the potentiometer, because there is just an analog value read out.
int measurement=0;
void setup()
{
75
Serial.begin(9600);
measurement=analogRead(A0);
//beep stands for the pin 6, which gets connected with the piezo
void setup()
{
Serial.begin(9600);
pinMode (6,OUTPUT);
76
void loop()
{
measurement=analogRead(A0);
Serial.print("Moisture measurement:");
Serial.println(measurement);
delay(500);
if (measurement <200)
//200...
{
digitalWrite(beep, HIGH);
}
else
//..if not...
{
digitalWrite(beep, LOW);
}
}
77
Drop sensor
Task: We want to detect a drop of water and display the read out value on the serial
monitor.
Required equipment: Arduino / drop sensor / cables / (piezo and breadboard for the
extension of the sketch)
The drop sensor, or liquid sensor, is able to detect liquid on its surface. A little drop would
already be enough to get a clear measurement.
One example of the usage can be a rain detector. If the drop sensor measures a rain drop,
the Arduino board could e.g. close the blinds, set off an alarm or turn on windshield wipers.
The way of functioning again is quite simple. On the long contacts that are running trough
the surface of the drop sensor a voltage is applied (+ or -). As soon as a liquid e.g. in form
of a drop touches the surface of the sensor, a small current flows from one contact to
another. The sensor converts this value into an analog signal and transfers it to the
microcontroller. The microcontroller, as mentioned in other tutorials, isn't able to read out a
voltage and has to convert the analog signal into a numerical value. 0V to 12V
corresponds to a numerical value from 0 to 1023 (That are 1024 numbers, since the zero
is counted as the first numerical value).
If the drop sensor is completely dry the value would be 0. Whenever a drop of water
touches the contacts of the sensor the value would be at about 480. The more drops
78
are touching the surface of the sensor, the higher the value would get.
Setup:
See image on page 58 (The two cables on the top (black and red) and the piezo speaker
on the breadboard will be needed later on).
The programming of the moisture sensor isn't that complicated and is very similar to the
programming of the potentiometer or the moisture sensor, because there is just an analog
value read out.
int measurement=0;
void setup()
{
Serial.begin(9600);
measurement=analogRead(A0);
79
Code:
int measurement=0;
int beep=6;
//beep stands for the pin 6, which gets connected with the piezo
void setup()
{
Serial.begin(9600);
pinMode (6,OUTPUT);
80
}
void loop()
{
measurement=analogRead(A0);
Serial.print("Moisture measurement:");
Serial.println(measurement);
delay(500);
if (measurement >400)
//than 400...
{
digitalWrite(beep, HIGH);
}
else
//..if not...
{
digitalWrite(beep, LOW);
}
}
81
RFID Kit
Task: Read out the UID of a RFID tag and display the UID as one contiguous decimal
number on the serial monitor.
Required equipment: Arduino / RFID Kit / cables
The RFID (radio frequency identification) reader is used to read out a certain code, which
is send from a RFID transmitter (also called RFID tag) by radio. Each RFID tag has only
one unique code. The RFID Kit is useful to realize projects like for example a locking
mechanism or other similar projects in which a person should be identified with a tag.
RFID tags may come in different shapes, like a key chain or a card in credit card format.
On the following image you can see on the left side two RFID tags, on the right the RFID
receiver RFID RC522 and pin header which have to be soldered to the receiver (There are
also versions with already soldered pin headers on the receiver).
82
How does it work? A RFID receiver contains a small copper coil that generates a magnetic
field. An RFID transmitter also includes a copper coil that picks up the magnetic field and
generates an electrical voltage insinde the transmitter. This voltage is used by a small
electronic chip to get it to emit an electrical code by radio. The transmitter directly receives
this code and processes it, so that the microcontroller is able to process the received code.
It is also possible to codify a RFID tag. Due to the complexity it is not mentioned in this
tutorial. But you can find several other tutorials for this on the web.
Read out and process the data of RFID tags with the Arduino
Required equipment: Arduino UNO or MEGA, RFID reader, at least one RFID tag,
breadboard, cables, one LED, one 200 Ohm resistor
Using a Arduino microcontroller, we want to read out a RFID tag. If it is the right tag the
LED should light up for 5 seconds.
Wiring of the RFID reader with the Arduino board:
On the image underneath the RFID reader has soldered pins bent by 90 on it (like the
already soldered version). This way it is possible to plug the reader vertical on the
breadboard.
83
84
Programming
Reading out and processing the data of an RFID receiver would require,as well as other
complex tasks, a lot lines of code. Therefore we are going to use one of many existing
libraries from the internet. The one we used in this tutorial can be found on
https://github.com/miguelbalboa/rfid.To work with the library you have to download it and
save it in the Arduino program folder. You just have to click on Download ZIP and save
the unziped data on your hard drive.
You have to unpack the folder into the Arduino software folder and save it under libraries.
Usually, the folder has been saved under "C: Programmearduinolibraries" (If you have
saved it somewhere else you have to use the libraries folder there).
After the data is unzipped and saved correctly, there should appear a data with the name
rfid-master in the libraries folder. Now you MUST delete the hyphen from the name.
So you rename the data into rfidmaster. If you have followed this steps correctly, the
library is ready to be used in the Arduino software.
Sketch 1
First of all we are going to read out the UID (Unique Identification Number). It is the
individual sign of every RFID tag. We are going to use the sketch underneath (Attention,
the sketch only works if the library has been added to the software the way it is explained
before). This program is only destined for UNO R3 microcontroller. If you want to use
MEGA2560 or other controllers, you have to adjust the pins.
85
86
If everything has worked it should look like this on the serial monitor (except of your own
UID):
87
It is not very easy to work with these HEX numbers one after another. So we change the
line " Serial.print ( mfrc522.uid.uidByte [i] , HEX ) ; " to " Serial.print ( mfrc522.uid.uidByte
[i] , DEC ; " . Then you will get the individual parts of the UID as a decimal number.
Sketch 2
Now the UID code is shown as a decimal number, but it is still divided into four blocks. We
are going to change the code in a mathematical way to effect that the UID is shown as a
single contiguous number (decimal number).
Why are we doing this? If we want to use this sketch later on, for example to let a LED
light up or turn a stepper around depending on the right recognized RFID tag, it will be
easier to use a IF command with one contiguous number. Example:
If the RFID Code is 1031720, a LED should turn for 5 seconds.
The command would be more complicated this way: If the first block is 195 and the
second block is 765 and the third block is 770 and the fourth block is 233 a LED should
turn on for 5 seconds.
A disadvantage of the decimal way is the fact that the sketch can be a little bit unsafe
because not all four blocks (max. 12 numbers) can be shown as a contiguous number.
If you want it to be completely safe you would have to test every single block.
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
88
void setup()
{
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
}
void loop()
{
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
long code=0; //We are using code as a new variable to save the UID as
//contiguous number later on. By using long instead of int we are able to
//save a longer number.
for (byte i = 0; i < mfrc522.uid.size; i++)
{
code=((code+mfrc522.uid.uidByte[i])*10); //Now the four blocks are read out and
//the code gets stretched by the factor 10 every passing (Actually, we should
//use the factor 1000, but the number would become too large).
}
Serial.print("The Card number is:"); //Finally the number code (you can't say
//UID anymore)
Serial.println(code);
}
Great, we are now able to get the individual identification number (on the serial monitor)
from a RFID Tag. In this case the number of the tag is 1031720.
89
And now? We want a LED to turn on for 5 seconds if the wanted RFID Tag gets hold in
front of the RFID Reader.
Sketch 3
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode (2, OUTPUT); //Pin 2 gets defined as output (here we are going to
//connect the LED)
}
void loop()
{
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
90
}
long code=0;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
code=((code+mfrc522.uid.uidByte[i])*10);
}
91
Keypad shield
92
The keypad shield can be plugged on the UNO board or the MEGA board for example.
The power supply pins of the keypad shield should be plugged into the power supply pins
of the microcontroller board (on the middle bottom you can find the power supply pins of
the keypad shield. The label VIN or 5V can also help you to find them). The pins on the top
of the Arduino are also used by the keypad shield (pin 0-13). Some of them are used for
the LCD on the keypad shield. The other free pins are combined in a row of slots (see on
the top of the image).
If you want to use these slots, we recommend to solder a connector strip onto them.
One example of a sketch can be the following:
Code:
//Sample using LiquidCrystal library
#include <LiquidCrystal.h>
/*******************************************************
This program will test the LCD panel and the buttons
Mark Bramwell, July 2010
********************************************************/
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
93
94
break;
}
case btnLEFT:
{
lcd.print("LEFT ");
break;
}
case btnUP:
{
lcd.print("UP ");
break;
}
case btnDOWN:
{
lcd.print("DOWN ");
break;
}
case btnSELECT:
{
lcd.print("SELECT");
break;
}
case btnNONE:
{
lcd.print("NONE ");
break;
}
}
}
95
IC Display
96
Wiring: The connection of the IC LCD is very simple. The module has only four contacts.
GND gets connected to GND on the Arduino, VCC to 5V on the Arduino, SDA with the
analog Input A4 and SCL to the analog Input A5.
LCD >> UNO
GND >> GND
VCC >> 5V
SDA >> A4
SCL >> A5
Attention!: The MEGA2560 microcontroller has its own SDA and SCL pins. You can find
them on pin 20 and pin 21.
97
Programing:
We will need another library to work with the IC LCD, which isn't already installed in the
arduino software. You can download the zip for example here:
https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library .
After you have downloaded the library you have to add it to the arduino software.
You can easily do this at the arduino software at Sketch then Include Library and
add .ZIP Library... Now you can use the library while writing a sketch.
Sketch:
#include <Wire.h> //Include Wire library
#include <LiquidCrystal_I2C.h>//Include the previous downloaded
//LiquidCrystal_I2C library
LiquidCrystal_I2C lcd(0x27, 16, 2);
//display we are using. In this case it is one with 16 signs and 2 rows.
void setup()
{
lcd.begin(); //In the setup part we are starting the LCD (without anything
//inside the brackets, because we defined the LCD already).
}
void loop()
{
lcd.setCursor(0,0); //At the loop part the IC LCD is programed just like the
//simple LED
lcd.print("Funduino GmbH");
98
lcd.setCursor(0,1);
//should start
Extension:
Just like with the simple LCD, you can show the result of measurements on the IC LCD.
Example code (moisture sensor gets connected to pin A0):
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int measurement=0;
void setup()
{
lcd.begin();
}
void loop()
{
measurement=analogRead(A0);
measurement
99
//In the second row we want to show the read out value of
100