• 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

This Article Has Been Shared 629 Times!

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

  • Arduino Vs Raspberry Pi

    Arduino is for prototyping, the boards are micro-controllers. Whereas, Rapasberry Pi is actually a computer. Here is Arduino Vs Raspberry Pi

  • How to install and setup Arduino on OS X

    Here is Step by Step Guide With Video on How to install and setup Arduino on OS X. The Arduino Board Used in this Guide is Arduino UNO R3.

  • Mini Christmas Light With Arduino (Very Easy)

    Here is a Mini Christmas Light With Arduino as Project Which Needs 3 Resisters and 3 LEDs, No External Power Supply. It Has 2 Blink Effects.

  • Arduino Basic 3V Motor Control : Speed Variation For Robot

    Very Easy With 100% Working Warranty. Code, Circuit Included. This Arduino Basic 3V Motor Control is Intended For Speed Variation For Robot.

  • DIY 3V DC Motor Stand With Female Jumper Ends

    Here is an Illustrated Guide For DIY 3V DC Motor Stand With Female Jumper Ends. 3V DC Motors Lacks Jumper, Needs Sound & Vibration Absorber.

Additionally, 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

  • Cyberpunk Aesthetics: What’s in it Special January 27, 2023
  • How to Do Electrical Layout Plan for Adding Smart Switches January 26, 2023
  • What is a Data Mesh? January 25, 2023
  • What is Vehicular Ad-Hoc Network? January 24, 2023
  • Difference Between Panel Light, COB Light, Track Light January 21, 2023

About This Article

Cite this article as: Abhishek Ghosh, "Servo Motor Arduino Code : Basic," in The Customize Windows, November 3, 2015, January 29, 2023, https://thecustomizewindows.com/2015/11/servo-motor-arduino-code-basic/.

Source:The Customize Windows, JiMA.in

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

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT