Sunteți pe pagina 1din 28

Arduino for Starter

Tutorials for the Arduino Kits from


www.funduino.de

This guide is in process. If you have any suggestions, please contact us: info@funduino.de

Funduino UG (Haftungsbeschrnkt), 12.01.2015


1

Content

Programming..............................................................................................................3
1. Sketch No.1: A flashing LED..................................................................................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...........................................................17
11. Sketch No.11: Usage of a infrared remote.........................................................21
12. Sketch No.12: control a servo............................................................................25
13. Sketch No.13: Show a text on a LCD display....................................................27

Programming

1. Sketch No.1: A flashing LED

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

void setup()

Here the setup begins

{
pinMode(13, OUTPUT);

Pin 13 is a output.

}
void loop()

Here the main program begins

{
digitalWrite(13, HIGH);

Voltage (5V) high on pin 13

delay(1000);

1000ms (1 second) delay

digitalWrite(13, LOW);

Voltage low on pin 13 (0V)

delay(1000);

1000ms (1 second) delay

}
Now the loop starts again.

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);
digitalWrite(13, LOW);
delay(200);
}

2. Sketch No.2: Two flashing LEDs


Required equipment: Arduino / two LEDs (blue) / two resistors 100 Ohm / Breadboard / cables

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

Pin 7 is a output.

pinMode(8,OUTPUT);

Pin 8 is a 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.

3. Sketch No.3: sound and light


Required equipment: Arduino / 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 a output.

pinMode(beep,OUTPUT);

Pin 5 (Pin Pieps) is a output.

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

Switch the LED on

digitalWrite(beep, HIGH);

Switch the piezo-speaker on

delay(1000);

Wait one second

digitalWrite(LED, LOW);

Switch the LED off

digitalWrite(beep, LOW);

Switch the piezo-speaker off

delay(1000);

Wait one second


6

}
4. Sketch No.4: A pulsating LED

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;


delay(25);
if (brightness == 0 || brightness ==
255)
7

{
fadesteps = - fadesteps ;
}
}

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


A LED has to be switched on for fife 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);
8

pinMode(pushbutton, INPUT);

Now the mode has to be input, because

the Arduino-board checks the incoming


voltage on that pin.

void loop()
{
buttonstate =digitalRead(pushbutton);
if (tasterstatus == HIGH)
{
digitalWrite(LEDblue, HIGH);
delay (5000);
digitalWrite(LEDblue, LOW);
}
else
{
digitalWrite(LEDblue, LOW);
}
}

6. Sketch No.6: measure light intensity


If the light intensity is low (as possible in the night), the LED gets switched on

int intensity= A0;


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

Activates the serial communication

}
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 )


{
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
delay (50);
}

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 middle potentiometer-pin is in the range 0


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

digitalWrite (LED, HIGH);

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);
}
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.

12

int piezo=5;
int movement=7;
int movestatus=0;

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);
13

}
else
{
digitalWrite(piezo, LOW);
}
}

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 outputs 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).

int TMP36 = A0;

The middle pin (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() {
14

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

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 addet and

mp[5]

divided with ten, to get a average

+temp[6]+temp[7]+temp[8]+temp[9]+temp[10])/10; // temperature.
everything in one line!!!!
Serial.print(temperatur);

The average temperature from the ten

Serial.println(" degree");

measurements get send to the serial-

monitor.

15

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() {
Serial.begin(9600);
pinMode (piezo, OUTPUT);

Pin5 is a 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
If the temperature is above 30C
Serial.print(temperatur);
Serial.println(" Grad Celsius");

the piezo gives a sound

if (temperatur>=30)

or...

{
digitalWrite(piezo,HIGH);

...it is quiet.

}
16

else
{
digitalWrite(piezo,LOW);
}
}

10. Sketch No.10: Measurement of distance


We want to measure the distance with the HC-SR04 ultrasonic sensor.
How does the ultrasonic sensor function?
The sensor has four pins.
a) 5V (+) b) GND (-) c) d echo) 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

17

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 an


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.
18

pinMode(trigger, OUTPUT);

"trigger" (Pin7) is a output.

pinMode(echo, INPUT);

"echo" (Pin6) is a 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.

time = 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

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.

19

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 a 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
{

20

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 a infrared remote


With an infrared receiver, the Arduinoboard can receive the commands
of an infrared remote control. The data are 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

21

22

The sketch is a variation of the sketch IRrecvDemo, an can be downloaded on the following link.
https://github.com/shirriff/Arduino-IRremote
You can download the zip-package and copy the files into your libraries directory in the arduinosoftware. Rename the downloaded directory 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();
23

}
}

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 hold the button permanently pressed, 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.

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


24

}
}

12. Sketch No.12: control a servo


A servo has to turn to three different positions. Between the movents 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

25

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

Position1 with a angle of 0


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

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

Required equipment: microcontroller 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.

26

#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) .
27

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 (In the picture VCC and GND). On the IN-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 three terminals on the right side you can connect the cables from the electrical thing, you
want to switch on and off.
The relays connects the terminals middle and right while the relays is switched off and when it is
activated, it connects the terminals middle and leftt.
For testing purpose, you can use the blink-sketch. Instead of the LED, you connect the output-pin
from the Arduino-board with the INpin 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);
}

28

S-ar putea să vă placă și