• 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 426 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 (22.1K 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

  • What are Offshore Sportsbooks and How to Take Advantage of Them? May 28, 2022
  • The True Cost Of Container Security: Why They’re Not A Silver Bullet May 28, 2022
  • How To Build An AI Strategy That Works May 26, 2022
  • The Future Of Serverless: The Load-Intensive Workload Case May 25, 2022
  • Cutting Out The Coding: Serverless Computing In Action May 24, 2022

About This Article

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

Source:The Customize Windows, JiMA.in

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Privacy Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

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

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