Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Interfacing Servo Motor with Arduino MEGA-2560

By Deepesh Verma

 

Servo motor is widely used by engineers and hobbyists in various applications. One major application is Robotics. Servo motor is used because it is easy to operate (PWM technique). It can maintain 90%of its rated torque at high speed. Sometimes a servo can provide up to twice its rated torque for short duration. Also, a servo operation is vibration-less and is free of resonance issues.

This article is about interfacing a servo motor with Arduino Mega series board.

Image of Servo Motor

Fig. 1: Image of Servo Motor

Image of Arduino Mega 2560

Fig. 2: Image of Arduino Mega 2560

Servo Motor:

The servo motor used here has 17 Kg-cm torque (This is its max. torque rating) at 6.6V. However this torque varies with supply voltage. Say at 4.8-5V it can give up to 14-15 Kg-cm.

It can rotate 0-180 degrees at a maximum speed of 2.2msec/180deg.

Image of Servo Motor

Fig. 3: Typical Image of Servo Motor

 Arduino Mega2560

Arduino Mega2560:

Image of Arduino Mega 2560

Fig. 4: Typical Image of Arduino Mega 2560

The Arduino Mega 2560 is a microcontroller board based on the ATmega2560. It has 54 digital input/output pins (of which 15 can be used as PWM outputs), 16 analog inputs, 4 UARTs(hardware serial ports), a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started. The Mega is compatible with most shields designed for the Arduino Duemilanove or Diecimila.

The Mega 2560 is an update to the Arduino Mega, which it replaces.

 

Each of the 54 digital pins on the Mega can be used as an input or output, using pinMode(), digitalWrite(), and digitalRead() functions. They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA and has an internal pull-up resistor (disconnected by default) of 20-50 kOhms.

The ATmega2560 on the Arduino Mega comes preburned with a bootloader that allows you to upload new code to it without the use of an external hardware programmer. It communicates using the original STK500 protocol.

Orange Wire: PWM output from pin9 to Motor Control Input

Red Wire: +6V supply for Motor

Black Wire: 0V (Ground)

Connect the Supply to the board (5-12V DC). Connect USB with PC and upload the sketch to the board.

On connecting the board first time, it’ll ask for the drivers (which can be downloaded from arduino website).

Open Arduino IDE and Create a new Sketch (In Arduino world code/program is called a ‘sketch’).

Write the code and ‘verify’ (option on toolbar) it. After  verifying it will notify if  there are any errors otherwise ‘Done Compiling’ will be shown.

Now Upload the sketch to the Hardware. When sketch is Uploaded, Press Reset on the board. Now connect Servo control Pin at respective Pin and provide supply to it. The Pin9 of PWM port of the board provides motor control.

  •  

Project Source Code

###

#include <Servo.h>
Servo servo1;                          // create servo object to control a servo
int posn = 0;                            // variable to store the servo position
void setup()
{
  servo1.attach (9);      // attaches the servo on pin 9 to the servo object
}
void loop()
{
  for (posn = 0; posn < 180; posn += 1)            // goes from 0 degrees to 180 degrees
 {                                                                       // in steps of 1 degree
    servo1.write (posn);                                                 // tell servo to go to position in variable 'pos'
    delay (10);                                       // waits 10ms for the servo to reach the position
  }
  for (posn = 180; posn>=1; posn-=1)                // goes from 180 degrees to 0 degrees                                                                                 // in steps of 1 degree
{                               
    servo1.write (posn);                                                  // tell servo to go to position in variable 'pos'
    delay (10);                                        // waits 10ms for the servo to reach the position
  }
}


 

###

 


Circuit Diagrams

Circuit-Diagram-Arduino-Mega-2560-Based-Servo-Motor-Controller


Filed Under: Electronic Projects
Tagged With: Arduino, arduino mega2560, servo motor
 

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Renesas to demonstrate first working silicon based on the Arm Cortex-M85 processor
  • STMicroelectronics releases first automotive IMU with embedded machine learning
  • Infineon offers DC-DC controller for full LED headlamps without a microcontroller
  • Vishay launches new high-precision, thin-film wraparound chip resistor
  • STMicroelectronics’ common-mode filters ensure signal integrity in serial interfaces

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Temperature sensor readout
  • Capacitive Switches
  • CSI-2 FIFO or Line buffer?
  • What is the function of the long stub?
  • Circuit Performance Exploration

RSS Electro-Tech-Online.com Discussions

  • software PWM
  • Opamp ciruit
  • Audio equalizer
  • Background of Members Here
  • SPI Questions
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering