Posted on Leave a comment

Arduino – Control a Servo

Materials

  • Arduino Uno
  • Micro-Servo
  • Hookup Wire

Coding

In order to easily code for a Servo motor, it is best to use a library. This example uses the Servo.h library from barraganstudio.com

The code below is from the example, but with the pin changed and the comments removed.

/* Sweep
by BARRAGAN http://barraganstudio.com
This example code is in the public domain.modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>

Servo myservo;

int pos = 0;

void setup() {
  myservo.attach(10);
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { 
    myservo.write(pos);  
    delay(15); 
  }
  for (pos = 180; pos >= 0; pos -= 1) {
    myservo.write(pos); 
    delay(15);
  }
}

Wiring

The wiring for this example is very simple

  1. Connect +5v on the Arduino UNO to the Red wire on the Servo ( or +5v / Vcc if marked )
  2. Connect GND on the Arduino UNO to the Black wire on the Servo ( or GND if marked )
  3. Connect Pin 10 on the Arduino UNO to the remaining wire on the Servo

Leave a Reply