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 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 Particle Photon
Example
Controlling position of servo motor using a potentiometer.
When you change the potentiometer resistor then start changing the analog value from 0 to 4095 and map these value from 0 to 180 accourding to ADC value to change the position of servo motor.
Sketch For Servo Motor
/*
Controlling a servo position using a potentiometer (variable resistor)
http://www.electronicwings.com
*/
Servo myservo; /* create servo object to control a servo */
int potpin = A0; /* 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(D0); /* attaches the servo on pin D0 to the servo object */
}
void loop() {
val = analogRead(potpin); /* reads the value of the potentiometer (value between 0 and 4095) */
Serial.print("Analog Value : ");
Serial.print(val);
Serial.print("\n");
val = map(val, 0, 4095, 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(100); /* waits for the servo to get there */
}
Serial Output Window of Servo Motor
Components Used |
||
---|---|---|
Particle Photon PHNTRAYH |
X 1 | |
Servo Motor MG995 Servo Motor MG995 |
X 1 |
Downloads |
||
---|---|---|
|
servo_motor | Download |