Sunday, 21 July 2013

Servo Interfacing - Working with the on-board Servo Interface

Induino R3  On-Board Servo Interface


The Induino R3 has 2 On-Board Interfaces for controlling Servo Motors. This is Next to the Sensor Interface on the Induino R3 Board.  The Servo interface has a +5V supply line for the servos that is being shared with the microcontroller. The Pins A4-18 / A5-19 are the Signal Pins. 



Servo Motors - What are they?

Servo motors are dc motors with a servo mechanism that lets us control the precise position of the Shaft.

A servo mechanism is a error correction mechanism that senses the negative error and applies a correction accordingly.

There are many types of Servo Motors available - depending upon applications.
They are used in areas requiring position control. While the basic working of all the servo motors is almost similar, for the scope of this tutorial we shall stick to discussing RC Servo Motors or the type of Servo motors used in Robotics / Radio Control Projects.


A Servo motor constitutes of 4 parts - a DC motor, a Gear system, A Potentiometer(for sensing feedback) and a Control Circuit. Here the Control Circuit and the Potentiometer together form the Servo Mechanism.





So How do Servo Motors Work?

The Control Circuitry on the Servo motor drives the DC motor based on input signals it receives, The output shaft of the DC motor is connected to the gear assembly and the gear assembly is such that it moves the potentiometer when it rotates. The Control Circuitry gets feedback from this potentiometer and spins the DC motor to achieve the desired angle between 0 & 180 degrees. The range of movement of the potentiometer is mechanically restrained.

And How do we Control these Servo Motors?

To Control a servo we need to give it a pulse once every 20 milliseconds. The Duration of this pulse will determine the Servo Angle which we need to achieve. For most Servo's a Pulse duration of 1millisecond will set the shaft position to 0 degrees and a pulse duration of 2 milliseconds will set the shaft position to 180 degrees. 



Controlling Servos with an Arduino

Arduino provides us with a servo control library that lets us control servos with ease. The library provides us with an option of writing both microseconds value and angle values. You can find more information on the library here - > Arduino Servo Library

Connecting the Servo Motors

First Identify the '+','GND' & 'Data'  Lines of your Servo Motor based on the image below

Connect the Servo Motor to the Servo Interface on the Induino R3 Board as shown in the image below
t

Ensure you use an External DC Adapter to Power the Board When Working with Servos
Programming the Servo
  • Include the Servo.h Library
  • Create a Object of Class Servo (can have any name like a variable) / Syntax - Servo myservo;
  • Map the Servo Object to a Pin using the attach() function of the Servo Library -> myservo.attach(servo pin number);
  • Control the Servo using the write() function of the Servo Library. the write() function takes in a degree value in the range of 0 to 179 as input parameter and creates the required control signal on the servo pin.


Here's a modified version of the Sweep Example from the Servo Library

/*  Induino R3 User Guide - Program 9.0 - Controlling a Servo Motor Using the Servo Interface on the Induino R3 */

#include  <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(18);  // attaches the servo on pin 18 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 
    myservo.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>=1; pos-=1) // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos); // tell servo to go to position in variable 'pos' 
    delay(15);          // waits 15ms for the servo to reach the position 
  } 
}  

Next Let's see how we can control the Servo using the Light Intensity sensed by the On-Board LDR.
The LDR will give us an analog value in the range of 0 - 1023. However the maximum value that can be given to the write() function is 179.  So we need to scale down the values. For this, we have an inbuilt function in Arduino


map(value, fromLow, fromHigh, toLow, toHigh) - The map() function re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.

Here's our program where the LDR value is mapped to control the Servo

/*  Induino R3 User Guide - Program 9.1 - Controlling a Servo Motor Using the LDR Value */


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
int ldr_pin = 3;  // ldr is on Analog pin 3
int val;    // variable to read the value from the analog pin 
 
void setup() 
{ 
  myservo.attach(18);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  val = analogRead(ldr_pin);      // reads the value of the LDR (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val); // sets the servo position according to the scaled value 
  delay(15);   // waits for the servo to get there 
} 


Thats It For This Part! Enjoy... and feel free to drop us an email with questions you might have -> info@simplelabs.co.in

Visit www.simplelabs.co.in for more interesting products

Back to List of Contents

7 comments:

  1. what is the problem connecting the usb as power for servo...?

    ReplyDelete
    Replies
    1. USB power is limited to a max of 500mA of current by the on-board Fuse and the internal fuse in your computers usb port. Servos when loaded can consume more current than this leading to resets, burn-outs, etc.

      Delete
    2. So you mean to say that voltage regulator in dc jack is not capped with a fuse to monitor current or is it capped with a higher rating fuse ?? I read in ur specs that , Induino- whether USB or DC jack, both are capped to 500mA.

      Delete
    3. There is no fuse on the DC jack however the limitations of current are that of the voltage regulator 7805

      Delete
  2. Dear sir, i need the induino-r3 schematic circuit

    ReplyDelete
  3. "Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.php jobs in hyderabad.
    "

    ReplyDelete
  4. Very much useful article. Kindly keep blogging

    Java Training in Chennai

    ReplyDelete