Tuesday, April 19, 2016

Controlling a servo motor with Arduino

How to interface servo motor with arduino?
The great cheap to buy things online, and abroad, is the almost complete lack of textbooks. An example is the servo motor below.

A label says " Micro Servo 9g" was the biggest clue that I had the model. There begins the fun part of it, which is to find out how the servant works, their connections, voltages and such. Sifting through Google, I found a very similar model, called HXT900, and that its operating voltage ranges from 3 to 6V. It's a beginning. ? But what connections Well, the cable has 3 ways: Black, red and white. Easy, right? Black on GND, red on 5V . And white for data I tested the servo using the example that comes in the Arduino IDE, which uses a potentiometer to control the engine speed:

Arduino Servo motor interface

The program below you can find on the menu File -> Examples -> Servo -> Knob :



// Program: Controlling the position of a servant using potentiometer 
// Controlling the servo position using the potentiometer (variable resistor) 


#include <Servo.h> 
myservo Servant;   // create servo object to control a servo

// Analog pin potentiometer 
int potpin = 0 ;

// Variable that stores the read value of the potentiometer 
int val;     

void setup ()
{ 
  // Set the servo is connected to port 9 
  myservo.attach ( 9 );  
} 

void loop ()
{ 
  // Le value of potentiometer (values ​​between 0 and 1023) 
  val = analogRead (potpin);            

  // Convert the value to be used in servo (values ​​between 0 and 180) 
  val = map (val, 0 , 1023 , 0 , 179 );     

  // Move the axis servo, according to the angle
  myservo.write (val);                  

  // Wait for the servo to reach the position 
  delay ( 15 );                           
}

The program reads the pot of information connected to the analog input values ​​(0-1023), converts these values ​​to be used in servo (0-180), and moves the servo motor to the corresponding position. Turn the potentiometer in 2 -way, and the servant will accompany the pot movement. We can also control the servo using pre-established positions, which is very useful when we need to use the engine to repetitive movements. As in the example below, where I used 3 keys (push-button):



and the following program:

// Program: Controlling the servo with 3 keys 

#include <Servo.h> 

Servo myservo;

int val, 
 int ChE = 3 ; // pin to be connected in key left 
int ChC = 4 ;   // pin to be connected in the central key 
int ChD = 5 ;   // pin to be connected in the right key

void setup ()
{ 
  // Servo data pin connected to pin 9 of the Arduino 
  myservo.attach ( 9 );   
   // Set the pin as input
  pinMode (ChE, INPUT);      
  // Triggers the internal pull-up resistor
  digitalWrite (ChE, HIGH);  
  pinMode (ChC, INPUT);
  digitalWrite (ChC, HIGH);
  pinMode (ChD, INPUT);
  digitalWrite (ChD, HIGH);
} 

void loop ()
{ 
 // Le value of the Left Key (On / Off)
 val = digitalRead (ChE);  
   // If the key is pressed, the servo moves 
   if (val! = 1 )
    {
       myservo.write ( 60 );   // Move the servo to the angle of 60 degrees 
       delay ( 15 );           // Delay for the servant reach the position
    }

 val = digitalRead (ChC);
   if (val! = 1 )
    {
       myservo.write ( 90 );   // Move the servo to the angle of 90 degrees 
       delay ( 15 );
    }

 val = digitalRead (ChD);
   if (val! = 1 )
    {
       myservo.write ( 120 );   // Move the servo to the angle of 120 degrees 
       delay ( 15 );
    }
}

Pressing the left button, the servo moves to the "60 degrees", the center button takes the position 90 degrees, and the right button, the position 120 degrees.

No comments:

Post a Comment