arduino starter kit tutorial

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: [email protected] 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 (Haftungsbeschränkt), 07.09.2014 Copyright © 2014 Funduino UG (Haftungsbeschränkt) 1

Upload: diego-dias

Post on 19-Nov-2015

22 views

Category:

Documents


7 download

DESCRIPTION

How to make projects with Arduino

TRANSCRIPT

  • 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:

    [email protected]

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

  • 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) 2

  • 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()

    {

    pinMode(13, OUTPUT);

    }

    void loop()

    {

    digitalWrite(13, HIGH);

    delay(1000);

    digitalWrite(13, LOW);

    delay(1000);

    }

    Here the setup begins

    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.

    Here the main sketch (loop) begins

    Voltage (5V) high on pin 13

    1000ms (1 second) delay

    Voltage low on pin 13 (0 V)

    1000ms (1 second) delay

    Now the loop starts again.

    Copyright 2014 Funduino UG (Haftungsbeschrnkt) 3

  • 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) 4

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

    pinMode(8,OUTPUT);

    }

    void loop()

    {

    digitalWrite(7, HIGH);

    delay(1000);

    digitalWrite(7, LOW);

    digitalWrite(8, HIGH);

    delay(1000);

    digitalWrite(8, LOW);

    }

    Pin 7 is an output.

    Pin 8 is an output.

    Here the main program begins

    Voltage (5V) high on pin 7

    1000ms (1 second) delay

    Voltage low on pin 7 (0V)

    Voltage (5V) high on pin 8

    1000ms (1 second) delay

    Voltage low on pin 8 (0V)

    Now the loop starts again.

    Copyright 2014 Funduino UG (Haftungsbeschrnkt) 5

  • 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;

    int beep=5;

    void setup()

    {

    pinMode(LED, OUTPUT);

    pinMode(beep,OUTPUT);

    }

    void loop()

    {

    digitalWrite(LED, HIGH);

    digitalWrite(beep, HIGH);

    delay(1000);

    digitalWrite(LED, LOW);

    digitalWrite(beep, LOW);

    delay(1000);

    }

    The word LED is now 4

    The word beep is now 5

    Pin 4 (Pin LED) is an output.

    Pin 5 (Pin Pieps) is an output.

    Switch on the LED

    Switch on the piezo-speaker

    Wait one second

    Switch off the LED

    Switch off the piezo-speaker

    Wait one second

    Copyright 2014 Funduino UG (Haftungsbeschrnkt) 6

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

    brightness = brightness + fadesteps;

    delay(25);

    if (brightness == 0 || brightness ==

    255)

    {

    The function analogWrite activates the PWM-function.

    For more information search for PWM on the arduino.cc

    webside or wikipedia.com

    Copyright 2014 Funduino UG (Haftungsbeschrnkt) 7

  • 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) 8

  • }

    void loop()

    {

    buttonstate =digitalRead(pushbutton);

    if (buttonstate == HIGH)

    {

    digitalWrite(LEDblue, HIGH);

    delay (5000);

    digitalWrite(LEDblue, LOW);

    }

    else

    {

    digitalWrite(LEDblue, LOW);

    }

    }

    the Arduino-board checks the incoming

    voltage on that pin.

    If the buttonstate is high...

    switch on the LED...

    ...for five seconds...

    ...and then switch off the LED

    otherwise...

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

  • int intensity= A0;

    int LED = 10;

    int sensorvalue = 0;

    void setup()

    {

    Serial.begin(9600);

    pinMode (LED, OUTPUT);

    }

    void loop()

    {

    sensorvalue =analogRead(intensity);

    Serial.print("sensorvalue = " );

    Serial.println(sensorvalue);

    if (sensorvalue > 512 )

    {

    digitalWrite(LED, HIGH);

    }

    else

    {

    digitalWrite(LED, LOW);

    }

    delay (50);

    }

    Activates the serial communication. Later it allowes to send

    the measured value to the serial monitor.

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

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

    to 5 volt)

    Serial.print sends informations to the serial monitor.

    If the value is above 512...

    ...the LED gets switched on...

    ...otherwise...

    ...it stays switched off.

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

    digitalWrite (LED, HIGH);

    delay (sensorvalue);

    digitalWrite (LED, LOW);

    delay (sensorvalue);

    }

    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

    number between 0 and 1023.

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

    delay-time in milliseconds.

    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;

    int movement=7;

    int movestatus=0;

    void setup()

    {

    pinMode(piezo, OUTPUT);

    pinMode(movement, INPUT);

    }

    void loop()

    {

    movestatus =digitalRead(movement);

    if (movestatus == HIGH)

    {

    digitalWrite(piezo, HIGH);

    delay(5000);

    digitalWrite(piezo, LOW);

    Piezo-speaker on pin5

    Movementsensor on pin7

    Value for detected movement

    Read the status of movement

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

    piezo-speaker will make a noise.

    Copyright 2014 Funduino UG (Haftungsbeschrnkt) 13

  • }

    else

    {

    digitalWrite(piezo, LOW);

    }

    }

    .otherwise...

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

    int temperature = 0;

    int temp[10];

    int time= 20;

    void setup() {

    Serial.begin(9600);

    }

    void loop() {

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

    delay(time);

    The pin in the middle (signal) is connected

    to analog pin A0.

    Value for the temperature.

    temp[10] creates ten values with the

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

    on...

    The value time is for the delay between

    two measurements.

    Starts the serial communication. It will

    send the informations from the Arduino-

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

    From here, the temperature gets measured

    ten times. In the same line, the measured

    Copyright 2014 Funduino UG (Haftungsbeschrnkt) 15

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

    delay(time);

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

    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

    mp[5]+temp[6]+temp[7]+temp[8]+temp[9]+temp[10])/

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

    Serial.print(temperatur);

    Serial.println(" degree");

    }

    voltage gets transformed in a number

    between -50 and 150. The function is called

    map.

    The ten temperatures get added and

    divided with ten, to get a average

    temperature

    The average temperature from the ten

    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;

    void setup() {

    Piezo-speaker on pin5.

    Copyright 2014 Funduino UG (Haftungsbeschrnkt) 16

  • Serial.begin(9600);

    pinMode (piezo, 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)

    {

    digitalWrite(piezo,HIGH);

    }

    else

    {

    digitalWrite(piezo,LOW);

    }

    }

    Pin5 is an output.

    If the temperature is above 30C

    the piezo gives a sound

    or...

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

    int echo=6;

    long time=0;

    long dist=0;

    void setup()

    {

    Serial.begin (9600);

    pinMode(trigger, OUTPUT);

    pinMode(echo, INPUT);

    }

    void loop()

    {

    digitalWrite(trigger, LOW);

    delay(5);

    digitalWrite(trigger, HIGH);

    delay(10);

    digitalWrite(trigger, LOW);

    dauer = pulseIn(echo, HIGH);

    dist = (time/2) / 29.1;

    if ( dist >= 500 || dist

  • Serial.println("No

    measurement");

    }

    else

    {

    Serial.print(dist);

    Serial.println(" cm");

    }

    delay(1000);

    }

    No measurement

    otherwise...

    the calculated distance gets send to the serial-monitor

    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;

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

    dauer = pulseIn(echo, HIGH);

    entfernung = (dauer/2) / 29.1;

    if (entfernung >= 500 ||

    entfernung

  • Serial.print(entfernung);

    Serial.println(" cm");

    }

    if (entfernung

  • 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 arduino-

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

    int RECV_PIN = 11;

    IRrecv irrecv(RECV_PIN);

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

    The signal-pin from the IR-Receiver is

    connected to pin 11

    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

    int RECV_PIN = 11;

    IRrecv irrecv(RECV_PIN);

    decode_results results;

    void setup()

    {

    Serial.begin(9600);

    pinMode (13, OUTPUT);

    digitalWrite(13, LOW);

    irrecv.enableIRIn();

    }

    void loop() {

    if (irrecv.decode(&results)) {

    Serial.println(results.value, DEC);

    if (results.value == 16724175)

    {digitalWrite (13, HIGH);}

    if (results.value == 16718055)

    {digitalWrite (13, LOW);}

    On pin13 is a LED (output)

    It starts with a switched off LED.

    If the IR-receiver receives the number 16724175

    (button1), the LED gets switched on.

    If the IR-receiver receives the number 16718055

    (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 servoblue;

    void setup()

    {

    servoblue.attach(8);

    }

    Include the servo library

    the servo gets the name servoblue

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

    Load the LCD-library

    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 Arduino-

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