• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Servo Motor Arduino Code : Basic

By Abhishek Ghosh November 3, 2015 5:30 am Updated on November 3, 2015

Servo Motor Arduino Code : Basic

Advertisement

First, we have the guide on how to buy a Servo motor for basic DIY electronics. Second, we already pointed on how to build a DIY robotic arm. Here is Basic Servo Motor Arduino Code to Test Newly Purchased Servo Motor or Perform Some Basic Action. We Can Fine Tune Degrees of Sweep. For this tutorial, we are using TowerPro SG-5010 Servo Motor, this should work for the cheaper TowerPro SG-90 Servo Motor as well.

 

Servo Motor Arduino Code : Basic

 

Unlike 3V DC Motor Code, we need not much extra electronics part to drive a servo motor. The big difference between a 3V DC Motor and a Servo Motor is the number of inputs. We will connect the black wire from the servo to the GND pin on the Arduino, the red wire from the servo to the +5V pin on the Arduino and the yellow (or white) wire from the servo to a digital pin on the Arduino – let us use number 4 digital pin. Setup is very easy :

Servo Motor Arduino Code Basic

 

Servo Motor Arduino Code

 

We can include the Arduino servo library. However, it is better to start from the basic, without including the library, here is the code :

Advertisement

---

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int servopin = 4;
int pulse = 1500;
 
void setup ()
{
pinMode(servopin, OUTPUT);
Serial.begin(9600);
}
 
void loop()
{
digitalWrite(servopin, HIGH);
delayMicroseconds(pulse);
digitalWrite(servopin, LOW);
delay(20);
}

We kept the code as Github Gist for you.

That int pulse = 1500 value is in microseconds. 5V for 1500 microseconds (1.5 milliseconds) corresponds to 90 degrees. 500 microseconds corresponds to 0 degrees and 2500 microseconds corresponds to 180 degrees. delay(20) means send pulse every 20ms.

If we want repeat 180 degree sweep including the Arduino Library, we can use the modified official code :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <Servo.h>
 
Servo thecustomizewindows;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
 
void setup() {
  thecustomizewindows.attach(4);  // attaches the servo on pin 4 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
    thecustomizewindows.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
    thecustomizewindows.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

That is it.

Tagged With arduino servo motor code , servo motor arduino code , basic arduino coding , servomotor , arduino servo motor basiss if , basic arduino code dc motor to servp , sg90 arduino connection , basic arduino motor code , basic servo code , cod 6 servo motor
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Servo Motor Arduino Code : Basic

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • Arduino Servo Motor Control With Pushbutton

    Here is Circuit Diagram, Code To Create Arduino Servo Motor Control With Pushbutton Project. This is very easy, basic but important project.

  • Arduino Bell : Brass Bell Ringing DIY Project

    Heavy Indian Brass Bells Used For Worship Can Be Shaken By Mechanical System Controlled By Arduino. Arduino Brass Bell Ringing DIY is Simple.

  • Tower Pro SG 5010 Servo Motor Test Code for Arduino

    Actually, we previously published Servo Motor Arduino Code for the Beginners but after publishing the guide Best Servo Motor for the Beginners we thought to somewhat rewrite the old guide with this new title Tower Pro SG 5010 Servo Motor Test Code for Arduino with proper sketch and video demo. You can read the former […]

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy