Lesson 1-7 Arduino

Embed Size (px)

DESCRIPTION

Arduino

Citation preview

  • 5/20/2018 Lesson 1-7 Arduino

    1/29

    Arduino 5 Minute Tutorials: Lesson 1

    Software

    Posted on April 11, 2012 byColeman Benson& filed underArduino,Schools & Curriculum,Software and Apps,Tutorials.

    Lessons Menu:

    Lesson 1Software Downloading / Installing & Interface

    Lesson 2Basic Code

    Lesson 3Sensors: Potentiometers

    Lesson 4Sensor: Infrared Distance

    Lesson 5Actuator: Servo Motor

    Lesson 6Sensor: Force, Bend, Stretch

    Lesson 7Sensor: Accelerometer, Gyro, IMU

    Lesson 1 Hardware:

    1. Computer / Laptop or Netbook

    2. Arduino Microcontroller

    3. USB to Serial Adapter(if your microcontroller does not have a USB port)

    4.

    Appropriate USBcable(Arduino boards draw power from the USB port

    no batteriesyet)

    The popularity ofArduinois steadily increasing and it is fast becoming the microcontroller ofchoice for students, hobbyists and smaller companies. Many different electronics PCB

    manufacturing companies are jumping on the bandwagon and producing their own variations of

    the boards, as well as shields(additional circuits that fit directly on top of many Arduinoboards to increase their functionality) and accessories. TheArduino websiteoffers free resources

    http://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorialshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorialshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorialshttp://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/arduino-shields.htmlhttp://www.robotshop.com/arduino-shields.htmlhttp://www.arduino.cc/http://www.arduino.cc/http://www.arduino.cc/http://www.robotshop.com/blog/en/files/arduino-tutorial-lesson-11.jpghttp://www.arduino.cc/http://www.robotshop.com/arduino-shields.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/robots/gorobotics/tutorialshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/author/cbenson
  • 5/20/2018 Lesson 1-7 Arduino

    2/29

    and tutorials as well as a language reference to help you understand the code and syntax. In order

    to get started, you will at the very minimum need an Arduino board. Note that all the Arduino

    (and most of the clone boards) can use the Arduino software. If you are unsure what hardware toget, theArduino USBis currently the most popular model, and these 5 minute tutorials are based

    around it.

    Downloading / Installing Arduino Software

    1.

    Go towww.arduino.ccto download the latest version of the Arduino software (Directlink:http://arduino.cc/en/Main/Softwareand select your operating system; in this case we

    are using Windows)2. Save the ZIP file to your desktop (you can move or delete it later)

    3. It is convenient to create a new folder called Arduino under Program Files. To do

    this, go to My computer -> C: (or the drive where the operating system is installed) -> Program Files, then left click once on program Files folder, then select New-

    >Folder from themain Explorer menu.

    4. Extract the entire ZIP folder to this new Arduino folder

    5. To run the Arduino software, open Windows Explorer by pressing the windows key(usually between the Ctrl and Alt keys on your keyboard) and the E character at the

    same time (there are other ways to access explorer as well).6. Go to My computer -> C: (or the drive where the operating system is installed) ->

    Program Files -> Arduino In this folder you will see an executable file (blue colored

    icon named Arduino), you canleft click (once) and then right click and select send to

    -> Desktop (create shortcut) to have Arduino more easily accessible.

    7. Double click the icon on the desktop to start the software.

    The Arduino Software Interface

    The Arduino interface is pretty bare-bones. When you load the software, the first screen youwill see is a white window (shown below) with several different shades of blue and blue-green as

    border. Arduino projects are called sketches and when you start a new sketch, several

    additional files are also created.

    Newest: Arduino Interface(as of Q1 2012)

    The main headings are File Edit Sketch Tools Help and several shortcut icons beneath

    Verify, Upload, New, Open, Save, and at the far right, the Serial Monitor.Note thatall these icons are also available from the main menus.

    http://www.robotshop.com/arduino-uno-usb-microcontroller-rev-3-2.htmlhttp://www.robotshop.com/arduino-uno-usb-microcontroller-rev-3-2.htmlhttp://www.robotshop.com/arduino-uno-usb-microcontroller-rev-3-2.htmlhttp://www.arduino.cc/http://www.arduino.cc/http://www.arduino.cc/http://arduino.cc/en/Main/Softwarehttp://arduino.cc/en/Main/Softwarehttp://arduino.cc/en/Main/Softwarehttp://www.robotshop.com/Images/small/en/solarbotics-hvwtech-freeduino-usb-microcontroller-T.jpghttp://www.robotshop.com/Images/small/en/dfrduino-uno-usb-arduino-compatible-T.jpghttp://www.robotshop.com/Images/small/en/arduino-uno-microcontroller-2-T.jpghttp://www.robotshop.com/Images/small/en/solarbotics-hvwtech-freeduino-usb-microcontroller-T.jpghttp://www.robotshop.com/Images/small/en/dfrduino-uno-usb-arduino-compatible-T.jpghttp://www.robotshop.com/Images/small/en/arduino-uno-microcontroller-2-T.jpghttp://www.robotshop.com/Images/small/en/solarbotics-hvwtech-freeduino-usb-microcontroller-T.jpghttp://www.robotshop.com/Images/small/en/dfrduino-uno-usb-arduino-compatible-T.jpghttp://www.robotshop.com/Images/small/en/arduino-uno-microcontroller-2-T.jpghttp://arduino.cc/en/Main/Softwarehttp://www.arduino.cc/http://www.robotshop.com/arduino-uno-usb-microcontroller-rev-3-2.html
  • 5/20/2018 Lesson 1-7 Arduino

    3/29

    Older Arduino Interface

    The main headings are File Edit Sketch Tools Help and several shortcut icons beneath

    Verify, Stop, New, Open, Save, Upload and Serial Monitor. Note that all theseicons are also available from the main menus.

    http://www.robotshop.com/blog/en/files/arduino-software-screen.jpg
  • 5/20/2018 Lesson 1-7 Arduino

    4/29

    To connect to your board,

    1. Launch the Arduino software by double-clicking the Arduino icon

    2. Plug one end of the USB into the Arduino and the other end into your computer.3. Your computer should detect the new device and tell you if it has installed correctly. At

    this time, two things can happen; if you have an older board using an FTDI chip (ex.Duemilanove based), Windows should detect it and youre good to go to the next step. Ifyou have a board which uses an ATMega chip to convert USB to serial (for example the

    UNO), you will need toinstall the drivers manually.

    4. Take a look at your boards main processor chip (usually found between the pin headers)to see which you have. It will likely be the ATMega168, ATMega328, or a more

    powerful ATMEga640. ATMega1280 etc

    http://arduino.cc/en/Guide/Windows#toc4http://arduino.cc/en/Guide/Windows#toc4http://arduino.cc/en/Guide/Windows#toc4http://www.robotshop.com/blog/en/files/arduino-default-software-screen.jpghttp://arduino.cc/en/Guide/Windows#toc4
  • 5/20/2018 Lesson 1-7 Arduino

    5/29

    5. In the software, select Tools -> Board -> You will get a list of possible boards. If you

    have a different board, select it from the drop-down list; if you have purchased a

    compatible board, that manufacturer should indicate which board to choose.6. In the software, select Tools -> Serial Port -> COM # (note that if you have several

    COM ports, you will need to go to Device Manager to see which COM port is assigned to

    your board.

    Arduino 5 Minute Tutorials: Lesson 2Basic

    Code & Blink LED

    Posted on April 17, 2012 byColeman Benson& filed underArduino,Electronics,Schools &

    Curriculum,Software and Apps.

    Lessons Menu:

    Lesson 1Software Downloading / Installing & Interface

    Lesson 2Basic Code

    Lesson 3Sensors: Potentiometers

    Lesson 4Sensor: Infrared Distance

    Lesson 5Actuator: Servo Motor

    Lesson 6Sensor: Force, Bend, Stretch

    Lesson 7Sensor: Accelerometer, Gyro, IMU

    Lesson 2 Hardware:

    1. Computer / Laptop or Netbook

    2. Arduino Microcontroller

    3. USB to Serial Adapter(if your microcontroller does not have a USB port)

    http://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/blog/en/files/arduino-tutorial-lesson-2.jpghttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/author/cbenson
  • 5/20/2018 Lesson 1-7 Arduino

    6/29

    4. Appropriate USBcable(Arduino boards draw power from the USB port no batteries

    yet)

    The Arduino language is CASE SENSITIVE: a capital letter is not the same as a lower case

    letter. The following code represents the minimum in order for a program to compile:

    The voidsetup() section is widely used to initialize variables, pin modes, set the serial baud

    rate and related. The software only goes though the section once.

    The voidloop() section is the part of the code that loops back onto itself and is the main part

    of the code. In the Arduino examples, this is called Bare Minimum under File-> Examples ->Basics. Note that you are free to add subroutines using the same syntax:

    voidsubroutinename() {}

    Almost every line of code needs to end with a semicolon ; (there are a few exceptions which

    we will see later). To write single line comments in the code, type two back slashes followed bythe text:

    //comments are overlooked when compiling your program

    To write multi-line comments, start the comment with /* and end with */

    /* This is a multi-line comment and saves you having to always use double slashes at the

    beginning of every line. Comments are used used to explain the code textually. Good code

    always has a lot of comments.*/

    Serial Communication

    TheArduinoboard can communicate at various baud (baud rates). A baud is a measure of how

    many times the hardware can send 0s and 1s in a second. The baud rate must be set properly for

    the board to convert incoming and outgoing information to useful data. If your receiver isexpecting to communicate at a baud rate of 2400, but your transmitter is transmitting at a

    http://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/arduino-microcontroller-modules.htmlhttp://www.robotshop.com/arduino-microcontroller-modules.htmlhttp://www.robotshop.com/arduino-microcontroller-modules.htmlhttp://www.robotshop.com/blog/en/files/arduino-basic-program.bmphttp://www.robotshop.com/arduino-microcontroller-modules.htmlhttp://www.robotshop.com/cables-wires-connectors-en.html
  • 5/20/2018 Lesson 1-7 Arduino

    7/29

    different rate (for example 9600), the data you get will not make sense. To set the baud rate, use

    the following code:

    voidsetup() {

    Serial.begin(9600);

    }

    9600 is a good baud rate to start with. Other standard baud rates available on most Arduino

    modules include: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200and you are free to specify other baud rates. To output a value in the Arduino window, consider

    the following program:

    http://www.robotshop.com/blog/en/files/arduino-hello-world.jpg
  • 5/20/2018 Lesson 1-7 Arduino

    8/29

    Verify the program by pressing the verify button (looks like a play button in order version or

    a check sign in Arduino 1.0); you should not get any errors at the bottom of the screen. If you get

    errors, check that only the two numbers in the code are black, the rest of the text should havebeen automatically recognized and assigned a color. If part of the text is black, check the syntax

    (often copy/pasting text from another program can include unwanted formatting) and

    capitalization.

    Next, upload the sketch to the board using the Upload to I/O Board button (arrow pointing

    right). Wait until the sketch has finished uploading. You will not see anything unless you thenselect the Serial Monitor button (rectangle with a circle that looks like a TV in theold

    software, or what looks like a magnifying glass in the new software). When you select the serial

    monitor, make sure the baud rate selected is the same as in your program. If you want to save all

    your programs, we suggest creating a new folder called reference and save this program asHello World.

    Blink LED Program

    Connect the board to the computer if it is not already connected. In the Arduino software go to

    File -> Examples -> Basics -> Blink LED. The code will automatically load in the window,

    ready to be transferred to the Arduino. Ensure you have the right board chosen in Tools ->Board, and have the right COM port as well under Tools -> Serial Port. If you are not sure which

    COM port is connected to the Arduino, (on a Windows machine) go to Device Manager under

    COM & Ports.

    Press the Upload button and wait until the program says Done Uploading. You should see

    the LED next to pin 13 start to blink. Note that there is already a green LED connected to mostboardsyou dont necessarily need a separate LED.

    Understanding Blink LED Code

  • 5/20/2018 Lesson 1-7 Arduino

    9/29

    From Lesson 2 you will recognize the basic code void setup(){} and void loop(){}. You will also

    recognize the green commented sections. The three new lines of code you have not seen before

    are:

    pinMode(13, OUTPUT);

    This sets pin 13 as an output pin. The opposite, being INPUT, would have the pin wait to read a

    5V signal. Note that the M is capitalized. A lower case m would cause the word pinmode tonot be recognized.

    digitalWrite(13, HIGH); and digitalWrite(13, LOW);

    http://www.robotshop.com/blog/en/files/arduino-blink-led-basic-code.jpg
  • 5/20/2018 Lesson 1-7 Arduino

    10/29

    The line digitalWrite(pin, HIGH); puts a specified pin high to +5V. In this case we chose pin 13

    since on the Uno, the LED is connected to pin 13. Replacing HIGH with LOW, the pin is set to

    0V. You can attach your own LED using a digital output and the GND pin. Note that the W iscapitalized.

    delay(1000);

    The delay(1000); line causes the program to wait for 1000 milliseconds before proceeding

    (where 1000 is just a convenient example to get a 1 second delay). Note that during a delay, themicrocontroller simply waits and does not execute any additional lines of code.

    Special Note

    Pin 13 incorporates a resistor with the LED, whereas none of the other digital pins have this

    feature. If you want to connect one or more LEDs to the other digital pins, you need toadd a

    resistor in series with the LED.

    Arduino 5 Minute Tutorials: Lesson 3

    Potentiometer

    Posted on April 19, 2012 byColeman Benson& filed underArduino,Electronics,Schools &

    Curriculum,Software and Apps.

    Lessons Menu:

    Lesson 1Software Downloading / Installing & Interface

    Lesson 2Basic Code

    http://www.ladyada.net/learn/arduino/lesson3.htmlhttp://www.ladyada.net/learn/arduino/lesson3.htmlhttp://www.ladyada.net/learn/arduino/lesson3.htmlhttp://www.ladyada.net/learn/arduino/lesson3.htmlhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/files/arduino-tutorial-lesson-3.jpghttp://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/author/cbensonhttp://www.ladyada.net/learn/arduino/lesson3.htmlhttp://www.ladyada.net/learn/arduino/lesson3.html
  • 5/20/2018 Lesson 1-7 Arduino

    11/29

    Lesson 3Sensors: Potentiometers

    Lesson 4Sensor: Infrared Distance

    Lesson 5Actuator: Servo Motor

    Lesson 6Sensor: Force, Bend, Stretch

    Lesson 7Sensor: Accelerometer, Gyro, IMU

    Lesson 3 Hardware:

    1. Computer / Laptop or Netbook2. Arduino Microcontroller

    3. USB to Serial Adapter(if your microcontroller does not have a USB port)

    4. Appropriate USBcable(Arduino boards draw power from the USB port no batteriesyet)

    5. Potentiometer(rotary or linear) Example:DFRobot Rotation Sensor

    6. Optional secondary LED.

    Open the sample sketch AnalogInput under File -> Examples -> Analog. The comments

    section has been reduced below to make the code clearly visible.

    http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/linear-rotary-resistors.htmlhttp://www.robotshop.com/linear-rotary-resistors.htmlhttp://www.robotshop.com/dfrobot-rotation-sensor-v1.htmlhttp://www.robotshop.com/dfrobot-rotation-sensor-v1.htmlhttp://www.robotshop.com/dfrobot-rotation-sensor-v1.htmlhttp://www.robotshop.com/Images/small/en/dfrobot-rotation-sensor-v2-T.jpghttp://www.robotshop.com/Images/small/en/dfrobot-slide-position-sensor-T.jpghttp://www.robotshop.com/Images/small/en/dfrobot-rotation-sensor-v1-T.jpghttp://www.robotshop.com/Images/small/en/dfrobot-rotation-sensor-v2-T.jpghttp://www.robotshop.com/Images/small/en/dfrobot-slide-position-sensor-T.jpghttp://www.robotshop.com/Images/small/en/dfrobot-rotation-sensor-v1-T.jpghttp://www.robotshop.com/Images/small/en/dfrobot-rotation-sensor-v2-T.jpghttp://www.robotshop.com/Images/small/en/dfrobot-slide-position-sensor-T.jpghttp://www.robotshop.com/Images/small/en/dfrobot-rotation-sensor-v1-T.jpghttp://www.robotshop.com/dfrobot-rotation-sensor-v1.htmlhttp://www.robotshop.com/linear-rotary-resistors.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3638
  • 5/20/2018 Lesson 1-7 Arduino

    12/29

    We see several new lines of code here:

    intsensorPin = A0;

    The int is short for integer. The name sensorPin was chosen only to describe what the

    variable represents; sensor pin. The fact that the P is capitalized makes it easier to see that it

    is actually two words, since spaces cannot be used. The integer sensor pin is equal to A0,

    http://www.robotshop.com/blog/en/files/arduino-potentiometer-led-code.jpg
  • 5/20/2018 Lesson 1-7 Arduino

    13/29

    where A0 is Analog pin 0 on the Arduino. On its own, A0 is not a reserved term. However,

    when used in context, the system recognizes it as analog pin 0. The line must be ended with a

    semicolon. By declaring a variable in the setup, you can use the term, which in this case issensorPin, throughout the code instead of A0. There are two main benefits to this:

    1) It makes the code more descriptive

    2) If you want to change the value of the variable, you only need to do it in one place.

    sensorValue = analogRead(sensorPin);

    This line uses the term analogRead in order to read the voltage of an analog pin. Most Arduino

    microcontrollers use 10 bit analog (voltage) to digital (numeric) conversion, which is 210

    possible

    numbers = 1024. Therefore a voltage of 0V corresponds to a numeric value of 0. A voltage of 5V

    corresponds to a numeric value of 1024. Therefore a value of 3V would correspond to a numericvalue of:

    3/5 =x/1024, x = 3*1024/5 = ~614

    Alternatively you could have written: sensorValue = analogRead(A0);

    intledPin = 13;

    Once again, the term ledPin is not a reserved word in Arduino, it was chosen to describe which

    pin was connected to the LED. The value 13 is a normal value, but just like A0, whenused

    in context represents pin 13.

    intsensorValue = 0;

    The term sensorValue is not a reserved term either.

    Connect the potentiometer to pins A0, 5V and GND. The middle (wiper) lead is the one to

    connect to the analog pin and the voltage varies on this pin. The orientation of the other two pins

    does not matter. The other option is to connect the potentiometer to pins A0, A1 and A2.However, you will need to add the following code under void setup():

    digitalWrite(A1, LOW);

    digitalWrite(A2, HIGH);

    This sets the corresponding pins to 0V (GND) and 5V (PWR). Once thepotentiometeris

    connected, upload this sketch to the board and change to the Serial monitor. As you rotate the

    knob (or slide the slider), the values should change between 0 to 1023. Correspondingly, theLED will blink with a faster or shorter delay.

    http://www.robotshop.com/linear-rotary-resistors.htmlhttp://www.robotshop.com/linear-rotary-resistors.htmlhttp://www.robotshop.com/linear-rotary-resistors.htmlhttp://www.robotshop.com/linear-rotary-resistors.html
  • 5/20/2018 Lesson 1-7 Arduino

    14/29

    You can now read values and use them within your code. The new function used here is

    analogRead(); where the pin selected is pin #2. If you used analog pin #5, you would change

    the code to read:

    int sensorpin = 5;

    If the system does not work, check the syntax and ensure the code uploads correctly. Next, check

    the connections to the potentiometer ensuring that the middle lead goes to the correct pin, and the

    other pins are powered at 0V and 5V. If you bought a very cheap or old potentiometer, there is achance it may be mechanically defective. You can test this using amultimeterand connect the

    ends to the middle pin and an outer pin. Set the multimeter to read resistance and rotate the knob;

    if the resistance changes slowly, the pot is working. If the resistance is erratic, you need a newpotentiometer.

    Now what if you want to see the value yourself? Take a look at the code above and write it in the

    Arduino interface as a new sketch. Some new code you will see is:

    http://www.robotshop.com/productinfo.aspx?pc=RB-Cyt-99&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Cyt-99&lang=en-UShttp://www.robotshop.com/productinfo.aspx?pc=RB-Cyt-99&lang=en-UShttp://www.robotshop.com/blog/en/files/analog-serial.jpghttp://www.robotshop.com/productinfo.aspx?pc=RB-Cyt-99&lang=en-US
  • 5/20/2018 Lesson 1-7 Arduino

    15/29

    Serial.println(sensorValue);

    This sends the value contained in the variable sensorValue serially via the USB plug anddigital pin 1. Verify, then upload this sketch to your Arduino. Once it is done, press on the

    magnifying glass located towards the top right of the window. This is the Serial monitor and

    monitors communications being sent and received by the Arduino. Here you must verify that theBaud rate is also 9600, or else you will see garbage.

    If you did not want the values to appear on a new line every time, you could write

    Serial.print(sensorValue);

    Arduino 5 Minute Tutorials: Lesson 4IR

    Distance Sensor & Push Button

    Posted on April 20, 2012 byColeman Benson& filed underArduino,Electronics,Schools &

    Curriculum,Software and Apps.

    Lessons Menu:

    Lesson 1Software Downloading / Installing & Interface

    Lesson 2Basic Code

    Lesson 3Sensors: Potentiometers

    Lesson 4Sensor: Infrared Distance

    Lesson 5Actuator: Servo Motor

    Lesson 6Sensor: Force, Bend, Stretch

    Lesson 7Sensor: Accelerometer, Gyro, IMU

    http://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/files/arduino-tutorial-lesson-4.jpghttp://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/author/cbenson
  • 5/20/2018 Lesson 1-7 Arduino

    16/29

    Lesson 4 Hardware:

    1. Computer / Laptop or Netbook

    2. Arduino Microcontroller3. USB to Serial Adapter(if your microcontroller does not have a USB port)

    4.

    Appropriate USBcable(Arduino boards draw power from the USB port

    no batteriesyet)

    5. IR Distance sensor(preferablySharp)and corresponding cable

    6. Push buttonand corresponding cables to connect to Arduino

    Infrared distance sensors are useful for measuring distances without actually touching a surface.The three wires protruding from a distance sensor represent +5V (in most cases), Gnd (Ground)

    and signal. These are almost always color coded with black as ground, red as +V and white or

    yellow as the signal. If your infrared distance sensor did not come with any wires, you will either

    need to find the appropriate connector, orsolderwires directly to the leads (ensure the pins andsolder do not contact one another) so you can attach wires.

    1. Connect the red wire to +5V on theArduino

    2. Connect the black wire to Gnd on the Arduino

    3. Connect the yellow wire to an analog pin on the Arduino (in this case we chose A2)

    Arduino 5 Minute Tutorials

    Since the sensor is connected to the analog input of the Arduino, the code is identical to that of

    thepotentiometer:

    http://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/infrared-light-sensors.htmlhttp://www.robotshop.com/infrared-light-sensors.htmlhttp://www.robotshop.com/sharp-gp2y0a21yk0f-ir-range-sensor-2.htmlhttp://www.robotshop.com/sharp-gp2y0a21yk0f-ir-range-sensor-2.htmlhttp://www.robotshop.com/sharp-gp2y0a21yk0f-ir-range-sensor-2.htmlhttp://www.robotshop.com/contact-sensors.htmlhttp://www.robotshop.com/contact-sensors.htmlhttp://www.robotshop.com/home/products/tools-equipment/soldering/index.htmlhttp://www.robotshop.com/home/products/tools-equipment/soldering/index.htmlhttp://www.robotshop.com/home/products/tools-equipment/soldering/index.htmlhttp://www.robotshop.com/arduino-microcontroller-modules.htmlhttp://www.robotshop.com/arduino-microcontroller-modules.htmlhttp://www.robotshop.com/arduino-microcontroller-modules.htmlhttp://www.robotshop.com/linear-rotary-resistors.htmlhttp://www.robotshop.com/linear-rotary-resistors.htmlhttp://www.robotshop.com/linear-rotary-resistors.htmlhttp://www.robotshop.com/blog/en/files/arduino-sharp-infrared-sensor.jpghttp://www.robotshop.com/linear-rotary-resistors.htmlhttp://www.robotshop.com/arduino-microcontroller-modules.htmlhttp://www.robotshop.com/home/products/tools-equipment/soldering/index.htmlhttp://www.robotshop.com/contact-sensors.htmlhttp://www.robotshop.com/sharp-gp2y0a21yk0f-ir-range-sensor-2.htmlhttp://www.robotshop.com/infrared-light-sensors.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.html
  • 5/20/2018 Lesson 1-7 Arduino

    17/29

    Upload this program to the board and change to the Serial Monitor. As you move the front of thedistancesensorcloser to and away from a solid object or wall, the values should change between

    0 to 1023. You can now read values and use them within your code. Check the range for yoursensor(not all sensors can read from zero cm); note that some sensors have a minimum distance

    although it is always listed in the specifications, try to find it by experimentation. To convert

    the values to actual distances (in cm or inches), consult the user guide of the sensor.

    Arduino and Push Buttons

    Connecting toggle switches, push buttons and momentarycontact switchesto the Arduino isstraightforward. A push button is a simple device that completes a circuit. One end of the button

    is connected to source, usually a low voltage (5V on the Arduino is ideal) and the other

    connected to the digital pin. When the switch is flipped, pressed or toggled, the circuit is eitheropened or closed. The digital pin simply returns if there is 5V or 0V. The code associated with

    this is:

    http://www.robotshop.com/sensors.htmlhttp://www.robotshop.com/sensors.htmlhttp://www.robotshop.com/sensors.htmlhttp://www.robotshop.com/sensors.htmlhttp://www.robotshop.com/sensors.htmlhttp://www.robotshop.com/home/products/robot-parts/sensors/contact-sensors/index.htmlhttp://www.robotshop.com/home/products/robot-parts/sensors/contact-sensors/index.htmlhttp://www.robotshop.com/home/products/robot-parts/sensors/contact-sensors/index.htmlhttp://www.robotshop.com/blog/en/files/arduino-potentiometer-simple1.jpghttp://www.robotshop.com/home/products/robot-parts/sensors/contact-sensors/index.htmlhttp://www.robotshop.com/sensors.htmlhttp://www.robotshop.com/sensors.html
  • 5/20/2018 Lesson 1-7 Arduino

    18/29

    digitalRead(pin);

    In the following simple program, a push button is used to turn on theLEDconnected to pin 13.The line

    digitalWrite(ledPin, status);

    turns the ledPin (in this case assigned to digital pin 13) HIGH (1) or LOW (0) depending on thestatus variable. We initially set the status to be low (0).

    http://www.robotshop.com/home/products/robot-parts/lighting/index.htmlhttp://www.robotshop.com/home/products/robot-parts/lighting/index.htmlhttp://www.robotshop.com/home/products/robot-parts/lighting/index.htmlhttp://www.robotshop.com/blog/en/files/arduino-push-button.jpghttp://www.robotshop.com/home/products/robot-parts/lighting/index.html
  • 5/20/2018 Lesson 1-7 Arduino

    19/29

    Arduino 5 Minute Tutorials: Lesson 5

    Servo Motors

    Posted on April 25, 2012 byColeman Benson& filed underArduino,Electronics,Schools &Curriculum,Software and Apps.

    Lessons Menu:

    Lesson 1Software Downloading / Installing & Interface

    Lesson 2Basic Code

    Lesson 3Sensors: Potentiometers

    Lesson 4Sensor: Infrared Distance

    Lesson 5Actuator: Servo Motor

    Lesson 6Sensor: Force, Bend, Stretch

    Lesson 7Sensor: Accelerometer, Gyro, IMU

    Lesson 5 Hardware:

    1.

    Computer / Laptop or Netbook

    2.

    Arduino Microcontroller

    3.

    USB to Serial Adapter(if your microcontroller does not have a USB port)

    4.

    Appropriate USBcable(Arduino boards draw power from the USB port no batteries yet)

    5.

    Standard servo motor (current consumption

  • 5/20/2018 Lesson 1-7 Arduino

    20/29

    1.

    Connect the black wire from the servo to the GND pin on the Arduino

    2.

    Connect the red wire from the servo to the +5V pin on the Arduino

    3.

    Connect the yellow or white wire from the servo to a digital pin on the Arduino

    Alternatively, you can plug the servos wire into three adjacent pins, and set the pin connected tothe red lead to HIGH and the pin connected to the black lead to LOW. If you want to use a

    more powerful servo, or if you want to connect it to a separate power supply, you would connect

    the battery / power supplys red (5V) and black (GND) wires to the servos red and black wires,and connect the signal wire to the Arduino. Note that you also need to connect the batters GND

    line to the Arduinos GND pins (common ground).

    http://www.robotshop.com/blog/en/files/arduino-servo-motor.jpghttp://www.robotshop.com/blog/en/files/arduino-servo-motor.jpghttp://www.robotshop.com/blog/en/files/arduino-servo-motor.jpg
  • 5/20/2018 Lesson 1-7 Arduino

    21/29

    pinMode(pin number, OUTPUT);

    This sets a pin number as dedicated input or output. In this case, we called the pin servopin and

    assigned it a value of 4. The term pulse is in black as it is not a reserved word and can be

    changed by the user. It is best to use descriptive variables when coding to understand what each

    does, or the information it will contain. Servos operate by sending a timed +5V pulse (usuallybetween 500us and 2500us) to the onboard electronics, which is repeated every ~20ms. This

    pulse corresponds to aservoposition, usually from 0 to 180 degrees.

    5V for 500 microseconds = 0.5 milliseconds and corresponds to 0 degrees

    5V for 1500 microseconds = 1.5 milliseconds and corresponds to 90 degrees

    5V for 2500 microseconds = 2.5 milliseconds and corresponds to 180 degrees

    The relationship is linear, so use mathematics to determine the pulse which corresponds to a

    given angle. Note that if you send a signal that is greater or lower than the servo can accept (for

    example, Firgelli linear actuators accept 1 to 2 ms), you might damage the actuator.

    http://www.robotshop.com/servo-motors.htmlhttp://www.robotshop.com/servo-motors.htmlhttp://www.robotshop.com/servo-motors.htmlhttp://www.robotshop.com/blog/en/files/arduino-servo-motor-code.jpghttp://www.robotshop.com/servo-motors.html
  • 5/20/2018 Lesson 1-7 Arduino

    22/29

    Another option for controlling servos is to use the Arduino servo library (previously separate

    from the basic Arduino software, it is now included with V1.0). The servo library manages much

    of the overhead and includes new, custom commands. If you want to control multiple servomotors, you should use a servo motor controller and a separate power supply between 4.8V to

    6V.

    Arduino 5 Minute Tutorials: Lesson 6

    Force, Bend, Stretch Sensors

    Posted on April 30, 2012 byColeman Benson& filed underArduino,Electronics,Schools &Curriculum,Software and Apps.

    Lessons Menu:

    Lesson 1Software Downloading / Installing & Interface

    Lesson 2Basic Code

    Lesson 3Sensors: Potentiometers

    Lesson 4Sensor: Infrared Distance

    Lesson 5Actuator: Servo Motor

    Lesson 6Sensor: Force, Bend, Stretch

    Lesson 7Sensor: Accelerometer, Gyro, IMU

    Lesson 6 Hardware:

    1.

    Computer / Laptop or Netbook

    2.

    Arduino Microcontroller

    3.

    USB to Serial Adapter(if your microcontroller does not have a USB port)

    http://www.arduino.cc/playground/uploads/ComponentLib/SoftwareServo.ziphttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/blog/en/files/arduino-tutorial-lesson-6.jpghttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3639http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/electronicshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/files/arduino-servo-motor.jpghttp://www.arduino.cc/playground/uploads/ComponentLib/SoftwareServo.zip
  • 5/20/2018 Lesson 1-7 Arduino

    23/29

    4.

    Appropriate USBcable(Arduino boards draw power from the USB port no batteries yet)

    5.

    Standard servo motor (current consumption

  • 5/20/2018 Lesson 1-7 Arduino

    24/29

    The output of this mini circuit is a signal between 0 to 5V (this is referred to as an analog

    signal), which is connected to an analog pin of themicrocontroller.The microcontrollers on-

    board analog to digital converter (ADC) interprets this voltage and assigns it a number whichyou can use in your code. For 10 bit ADC (2

    10), you will get a number between 0 and 1024

    representing 0V to 5V. You would need an equation in your code to use this number to send the

    appropriate signal to a motor controller. As you might have suspected, the code is now identical

    to that used to get an analog input.

    To get sample code, open the Arduino software and go to File -> Examples -> Analog ->AnalogInOutSerial

    The video above shows a bend sensor connected to anArduino,and the Arduino is connected to

    a smallservo motor.The analog value associated with the flex sensor is read by the Arduino, and

    that value is converted to a rough position. You would merge the Analog example code with theservo code, and add a single line to convert the 0 to 1024 value to 0 to 180 degrees. It is easy to

    see how, with many of these sensors, you can create a data glove which controls a robotic hand.

    http://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://robotshop.helpserve.com/staff/Tickets/Manage/Filter/17/4/-1http://robotshop.helpserve.com/staff/Tickets/Manage/Filter/17/4/-1http://robotshop.helpserve.com/staff/Tickets/Manage/Filter/17/4/-1http://www.robotshop.com/servo-motors.htmlhttp://www.robotshop.com/servo-motors.htmlhttp://www.robotshop.com/servo-motors.htmlhttp://www.robotshop.com/Images/big/en/anthrotronix-acceleglove-sensor-feedback-glove-1.jpghttp://www.robotshop.com/productinfo.aspx?pc=RB-Phi-50&lang=en-UShttp://www.robotshop.com/Images/big/en/anthrotronix-acceleglove-sensor-feedback-glove-1.jpghttp://www.robotshop.com/productinfo.aspx?pc=RB-Phi-50&lang=en-UShttp://www.robotshop.com/servo-motors.htmlhttp://robotshop.helpserve.com/staff/Tickets/Manage/Filter/17/4/-1http://www.robotshop.com/microcontrollers.html
  • 5/20/2018 Lesson 1-7 Arduino

    25/29

    Arduino 5 Minute Tutorials: Lesson 7

    Accelerometers, Gyros, IMUs

    Posted on May 2, 2012 byColeman Benson& filed underArduino,Schools & Curriculum,Software and Apps.

    Lessons Menu:

    Lesson 1Software Downloading / Installing & Interface

    Lesson 2Basic Code

    Lesson 3Sensors: Potentiometers

    Lesson 4Sensor: Infrared Distance

    Lesson 5Actuator: Servo Motor

    Lesson 6Sensor: Force, Bend, Stretch

    Lesson 7Sensor: Accelerometer, Gyro, IMU

    Lesson 8Actuator: DC Motor

    Lesson 9more to come

    Lesson 7 Hardware:

    1.

    Computer / Laptop or Netbook

    2.

    Arduino Microcontroller

    3.

    USB to Serial Adapter(if your microcontroller does not have a USB port)4.

    Appropriate USBcable(Arduino boards draw power from the USB port no batteries yet)

    5.

    Analog accelerometer, gyroscope and/or IMU

    6.

    Connectors (between the IMU and the Arduino

    Accelerometers, gyroscopes and IMUs are incredibly useful little sensors which are being

    integrated more and more into the electronics devices around us. These sensors are used in cell

    phones, gaming consoles such as the Wii wireless remote control, toys, self-balancing robots,

    http://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/author/cbensonhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/articles/arduino-5-minute-tutorials-lesson-2-basic-codehttp://www.robotshop.com/blog/en/articles/arduino-5-minute-tutorials-lesson-2-basic-codehttp://www.robotshop.com/blog/en/articles/arduino-5-minute-tutorials-lesson-2-basic-codehttp://www.robotshop.com/blog/en/articles/arduino-5-minute-tutorials-lesson-2-basic-codehttp://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/blog/en/files/arduino-tutorial-lesson-7.jpghttp://www.robotshop.com/cables-wires-connectors-en.htmlhttp://www.robotshop.com/ttl-cmos-serial.htmlhttp://www.robotshop.com/arduino-microcontrollers-1.htmlhttp://www.robotshop.com/blog/en/?p=3634http://www.robotshop.com/blog/en/?p=3635http://www.robotshop.com/blog/en/?p=3636http://www.robotshop.com/blog/en/?p=3637http://www.robotshop.com/blog/en/?p=3638http://www.robotshop.com/blog/en/articles/arduino-5-minute-tutorials-lesson-2-basic-codehttp://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-1-software-3640http://www.robotshop.com/blog/en/robots/gorobotics/tutorials/software-appshttp://www.robotshop.com/blog/en/robots/gorobotics/schools-curriculumhttp://www.robotshop.com/blog/en/robots/gorobotics/tutorials/arduinohttp://www.robotshop.com/blog/en/author/cbenson
  • 5/20/2018 Lesson 1-7 Arduino

    26/29

    motion capture suits and more. Accelerometers are used mainly to measure acceleration and tilt,

    gyroscopes are used to measure angular velocity and orientation and IMUs (which combine both

    accelerometers and gyroscopes) are used to give a complete understanding of a devicesacceleration, speed, position, orientation and more.

    When choosing an accelerometer, gyroscope or IMU, it is also important to consider the type of

    output; depending on the type of sensor, readings can be output as:

    Serial data (Tx pin)

    I2C (SDA, SCL)

    Analog

    TTL

    others

    In this tutorial were only going to cover analog output. The code shown below includes theoutput for a single axis sensor and factors in the rest value.

    http://www.robotshop.com/blog/en/files/arduino-accelerometer-connections.jpg
  • 5/20/2018 Lesson 1-7 Arduino

    27/29

    Accelerometer

    Accelerometers measure acceleration in one to three linear axes (x, y, z). A single axisaccelerometer can measure acceleration in whichever direction it is pointed. This may be good

    for a rocket, an impact, a train or other scenario where the device really moves in one basic

    direction. Knowing the acceleration and time, you can use mathematics tofind the distancetraveled by the object.There are fewer and fewer single and double axis accelerometers on the

    market because a triple axis accelerometer can do so much more. Thanks to low manufacturing

    costs the three axes accelerometers are not much more expensive than single or double.

    http://www.robotshop.com/sensors-accelerometers.htmlhttp://www.robotshop.com/sensors-accelerometers.htmlhttp://www2.usfirst.org/2005comp/Manuals/Acceler1.pdfhttp://www2.usfirst.org/2005comp/Manuals/Acceler1.pdfhttp://www2.usfirst.org/2005comp/Manuals/Acceler1.pdfhttp://www2.usfirst.org/2005comp/Manuals/Acceler1.pdfhttp://www.robotshop.com/blog/en/files/arduino-accelerometer-gyroscope-imu-code.jpghttp://www2.usfirst.org/2005comp/Manuals/Acceler1.pdfhttp://www2.usfirst.org/2005comp/Manuals/Acceler1.pdfhttp://www.robotshop.com/sensors-accelerometers.html
  • 5/20/2018 Lesson 1-7 Arduino

    28/29

    Acceleration due to gravity is a constant and is in fact measurable using an accelerometer. When

    placed parallel to the ground, acceleration due to gravity would only be felt by one axis.

    However, when tilted, this acceleration would appear as components of two (or three) axes. Youcan get an idea of how to use an accelerometer to measure tilthereandhere.

    Connect theaccelerometerto theArduino;each output pin goes to one of the analog pins on theArduino, the Vin pin goes to the 5V pin on the Arduino (read the user guide to ensure the Vin

    pin is 5V as opposed to 3.3V), and connect the GND pin to the GND pin on the Arduino. Note

    that there is no need for additional electronics! Next, open the sample sketch File -> Examples ->Sensors -> ADXL3xx. Upload to the Arduino and see the values change.

    In order to choose the right accelerometer, consider the maximum linear acceleration the sensorwill be subjected to. If you are planning to add an accelerometer to a small mobile robot, you

    will likely use a 2g accelerometer (even that is likely overkill), whereas if you are attaching it to

    a rocket, a 16g accelerometer is likely a better choice. When connected to a 10 bit ADC, the 2g

    accelerometer will have an accuracy of 2 / 1024 = 0.002g, and the 16g accelerometer will have

    and accuracy of 16 / 1024 = 0.0156. Therefore if you only need a range of 2g, but purchase a 16gaccelerometer, you will only have about 128 possible readings, instead of the full 1024.

    Conversely, if you choose a 2g accelerometer when you really needed a 16g, you will get a lot ofmaximum (1024) readings since the acceleration is off the scale.

    Gyroscope

    Gyroscopesmeasure angular velocity in , , (see image below). Gyroscopes can be used to

    help with stabilization and well as changes in direction and orientation. Unlike accelerometers,

    gyroscopes do not have a fixed reference, and only measure changes. To choose the rightgyroscope for your needs, consider the maximum angular rate of change (degrees per second)

    your product will be subjected to. A remote control will likely rotate at less than 1 rotation perminute (360 degrees per second), while a rocket tumbling out of the sky may be rotating at 1500

    degrees per second. When connected to the samemicrocontroller(10 bit for example), the 360degree/s gyro will have an accuracy of 360 / 1024 = 0.35 deg/s, whereas the 1500 deg/s gyro will

    have an accuracy of 1500 / 1024 = 1.46 deg/s. Therefore if you chose a 1500 deg/s gyro when

    you only needed a 360 deg/s gyro, you will only get about 245 readings as opposed to 1024.

    http://www.freescale.com/files/sensors/doc/app_note/AN3107.pdfhttp://www.freescale.com/files/sensors/doc/app_note/AN3107.pdfhttp://www.freescale.com/files/sensors/doc/app_note/AN3107.pdfhttp://www.freescale.com/files/sensors/doc/app_note/AN3461.pdfhttp://www.freescale.com/files/sensors/doc/app_note/AN3461.pdfhttp://www.freescale.com/files/sensors/doc/app_note/AN3461.pdfhttp://www.robotshop.com/sensors-accelerometers.htmlhttp://www.robotshop.com/sensors-accelerometers.htmlhttp://www.robotshop.com/sensors-accelerometers.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/sensors-gyroscopes.htmlhttp://www.robotshop.com/sensors-gyroscopes.htmlhttp://www.robotshop.com/sensors-gyroscopes.htmlhttp://www.robotshop.com/sensors-gyroscopes.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/sensors-gyroscopes.htmlhttp://www.robotshop.com/sensors-gyroscopes.htmlhttp://www.robotshop.com/arduino-2.htmlhttp://www.robotshop.com/sensors-accelerometers.htmlhttp://www.freescale.com/files/sensors/doc/app_note/AN3461.pdfhttp://www.freescale.com/files/sensors/doc/app_note/AN3107.pdf
  • 5/20/2018 Lesson 1-7 Arduino

    29/29

    Courtesy: Wikipedia

    IMU

    An IMU (Inertial Measurement Unit) usually consists of an accelerometer and gyroscope and is

    used to measures an objects orientation, velocity etc. Often additional sensors (magnetic,

    temperature) are included to improve accuracy. The number of degrees of freedom indicatesthe number of different axes measured by the chip. For example, combining a three axis

    accelerometer with a two axis gyroscope would be consider a 3+2 = 5 DoF IMU.

    Additional Considerations

    When using accelerometers, gyroscopes or inertial measurement units (IMUs) to obtain positionsin space, it is important to note that there are several additional factors that will affect the

    readings, the main obstacle being the sampling rate.Microcontrollersrequire a certain amount of

    time to read values being provided to them by thesensor,and because of this, the values between

    these readings are lost. There are several mathematical methods (a Kalman filter being a popularchoice) that attempt to compensate for this. A second source of error is that readings are often

    affected by fluctuations in temperature. Most datasheets associated with micro-electro-

    mechanical systems (MEMS) attempt to describe how temperature affects the output.

    http://www.robotshop.com/sensors-imu.htmlhttp://www.robotshop.com/sensors-imu.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/sensors.htmlhttp://www.robotshop.com/sensors.htmlhttp://www.robotshop.com/sensors.htmlhttp://www.robotshop.com/blog/en/files/euler-angles1.jpghttp://www.robotshop.com/sensors.htmlhttp://www.robotshop.com/microcontrollers.htmlhttp://www.robotshop.com/sensors-imu.html