Getting Started

The Quest

Control a servo using the Arduino.

Materials

Overview

  1. Start downloading Arduino software
  2. Attach pigtails to carrier board, put Arduino on board, connect battery to board via pigtails.
  3. Connect servo to board
  4. Connect Arduino to computer via USB cable, install Arduino software and write code for running a servo
  5. Upload code to Arduino, flick switch, and run servo
  6. Profit

Hardware Steps

DO NOT EVER CONNECT BATTERY STRAIGHT TO ARDUINO. Don’t learn this the hard way like me .___.
  1. Connect the battery to the carrier board via the pigtails. Follow instructions @ Power Input (7-12V).
    1. End result:
  2. Take a servo (e.g. Vigor 2S-A or SpringRC Continuous), connect it to D2O (digital out pin 2) with the black wire closest to outside (see below, which has a servo connected to D20 and another connected to D30)
     
    • Black: ground, Red: 5V, White: signal. See RC Interface for more information.

Software Steps

  1. Download, decompress, and install Arduino software from 2007 Arduino/installation.
  2. Write code
    • Here is our code:
      #include <Servo.h>
      Servo myservo;
      void setup() {
        myservo.attach(2);
        myservo.write(180);
      }
      void loop() { }
    • Here it is with comments:
      /* Import the Servo library. Libraries provide extra functionality, e.g. working with hardware, for us in Arduino programs (aka sketches). Now we can use the attach() and write() functions to control our servos. */
      #include <
      Servo.h>


      Servo myservo; // Create servo object to control a servo.
      // A max of 8 servos can be created

      /* Arduino code requires a setup() and a loop() block. Code in the setup() block is run once, code in the loop() block is run continuously thereafter. */
      void setup {

      // Attach()- Let the Arduino know which pin to write to (the pin the real-life servo is connected to)

      myservo.attach(2);
      // See Arduino documentation for ServoWrite (quoted below, after this)
      myservo.write(180); 
      }

      // loop() block here, even if its empty, because Arduino code requires it
      void loop() { } 

    • http://arduino.cc/en/Reference/ServoWrite
      “Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous rotation servo, this will set the speed of the servo (with 0 being full-speed in one direction, 180 being full speed in the other, and a value near 90 being no movement).”
    • Look at the documentation for more code examples, e.g. http://arduino.cc/en/Tutorial/Sweep
    • For reference:
  3. Compile and upload the code.
    Click the play |> and the upload =>| buttons.
    • An error may pop up: Serial port ____ not found or Problem uploading to board
    • If a dialog box pops up, go ahead and "Try again with a different port"
    • Otherwise, manually trial-and-error to find the right serial port:
      1. Tools > Serial Port > COM2 (or /dev/ttyUSB0 for linux)
      2. Click upload button again
      3. Repeat until find serial port (code upload is successful)
    • When successful, red and green lights (TX and RX) should blink rapidly for a second or two
  4. Flick the switch on the carrier board (if it's not already in ON position). Servo should start to turn. (May need to wait a few seconds for the Arduino to finish updating itself with the new code)
    • Flick switch again to turn off power to the servo

Next Steps

  • Add a cardboard backing (e.g. with a pizza box and electrical tape) to the back of the carrier board to make sure it doesn't short out (since all the leads are exposed at the back).
  • Note: There are rubber feet and plastic standoffs available in the cabinet. Duct tape also works.
  • Check out the iPhone tutorial under (see: file-cabinet), which has more code examples.
  • Also see all the excellent examples under Example Code.

Conclusion

Quest completed. Yay!
Now go forth and build your robot army and take over the world! *__* bwahaha

Comments? Suggestions? Did this tutorial take too long? Was it too easy? Do the images fail? Let me know.
~nouyang@mit.edu

Comments