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!
Arca
Published © GPL3+

How to Use Servo Motors with Arduino

This tutorial will teach you how to use servo motors with Arduino.

BeginnerProtip90,059
How to Use Servo Motors with Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
Any Arduino/Genuino will do.
×1
Servos (Tower Pro MG996R)
×1
Male/Male Jumper Wires
×1
SparkFun Breadboard Power Supply 5V/3.3V
SparkFun Breadboard Power Supply 5V/3.3V
You will need this if you don't use a Sg90 TowerPro Servo Motor
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Arduino Web Editor
Arduino Web Editor
Any of these will do

Story

Read more

Schematics

Schematics

Code

Using_Servo_Motors

C/C++
#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
  }
}

Credits

Arca
1 project • 47 followers

Comments