Servo Motor Interfacing With Arduino Uno
Introduction
Servo Motor
A servo motor is an electric device used for precise control of angular rotation. It is used in applications that demand precise control over motion, like in case of control of a robotic arm.
The rotation angle of the servo motor is controlled by applying a PWM signal to it.
By varying the width of the PWM signal, we can change the rotation angle and direction of the motor.
For more information about Servo Motor and how to use it, refer the topic Servo Motor in the sensors and modules section.
Interfacing Diagram
Interfacing Servo Motor With Arduino UNO
Example
Controlling position of servo motor using a potentiometer.
Here, we will be using the Servo library that comes along with the Arduino IDE.
There are two examples in this library. We will be using those two examples.
To open the Knob example, go to File > Examples > Servo* > Knob
To open the Sweep example, go to File > Examples > Servo* > Sweep
* You will find the Servo examples in the Example for any board that is below the Built-In Examples. You will have to navigate down and find it.
Sketch For Servo Position Control Using Potentiometer
/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
Servo myservo; /* create servo object to control a servo */
int potpin = 1; /* analog pin used to connect the potentiometer */
int val; /* variable to read the value from the analog pin */
void setup() {
Serial.begin(9600);
myservo.attach(9); /* attaches the servo on pin 9 to the servo object */
}
void loop() {
val = analogRead(potpin); /* reads the value of the potentiometer (value between 0 and 1023) */
Serial.print("Analog Value : ");
Serial.print(val);
Serial.print("\n");
val = map(val, 0, 1023, 0, 180); /* scale it to use it with the servo (value between 0 and 180) */
Serial.print("Mapped Value : ");
Serial.print(val);
Serial.print("\n\n");
myservo.write(val); /* sets the servo position according to the scaled value */
delay(1000); /* waits for the servo to get there */
}
No comments:
Post a Comment