Use servo motors with Arduino

 

Hi Everyone, this is a tutorial on how to use the servo motors with Arduino Uno you can check it in the video, here I’m gonna post the wiring and codes I used in the video hope you like it.

Servo motors are used in a lot of projects, so everyone learning how to use Arduino should know how to use one, these motors have a high torque, up to 13 kg-cm, it depends on the type of motor and the operating voltage, they are mostly known for positioning, also as we can see in codes used we rotate them by mentionning the position, and these motors are known to be slow, and they have a known angle some are between 0-180° and others 0-360°, once the reach their limit they should return, not like the continuous servo-motors they can rotate freely but they cannot be positionned like this and I’ll do another tutorial on these types of motors.

Wiring:

Codes:

 

Code 1 : Simple write/rotation of the servo motor

 

//This code is to use a servo motor with Arduino board it rotate the servo from 0° to 180°

//You wire GND VCC SIGNAl to GND 5V D6 from Arduino //SurtrTech//


#include <Servo.h> //Servo libraries


Servo myservo;  //Declaring Servo you can change the name "myservo"


void setup() {

    Serial.begin(9600);

    myservo.attach(6); //You declare on which pin your servo is wired (The signal pin)

}


void loop() {

  myservo.write(0); //"write" function permits you to rotate the servo to which position you want

  delay(3000);

  myservo.write(180);

  delay(3000);


}

 

Code2 : Sweep mode (taken from the Arduino Servo library)

——————————————————-Code——————————————————

/* 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(6);  // 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

  }

}

Code 3: Simple code on rotating the servo and keep it in that position, I used it on the video so I decided to write it here too

 

//This code is to use a servo motor with Arduino board it rotate the servo from 0° to 90° 

//and stops there while writing "ON" on the serial monitor

//You wire GND VCC SIGNAl to GND 5V D6 from Arduino //SurtrTech//

#include <Servo.h>


Servo myservo;  


void setup() {

    Serial.begin(9600);

    myservo.attach(6);

}


void loop() {

 myservo.attach(6);

 Serial.println("ON");

 myservo.write(90);

}

 


Code 4: How to use the function “detach”

 

//This code is to use a servo motor with Arduino board and the use of the function "detach"

//You wire GND VCC SIGNAl to GND 5V D6 from Arduino //SurtrTech//


#include <Servo.h> // Servo library


Servo myservo;  //Declaring the Servo


void setup() {

    Serial.begin(9600);

    myservo.attach(6); //Declaring on which pin the servo is connected

}


void loop() {

 myservo.attach(6);     //The first part until "delay" it attach the servo = connect it, then write "ON"

 Serial.println("ON");  //on the serial monitor and rotate the servo to 90°

 myservo.write(90);

 delay(5000);

 myservo.detach();     // In this part we detach the servo which means you can rotate it freely from 0 to 180°

 Serial.println("OFF");// While writing "OFF" on the monitor

 delay(5000);


}

 

Code 5: Code to read the postion of the Servo motor, I didn’t use it in the video but here is it, it will be useful in the future

 

//This code is to use a servo motor with Arduino board and the use of the function "read"

//You wire GND VCC SIGNAl to GND 5V D6 from Arduino //SurtrTech//


#include <Servo.h>


Servo myservo;  


void setup() {

    Serial.begin(9600);

    myservo.attach(6);

}


void loop() {

float a=myservo.read(); // permits to read the position of the servo motor and show it on the serial monitor

Serial.println(a);


}

 

One thought on “Use servo motors with Arduino

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: