Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Alex Glow
Published

Servo: Arduino Basics

Able to point in a specific direction or turn by degrees, this motor is a staple in home automation and robotics.

BeginnerProtip1 hour60,016
Servo: Arduino Basics

Things used in this project

Hardware components

Arduino 101
Arduino 101
Any 'Duino will do.
×1
Servos (Tower Pro MG996R)
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Hand tools and fabrication machines

HW101 Marker
Hardware 101 HW101 Marker

Story

Read more

Schematics

1. Arduino with servo on pin 9

2. Arduino with servo & button

For the third code sample, Dial Counter :)

Code

Arduino example sketch: "Sweep"

C/C++
File > Examples > Servo > Sweep
/* 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;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

SweepCalibrated

C/C++
This sketch modifies Sweep to add calibration variables – so you don't drive your motor outside its range – and also adds a little LED feedback each time the loop finishes. My servo is called George.
/* 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 george;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
int ledPin = 13;
int mini = 15; // servo's minimum position
int maxi = 160; // servo's maximum position

void setup() {
  george.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = mini; pos <= maxi; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    george.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = maxi; pos >= mini; pos -= 1) { // goes from 180 degrees to 0 degrees
    george.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  digitalWrite(ledPin, !digitalRead(ledPin));
}

Dial counter sketch

C/C++
Count the number of button presses, and track them on a dial.
/*
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;  // create servo object to control a servo
                // twelve servo objects can be created on most boards

int pos = 10;    // variable to store the servo position
int pushButton = 2;
int ledPin = 13;
int clicks = 0;

void setup()
{
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(pushButton, INPUT_PULLUP);
  digitalWrite(ledPin, LOW);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  if (buttonState == 0 && pos <= 160) {
    clicks = clicks + 1;
    Serial.print("Clicks: ");
    Serial.println(clicks);
    pos = map(clicks, 0, 29, 15, 160);
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(100);
  }
  else if (buttonState == 0) {
    digitalWrite(ledPin, HIGH); // LED goes on when the button has been pressed too many times
  }
}

Credits

Alex Glow
149 projects • 1593 followers
The Hackster team's resident Hardware Nerd. I love robots, music, EEG, wearables, and languages. FIRST Robotics kid.

Comments