Sei sulla pagina 1di 29

Arduino for beginner

Tutorials especially for the


Arduino Kits from www.funduino.de

This tutorials are in process. If you have any suggestions, please contact us:
info@funduino.de
We know, that our english sounds very german until now ;) We will work on a better translation.
But we hope, that the tutorials are useful anyway.

Funduino UG (Haftungsbeschrnkt), 07.09.2014


Copyright 2014 Funduino UG (Haftungsbeschrnkt)

Content
Programming............................................................................................................3
1. Sketch No.1: A flashing LED (blink)......................................................................3
2. Sketch No.2: Two flashing LEDs...........................................................................5
3. Sketch No.3: Sound and light...............................................................................6
4. Sketch No.4: A pulsating LED...............................................................................7
5. Sketch No.5: Switch a LED on by pressing a pushbutton.....................................8
6. Sketch No.6: Measure light intensity....................................................................9
7. Sketch No.7: Use a potentiometer to choose the flashing-speed of a LED........11
8. Sketch No.8: Movement detection......................................................................12
9. Sketch No.9: Temperature measurement...........................................................14
10. Sketch No.10: Measurement of distance..........................................................18
11. Sketch No.11: Usage of an infrared remote......................................................22
12. Sketch No.12: Control a servo..........................................................................26
13. Sketch No.13: Show a text on a LCD display...................................................27
14. Sketch No.14: Use a relais shield.....................................................................29

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

Programming

1. Sketch No.1: A flashing LED (blink)

Required equipment: Only the Arduino board and an USB-cable.


There is LED mounted on the arduino-board that is connected with the pin13. In this sketch we want
to vary the speed of blinking.

void setup()

Here the setup begins

{
pinMode(13, OUTPUT);

Pin 13 is a output. (Because the arduino-board has to put out a

voltage. In case of a connected sensor, the pin has to be declared


as an input.

void loop()

Here the main sketch (loop) begins

{
digitalWrite(13, HIGH);

Voltage (5V) high on pin 13

delay(1000);

1000ms (1 second) delay

digitalWrite(13, LOW);

Voltage low on pin 13 (0 V)

delay(1000);

1000ms (1 second) delay

}
Now the loop starts again.

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

Upload the sketch on the Board.

1.4 Extension of the Sketch


The LED has to flash faster by using a shorter delay
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(200);

// this is the shorter delay

digitalWrite(13, LOW);
delay(200);

// this is the shorter delay

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

2. Sketch No.2: Two flashing LEDs


Required equipment: Arduino-board / two LEDs (blue) / two resistors 100 Ohm / breadboard / cables

void setup()
{
pinMode(7, OUTPUT);

Pin 7 is an output.

pinMode(8,OUTPUT);

Pin 8 is an output.

}
void loop()

Here the main program begins

{
digitalWrite(7, HIGH);

Voltage (5V) high on pin 7

delay(1000);

1000ms (1 second) delay

digitalWrite(7, LOW);

Voltage low on pin 7 (0V)

digitalWrite(8, HIGH);

Voltage (5V) high on pin 8

delay(1000);

1000ms (1 second) delay

digitalWrite(8, LOW);

Voltage low on pin 8 (0V)

}
Now the loop starts again.

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

3. Sketch No.3: Sound and light


Required equipment: Arduino-board / 1x LED / 1x resistor 200 Ohm / 1x Piezo-speaker / breadboard /
cables

int LED=4;

The word LED is now 4

int beep=5;

The word beep is now 5

void setup()
{
pinMode(LED, OUTPUT);

Pin 4 (Pin LED) is an output.

pinMode(beep,OUTPUT);

Pin 5 (Pin Pieps) is an output.

}
void loop()
{
digitalWrite(LED, HIGH);

Switch on the LED

digitalWrite(beep, HIGH);

Switch on the piezo-speaker

delay(1000);

Wait one second

digitalWrite(LED, LOW);

Switch off the LED

digitalWrite(beep, LOW);

Switch off the piezo-speaker

delay(1000);

Wait one second

}
Copyright 2014 Funduino UG (Haftungsbeschrnkt)

4. Sketch No.4: A pulsating LED


Required equipment: Arduino-board / 1x LED / 1x resistor 200 Ohm / breadboard / some wires

int LED=9;
int brightness= 0;
int fadesteps= 5;
void setup()
{
pinMode(LED, OUTPUT);
}
void loop()
{
analogWrite(LED, brightness);

The function analogWrite activates the PWM-function.

brightness = brightness + fadesteps;

For more information search for PWM on the arduino.cc

delay(25);

webside or wikipedia.com

if (brightness == 0 || brightness ==
255)
{
Copyright 2014 Funduino UG (Haftungsbeschrnkt)

fadesteps = - fadesteps ;
}
}

5. Sketch No.5: Switch a LED on by pressing a pushbutton


A LED has to be switched on for five seconds after a pushbutton has been pressed.
Required equipment: Arduino / 1x LED (blue) / 1x resistor 100 Ohm / 1x resistor 1KOhm (1000 Ohm) /
breadboard / cable / 1x pushbutton

int LEDblue=6;
int pushbutton=7;
int buttonstate=0;
void setup()
{
pinMode(LEDblue, OUTPUT);
pinMode(pushbutton, INPUT);

Now the mode has to be input, because

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

the Arduino-board checks the incoming


voltage on that pin.

void loop()
{
buttonstate =digitalRead(pushbutton);
if (buttonstate == HIGH)

If the buttonstate is high...

{
digitalWrite(LEDblue, HIGH);

switch on the LED...

delay (5000);

...for five seconds...

digitalWrite(LEDblue, LOW);

...and then switch off the LED

}
else

otherwise...

{
digitalWrite(LEDblue, LOW);

...the LED stays switched off.

}
}

6. Sketch No.6: Measure light intensity


If the light intensity is low (for example in the night), the LED gets switched on

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

int intensity= A0;


int LED = 10;
int sensorvalue = 0;
void setup()
{
Serial.begin(9600);

Activates the serial communication. Later it allowes to send

pinMode (LED, OUTPUT);

the measured value to the serial monitor.

}
void loop()
{
sensorvalue =analogRead(intensity);

analogRead(intensity) reads the voltage on pin A0 (analog

Serial.print("sensorvalue = " );

0). The value gets saved as a number between 0 and 1023 (0

Serial.println(sensorvalue);

to 5 volt)
Serial.print sends informations to the serial monitor.

if (sensorvalue > 512 )

If the value is above 512...

{
digitalWrite(LED, HIGH);

...the LED gets switched on...

}
else

...otherwise...

{
digitalWrite(LED, LOW);

...it stays switched off.

}
delay (50);

Wait a little bit before the sketch starts again.

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

10

7. Sketch No.7: Use a potentiometer to choose the flashing-speed of a LED

int input= A0;


int LED = 13;
int sensorvalue = 0;
void setup()
{
pinMode (LED, OUTPUT);
}
void loop()
{
sensorvalue =analogRead(input);

The voltage on the potentiometer-pin (in the middle) is in the


range 0 volt to 5 volt. The Arduino-board will save it as a

digitalWrite (LED, HIGH);

number between 0 and 1023.

delay (sensorvalue);

That value gets used by the delay. The number is now the

digitalWrite (LED, LOW);

delay-time in milliseconds.

delay (sensorvalue);
}

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

11

8. Sketch No.8: Movement detection


A piezo-speaker has to make a noise if a movement gets detected.

Left side: time of output in case of a


detected movement.
Right side: sensibility

1) Jumper outside: in case of a detected


movement, the output signal (5 volt)
holds for some time.
2) Jumper inside (picture): the output
signal is only active while a movement is
detected.

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

12

int piezo=5;

Piezo-speaker on pin5

int movement=7;

Movementsensor on pin7

int movestatus=0;

Value for detected movement

void setup()
{
pinMode(piezo, OUTPUT);
pinMode(movement, INPUT);
}
void loop()
{
movestatus =digitalRead(movement);

Read the status of movement

if (movestatus == HIGH)

If the voltage on the movement input-pin is high, the

piezo-speaker will make a noise.

digitalWrite(piezo, HIGH);
delay(5000);
digitalWrite(piezo, LOW);
Copyright 2014 Funduino UG (Haftungsbeschrnkt)

13

}
else

.otherwise...

{
digitalWrite(piezo, LOW);

...the speaker is quiet.

}
}

9. Sketch No.9: Temperature measurement


We want to read the temperature with theTMP36 sensor. The temperature should be shown on the
serial-monitor
Required equipment: Arduino / breadboard / jumper-wire / temperaturesensor 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 voltage on this pin must be read by the microcontroller board and then it hast 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).

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

14

int TMP36 = A0;

The pin in the middle (signal) is connected


to analog pin A0.

int temperature = 0;

Value for the temperature.

int temp[10];

temp[10] creates ten values with the


names temp[1], temp[2], temp[3] and so
on...

int time= 20;

The value time is for the delay between


two measurements.

void setup() {
Serial.begin(9600);

Starts the serial communication. It will

send the informations from the Arduinoboard to the computer to show it there in
the serial monitor.
You can start the serial monitor in the
arduino-software with a click on settings
and serial monitor.

void loop() {
temp[1] = map(analogRead(TMP36), 0, 410, -50, 150);

From here, the temperature gets measured

delay(time);

ten times. In the same line, the measured

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

15

temp[2] = map(analogRead(TMP36), 0, 410, -50, 150);

voltage gets transformed in a number

delay(time);

between -50 and 150. The function is called

temp[3] = map(analogRead(TMP36), 0, 410, -50, 150);

map.

delay(time);
temp[4] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[5] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[6] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[7] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[8] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[9] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[10] = map(analogRead(TMP36), 0, 410, -50, 150);
temperature=(temp[1]+temp[2]+temp[3]+temp[4]+te

The ten temperatures get added and

mp[5]+temp[6]+temp[7]+temp[8]+temp[9]+temp[10])/ divided with ten, to get a average


10; // everything in one line!!!!

temperature

Serial.print(temperatur);

The average temperature from the ten

Serial.println(" degree");

measurements gets send to the serial-

monitor

9.1 Extension of the sketch:


If the temperature reaches 30C , a noise from the piezo-speaker appears.

int TMP36 = A0;


int temperature = 0;
int temp[10];
int time= 20;
int piezo=5;

Piezo-speaker on pin5.

void setup() {

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

16

Serial.begin(9600);
pinMode (piezo, OUTPUT);

Pin5 is an output.

}
void loop() {
temp[1] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[2] = map(analogRead(TMP36), 0, 410, -50, 150);
.
temp[9] = map(analogRead(TMP36), 0, 410, -50, 150);
delay(time);
temp[10] = map(analogRead(TMP36), 0, 410, -50, 150);
temperature=(temp[1]+temp[2]+temp[3]+temp[4]+temp[
5]
+temp[6]+temp[7]+temp[8]+temp[9]+temp[10])/10; // all
in one line
Serial.print(temperatur);
Serial.println(" Grad Celsius");
if (temperatur>=30)

If the temperature is above 30C

{
digitalWrite(piezo,HIGH);

the piezo gives a sound

}
else

or...

{
digitalWrite(piezo,LOW);

...it is quiet

}
}

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

17

10. Sketch No.10: Measurement of distance


We want to measure the distance with the HC-SR04 ultrasonic sensor.
How does the ultrasonic sensor work?
The sensor has four pins.
a) 5V (+) b) GND (-)

c) echo d) trigger

The connections 5V and GND are for the power supply. The Pin "trigger" gets a short signal (5V), and
creates 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
sends a signal to the Arduino microcontroller by 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.
Required equipment: microcontroller board / cable / breadboard / HC-SR04 ultrasonic sensor

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

18

int trigger=7;

trigger on pin7.

int echo=6;

echo on pin 6.

long time=0;

The value time will save the time between transmition and
returning of the soundwave.

long dist=0;

The value dist will save the calculated distance. It will start
with 0. Instead of int we use long for this value, to save
a bigger number

void setup()
{
Serial.begin (9600);

Starts the serial communication. It will send the informations


from the Arduino-board to the computer to show it there in
the serial monitor.

pinMode(trigger, OUTPUT);

"trigger" (Pin7) is an output.

pinMode(echo, INPUT);

"echo" (Pin6) is an input.

}
void loop()
{
digitalWrite(trigger, LOW);

low voltage on the trigger pin to produce a clear signal.

delay(5);

...for 5 milliseconds.

digitalWrite(trigger, HIGH);

Creating the soundwave.

delay(10);

...for 10 milliseconds.

digitalWrite(trigger, LOW);

Stop creating the soundwave.

dauer = pulseIn(echo, HIGH);

With the command pulseIn " (with a big i next to the last
n) the Arduino-board counts the time between sending and
receiving the soundwave.

dist = (time/2) / 29.1;

This calculation transforms the measured time into the


distance in centimeters. (The sound needs 29,1 seconds for
one centimeter. The time is devided by two, because we only
want to receive only one distance and not the two ways, the
sound has to take)

if ( dist >= 500 || dist <= 0)

If the distance is over 500cm OR under 0cm, the

measurement is not accurate. So the serial-monitor shows

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

19

Serial.println("No

No measurement

measurement");
}
else

otherwise...

{
Serial.print(dist);

the calculated distance gets send to the serial-monitor

Serial.println(" cm");
}
delay(1000);

This command causes a short break between the

measurements.

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

20

10.1 Extension of the sketch


If the distance is less than 80cm, a sound from the piezo-speaker should appear.

int trigger=12;
int echo=13;
long dauer=0;
long entfernung=0;
int piezo=5;

Piezo-speaker on pin5

void setup()
{
Serial.begin (9600);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(piezo, OUTPUT);

The pin for the speaker is an output

}
void loop()
{
digitalWrite(trigger, LOW);
delay(5);
digitalWrite(trigger, HIGH);
delay(10);
digitalWrite(trigger, LOW);
dauer = pulseIn(echo, HIGH);
entfernung = (dauer/2) / 29.1;
if (entfernung >= 500 ||
entfernung <= 0)
{
Serial.println("Kein Messwert");
}
else
{

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

21

Serial.print(entfernung);
Serial.println(" cm");
}
if (entfernung <= 80)

If the distance is less than 80cm...

{
digitalWrite(piezo,HIGH);

...the speakter makes some noise.

}
else

otherwise...

{
digitalWrite(piezo,LOW);

it is quiet.

}
delay(1000);
}

11. Sketch No.11: Usage of an infrared remote


With an infrared receiver, the Arduinoboard can receive the commands
of an infrared remote control. The data is sent with infrared light from
remote control to the receiver. Since our eyes can not perceive this
light, we can not see this light.
With a little trick you can see the light.
Take your mobile-phone and look with the camera on the infrared
diode of the remote while pressing a button on the remote.
You will see the flashing infrared diode on the display of the mobilephone.

Required equipment: Arduino / breadboard / cable / infrared sensor /


infrared remote control

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

22

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

23

The sketch is a variation of the sketch IRrecvDemo, and can be downloaded on the following link:
https://github.com/shirriff/Arduino-IRremote
You can download the zip-package and copy the folder into your libraries directory in the arduinosoftware. Rename the folder to "Irremote".
Now you can open the sketch in the sample-files in the arduino-software:
File -> Examples -> IRremote -> IRrecvDemo
Now we edit the sketch to this sketch:

/*
* 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
* http://arcfn.com
*/
#include <IRremote.h>
int RECV_PIN = 11;

The signal-pin from the IR-Receiver is

IRrecv irrecv(RECV_PIN);

connected to pin 11

decode_results results;
void setup()
{
Serial.begin(9600);
pinMode (13, OUTPUT);
irrecv.enableIRIn();
}
void loop()
{
if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);
irrecv.resume();

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

24

}
}

Pressing the "1" key on the infrared remote control causes (in my case) the serial-monitor writes the
number "16724175". This is the decrypted number code behind this button.
When you press the button permanently, the number "4294967295" appears. This is the code that
indicates that a key is pressed continuously. This number does not depend on which key is pressed.
There can also appear other numbers if a key is pressed only very short or pulsating. In the case the
sensor may not read 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);

On pin13 is a LED (output)

digitalWrite(13, LOW);

It starts with a switched off LED.

irrecv.enableIRIn();
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, DEC);
if (results.value == 16724175)

If the IR-receiver receives the number 16724175

{digitalWrite (13, HIGH);}

(button1), the LED gets switched on.

if (results.value == 16718055)

If the IR-receiver receives the number 16718055

{digitalWrite (13, LOW);}

(button2), the LED gets switched off.

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

25

irrecv.resume(); // Receive the next value


}
}

12. Sketch No.12: Control a servo


A servo has to turn to three different positions. Between the movements is a short break.
Required equipment: A microcontroller board, a servo, three jumper wire

#include <Servo.h>

Include the servo library

Servo servoblue;

the servo gets the name servoblue

void setup()
{
servoblue.attach(8);
}

The signal-line of the servo is on pin8

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

26

void loop()
{
servoblue.write(0);
delay(3000);
servoblue.write(90);
delay(3000);
servoblue.write(180);
delay(3000);
servoblue.write(20);
delay(3000);
}

Position1 with an angle of 0


break for 3 seconds
Position2 with an angle of 90
break for 3 seconds
Position3 with an angle of 180
break for 3 seconds
Position4 with an angle of 0
break for 3 seconds

13. Sketch No.13: Show a text on a LCD display

Required equipment: Arduino-board, potentiometer, some jumper wire , breadboard


Note: The potentiometer is needed to adjust the contrast.
A good cabling is very important, solder the cable to the LCD.

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

27

#include <LiquidCrystal.h>

Load the LCD-library

LiquidCrystal lcd(12, 11, 6, 5, 4, 3);

void setup() {
lcd.begin(16, 2);
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("www.funduino.de");
lcd.setCursor(0, 1);

This LCD has 16 signs in two rows.

Startposition of the cursor on the LCD (0,0 = first


character in the first row) .
Write the text www.funduino.de.
Startposition of the cursor on the LCD (0,0 = first
character in the second row) .

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

28

lcd.print("good luck!!!");
}

Write the text good luck!!!.

14. Sketch No.14: Use a relais shield

A relays is a switch, that can be activated with a low current from the
Arduino-board. So you can switch on and off electrical things, that need
much more power than a Arduino-board can provide.
The relays need a permanent power supply with 5V+ and (Top of the
picture). On the Signal-pin, the switch can be activated by the Arduinoboard. Dependent of the manufacturer, there has to be a LOW or HIGH
signal from the arduino output-pin.
On the terminal A, B and C you can connect the cables from the electrical
thing, you want to switch on and off.
The relays connects the terminals A and B while the relays is switched off and when it is activated, it
connects the terminals A and C.
For testing purpose, you can use the blink-sketch. Instead of the LED, you connect the output-pin
from the Arduino-board with the signal-pin from the relay. With that sketch, the relays will switch on
and off in a 1 second rhythm.

void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}

This tutorial is under construc7on. There will be more tutorials soon www.funduino.de.

Copyright 2014 Funduino UG (Haftungsbeschrnkt)

29

Potrebbero piacerti anche