Docsity

Arduino tutorials for beginner, Study Guides, Projects, Research of Electronic Technology

Toggle sidebar
bg1
Tutorials for Arduino
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: [email protected]
Have fun with our tutorials! Kind regards,
Service Team
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Arduino tutorials for beginner and more Study Guides, Projects, Research Electronic Technology in PDF only on Docsity!

Tutorials for Arduino

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: [email protected] Have fun with our tutorials! Kind regards, Service Team

Contents

  • Preface...................................................................................................................................
      1. Preface to the Arduino tutorials.....................................................................................
    1. Hardware and Software.....................................................................................................
    • 2.1 Hardware.....................................................................................................................
      • 2.1.2 Description of typical equipment..........................................................................
      • 2.1.2.1 Breadboard........................................................................................................
      • 2.1.2.2 LED (light emitting diode)..................................................................................
    • 2.2 Software.......................................................................................................................
      • 2.2.1 Installation............................................................................................................
      • 2.2.1.1 Installation and set up of the Arduino software.................................................
      • 2.2.1.2 Installation of the USB driver.............................................................................
    1. Programming......................................................................................................................
    • Basic structure of a sketch:................................................................................................
        1. Name variable...........................................................................................................
        1. Setup (absolutely necessary for the program)..........................................................
  • Tutorials:...............................................................................................................................
    • Blinking LED.....................................................................................................................
    • Alternately blinking LED...................................................................................................
    • Fading LED......................................................................................................................
    • Light and sound...............................................................................................................
    • Push button and LED.......................................................................................................
    • RGB LED.........................................................................................................................
    • Motion detector................................................................................................................
    • Photo resistor...................................................................................................................
    • Potentiometer...................................................................................................................
    • Temperature measurement..............................................................................................
    • Measurement of distance................................................................................................
    • Usage of an infrared remote............................................................................................
    • Control a servo.................................................................................................................
    • LCD Display.....................................................................................................................
    • Relay shield.....................................................................................................................
    • Stepper............................................................................................................................
    • Moisture sensor...............................................................................................................
    • Drop sensor.....................................................................................................................
    • RFID Kit...........................................................................................................................
  • Tutorials with additional Equipment.....................................................................................
    • Keypad shield..................................................................................................................
    • I²C Display.......................................................................................................................

2. Hardware and Software

The term „Arduino“ ist mostly used for both components. The hardware (Arduino Boards) and the corresponding software (Arduino). 2.1 Hardware The Arduino hardware is a so-called microcontrolling board (Following called „board“). Basically it is a circuit board with many electronic parts around the actual microcontroller. On the edge of the board are many pins with whom it is possible to connect different components. Some of them are for example: Switches, LED's, Ultrasonic sensors, temperature sensors, displays, stepper, etc.. There are different kind of boards, that can be used with the Arduino sofware. Different sized “ official” boards, with the official “Aduino” name on it, but also many, mostly cheaper, but equivalent Arduino “fitting” boards. Typical official boards are called Arduino UNO, Arduino MEGA, Arduino Mini, etc. Arduino compatible boards are for example Funduino UNO, Funduino MEGA, Freeduino, Seeduino, Sainsmart UNO etc..

2.1.2 Description of typical equipment 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. 2.1.2.2 LED (light emitting diode) 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 program named arduino.exe. Two set ups on the program are important and should be considered. a) The board that you want to connect, has to be selected on the arduino software. The “Funduino Uno” is here known as “Arduino / Genuino Uno”. 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. 2.2.1.2 Installation of the USB driver How it should be:

  1. You connect the board to the computer.
  2. The Computer recognizes the board and suggests to install a driver automatically. ATTENTION: Wait a second! Most of the time the computer can't find the driver automatically to install it. You might choose the driver by your own to install it. It can be found in the Arduino file under “Drivers”. Control: At the control panel of the Computer you can find the “Device manager”. If the board has been installed successfully, it should appear here. When the installation has failed, there is either nothing special to find or you will find an unknown USB device with a

2.) There is a mistake in the sketch. For example, a word is misspelled or a bracket is missing. In the example on the left the last semicolon in the sketch is missing. In this Case the error report often starts with “excepted..”. This means that the program is still expecting something that is missing.

Basic structure of a sketch:

A sketch can be divided in three parts.

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. 2. Setup (absolutely necessary for the program) 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.

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.

3. Loop (absolutely necessary for the program)

This loop part will be continuously repeated by the board. It assimilates the sketch from beginning to end and starts again from the beginning and so on. Let's start!

1.2 Second part of the program: Setup We only have one output – Pin 13 should put out voltage (The LED should light up.). We start writing in the white area of the arduino software: void setup() //The setup begins here { //opening curly bracket – A program part begins here } //closing curly bracket – A program part is ending here Now we are going to write the setup information between the curly brackets. In this case: “pin 13 is supposed to be an output” : void setup() //The setup begins here { //A program part begins here pinMode(13, OUTPUT); //Pin 13 is supposed to be an ouput. } //A program part is ending here.

1.3 Third part of the program: Loop (main part): void setup() //The setup begins here { //A program part begins here pinMode(13, OUTPUT); //Pin 13 is supposed to be an ouput. } //A program part is ending here. void loop() //The main part of the program begins here { //A program part begins here } //A program part is ending here. Now we bring in the loop part (main part). THIS IS THE COMPLETE SKETCH: void setup() //The setup begins here { //A program part begins here pinMode(13, OUTPUT); //Pin 13 is supposed to be an ouput. } //A program part is ending here. void loop() //The main part of the program begins here { //program part begins here digitalWrite(13, HIGH); //Turn on the voltage on pin 13 (LED on) delay(1000); //Wait for 1000 milliseconds (one second) digitalWrite(13, LOW); //Turn off the voltage on pin 13 (LED off) delay(1000); //Wait for 1000 milliseconds (one second) } //Program ends here

Alternately blinking LED Task: We want to let two LEDs blink alternately. Required equipment: Microcontroller / two LEDs (blue) / two resistors with 100 Ohm / Breadboard / cables Setup:

Code: void setup() { //We are starting with the setup pinMode(7, OUTPUT); //Pin 7 is defined as output pinMode(8, OUTPUT); //Pin 8 is defined as output } void loop() { //The main part starts digitalWrite(7, HIGH); //turn on the LED on pin 7 delay(1000); //wait for 1000 milliseconds digitalWrite(7, LOW); //turn off the LED on pin 7 digitalWrite(8, HIGH); //turn on the LED on pin 8 delay(1000); //wait for 1000 milliseconds digitalWrite(8, LOW); //turn off the LED on pin 8 } //Here at the end of the loop the program starts again from the beginning of //the loop part. So..turn on LED on pin 7..wait for 1000 //milliseconds..etc..etc..

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; //The word “LED” stands for the number 9. int brightness=0; //The word “brightness” stands for the value that is emitted //at the PWM.The number “0” is only used as an initial value. int fading=5; //”fading” sets up the speed of the fading. void setup() { // The setup begins here. pinMode(LED, OUTPUT); //The pin with the LED is supposed to be an output. } void loop() { //The loop part begins here. analogWrite(LED, brightness); //The function analogWrite is used to activate // 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.

if(brightness==0 || brightness== 255){ fading= -fading; } //Meaning of this command: If the Value of brightness will reach 0 OR 255, the //value of fading will change from negative to positive and the other way //around. The Reason why is, that while the program is running trough the loop //part, the LED will shine brighter and brighter. But at the point of 255 as a //value of the PWM, it will reach it's maximum. At this point the LED should get //darker step by step. Because of this the fading will be negate at this point. //This means the next time the program runs through the loop the part //“brightness=brightness+fading;” will effect that the LED gets darker. Example: //“brightness=255+(-5)”. The value of brightness will be 250 after that. The //next time it will be 245..etc. As the value of brightness will reach 0 the //value of fading will get positive again. } // With this bracket the loop part ends.