$30 off During Our Annual Pro Sale. View Details »

Intro to Arduino - RPI Embedded Hardware Club (10 OCT 2013)

Theo Pak
October 10, 2013

Intro to Arduino - RPI Embedded Hardware Club (10 OCT 2013)

RPI Embedded Hardware Club
http://rpiEHC.org

Intro to Arduino Workshop
Held October 10, 2013 @ 7-9pm in Sage 2202
Open to campus

Contact Theo Pak
[email protected]

Theo Pak

October 10, 2013
Tweet

More Decks by Theo Pak

Other Decks in How-to & DIY

Transcript

  1. Arduino Nano – $15 each

    View Slide

  2. Please install the Arduino
    software. You can find it at
    arduino.cc/en/Main/Software

    View Slide

  3. JOIN THE MAILING LIST

    View Slide

  4. EMBEDDED HARDWARE CLUB
    We are a non-profit organization
    made of students at Rensselaer
    Polytechnic Institute who share
    a passion for microcontrollers,
    electronics, and programming.

    View Slide

  5. View Slide

  6. INTRO TO ARDUINO
    rpiEHC.org | October 10, 2013

    View Slide

  7. Theo
    Lincoln
    Matt
    Anthony
    Ethan
    Soraya
    ELECTED CLUB OFFICERS
    [email protected]

    View Slide

  8. 1.GETTING STARTED
    2. UPLOADING CODE
    3.DIGITAL I/O
    4.ANALOG I/O

    View Slide

  9. 1.GETTING STARTED
    2. UPLOADING CODE
    3.DIGITAL I/O
    4.ANALOG I/O

    View Slide

  10. • Easier to learn than alternatives.
    • Widely available for cheap!
    • Tons of code already exists.
    • Generally appropriate for real-time.
    • Other options have better specs.
    • Other options use lower power.
    WHY USE AN ARDUINO?

    View Slide

  11. TYPES OF ARDUINOS
    Nano $15
    ATmega328p
    small, cheap,
    breadboardable
    Leonardo $20
    ATmega32u4
    cheap, more GPIO
    Du
    32-
    tons

    View Slide

  12. TYPES OF ARDUINOS
    Due $50
    32-bit ARM
    tons of GPIO
    Yún $70
    dual processors
    Linux, WiFi, etc.
    o $20
    2u4
    e GPIO

    View Slide

  13. INSTALL THE ENVIRONMENT
    An Integrated Development Environment
    includes a code editor, compiler, and
    debugger. You need all three!
    The Arduino Team makes an IDE.
    Download it at http://arduino.cc

    View Slide

  14. 1.GETTING STARTED
    2. UPLOADING CODE
    3.DIGITAL I/O
    4.ANALOG I/O

    View Slide

  15. PROGRAMMING 101
    /* These comments describe code,
    but don't affect the output. */
    void setup () {
    // This part runs once
    }
    void loop () {
    // This part runs infinitely
    }

    View Slide

  16. SELECT THE RIGHT BOARD
    Auto Format
    Archive Sketch
    Fix Encoding & Reload
    Serial Monitor
    Board
    Serial Port
    Programmer
    Burn Bootloader
    Arduino Uno
    Arduino Duemilanove
    Arduino Diecimila
    Arduino Nano /atmega328
    Arduino Something else
    Arduino Mega
    Arduino Leonano
    Powahduino
    Tools >>

    View Slide

  17. SELECT THE RIGHT PORT
    Tools >> Auto Format
    Archive Sketch
    Fix Encoding & Reload
    Serial Monitor
    Board
    Serial Port
    Programmer
    Burn Bootloader
    COM3
    COM4
    COM6
    /dev/ttyUSB1
    /dev/ttyCOM1
    So many Arduinos
    Arduino Leonano

    View Slide

  18. UPLOAD A TEST PROGRAM!
    The Arduino
    Software is an IDE.
    Use it to write your
    code.
    Press the buttons to
    compile or
    compile+upload.

    View Slide

  19. UPLOAD A TEST PROGRAM!
    Press the buttons to
    compile or
    compile+upload.
    The arrow button is
    "Compile+upload."
    That's how you run
    programs on your
    Arduino.

    View Slide

  20. HOW TO PRINT A MESSAGE
    void setup () {
    // Begin serial communication
    // at 9600 BAUD
    Serial.begin(9600);
    }
    void loop () {
    // Print the line "hello world"
    Serial.println("hello world");
    }

    View Slide

  21. THE SERIAL MONITOR
    Arduino Version 1.0 Arduino Version Before 1.0
    Open the serial Monitor

    View Slide

  22. WTF IS A BAUD RATE?

    View Slide

  23. 1.GETTING STARTED
    2. UPLOADING CODE
    3.DIGITAL I/O
    4.ANALOG I/O

    View Slide

  24. It's not made of food!
    Breadboards let you
    quickly build a circuit
    without using any tools.
    WHAT'S A BREADBOARD?

    View Slide

  25. View Slide

  26. DIGITAL OUTPUT: PLUG IN THE LED
    Switch on D5, LED on D13

    View Slide

  27. FLASH AN LED
    void setup () {
    pinMode(13,OUTPUT);
    }
    void loop () {
    // Turn LED on
    digitalWrite(13,HIGH);
    delay(100); // Wait 100 milliseconds
    // Turn LED off
    digitalWrite(13,LOW);
    delay(100); // Wait 100 milliseconds
    }

    View Slide

  28. PUSHBUTTONS AS INPUT
    void setup () {
    pinMode(13,OUTPUT); // Configure Pin13 as output
    pinMode(5,INPUT); // Configure Pin5 as input
    }
    void loop () {
    // If the button is pressed...
    if (digitalRead(5)) {
    // ...then turn LED on
    digitalWrite(13,HIGH);
    } else {
    // ...if not, the turn LED off
    digitalWrite(13,LOW);
    }
    }

    View Slide

  29. 1.GETTING STARTED
    2. UPLOADING CODE
    3.DIGITAL I/O
    4.ANALOG I/O

    View Slide

  30. POTENTIOMETERS AS INPUT
    void setup () {
    // Configure the serial monitor
    Serial.begin(9600);
    // We'll use the default settings for Analog1
    }
    void loop () {
    // read the 10 bit value from Analog1
    int value = analogRead(1);
    // write the value to serial
    Serial.println(value);
    }

    View Slide

  31. MORE ABOUT ANALOG INPUT
    void setup () {
    Serial.begin(9600); // Begin serial communication
    pinMode(A0,OUTPUT); // A0 (Pin14)
    digitalWrite(A0,LOW); // Start Pin14 LOW
    pinMode(A2,OUTPUT); // Pin A2 (Pin16)
    digitalWrite(A2,HIGH); // Start A2 HIGH
    }
    Analog pins 0-5 can be used as digital pins, if configured.
    This trick makes hooking up the pot easier in this example.

    View Slide

  32. NOW BACK TO POTS
    void setup () {
    Serial.begin(9600);
    pinMode(14,OUTPUT);
    digitalWrite(14,LOW);
    pinMode(16,OUTPUT);
    digitalWrite(16,HIGH);
    }
    void loop () {
    // read the 10 bit value from Analog 1
    int value = analogRead(1);
    // write the value to serial
    Serial.println(value);
    }

    View Slide

  33. 1.GETTING STARTED
    2. UPLOADING CODE
    3.DIGITAL I/O
    4.ANALOG I/O

    View Slide

  34. 5.BONUS!

    View Slide

  35. ALL ABOUT PWM
    Voltage vs. Time: The PW
    defines a servo position.

    View Slide

  36. PULSE-WIDTH MODULATION
    void setup() {
    pinMode(3,OUTPUT);
    }
    void loop() {
    // send pulses to pin 3
    // 128 of 256 should be high
    analogWrite(3,128);
    }

    View Slide

  37. PWM WITH FADING
    void setup() {
    pinMode(3,OUTPUT);
    }
    void loop() {
    for (int i = 0; i < 256; i++) {
    // send pulses to pin 3
    analogWrite(3,i);
    // delay each loop
    delay(100);
    }
    }

    View Slide

  38. PPM FOR SERVO CONTROL
    #include
    Servo myservo; //create servo object to control a servo
    void setup () {
    myservo.attach(9); //attach pin 9 to the servo
    object
    }
    void loop () {
    for (int i = 0; i < 255; i ++) {
    // set the servo position
    myservo.write(i);
    // wait for the servo to get there
    delay(15);
    }
    }

    View Slide

  39. PPM FOR SERVO CONTROL

    View Slide

  40. SERIAL INPUT
    String inputString = ""; // a string to hold incoming data
    boolean stringComplete = false; // whether the string is complete
    void setup() {
    // initialize serial:
    Serial.begin(9600);
    // reserve 200 bytes for the inputString:
    inputString.reserve(200);
    }
    void loop() {
    // print the string when a newline arrives:
    if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
    }
    }

    View Slide

  41. SERIAL EVENT
    void serialEvent() {
    while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
    stringComplete = true;
    }
    }
    }

    View Slide

  42. QUESTIONS?
    RPI Embedded Hardware Club
    http://rpiEHC.org
    Intro to Arduino Workshop
    Thursday, October 10, 2013
    Theo Pak

    View Slide