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.
Fig. 1: Image of Servo Motor
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.
Fig. 3: Typical Image of Servo Motor
Arduino Mega2560
Arduino Mega2560:
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
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.