For Arduino, there are roughly two ways to control the servo motor.
PWM (Pulse Width Modulation) is a method of controlling the motor by turning on and off the pulse signal. It’s possible to achieve discrete movement by controlling the servo motor directly using PWM, but for more complex projects, you can take advantage of servo motor library included in Arduino IDE.
Arduino IDE → [File] → [Examples] → [10.StarterKit BasicKit] → [p05_ServoMoodindicator]
This program can change the angle of the servo motor based on the input value of the analog 0 pin (A0). By using a variable resistor such as potentiometer or an optical sensor for the analog pin, try moving the servo motor when the value changes.
Servo motor library function
Servo motor library is based on two types of commands: 1) specifying the pin number of the control signal to be sent to the servo motor and 2) specifying the angle when moving the servo motor.
Code-Example
1 |
myServo.attach(9); //Specify the servo motor's signal pin |
Code-Example
1 |
myServo.write(angle); //Move the servomotor to a specific angle |
The following circuit is an example using FEETECH FS90 micro servo. The operating voltage of this servo motor is 6V. Since the current at the time of operation is 200 mA, the servo motor is powered by four AA batteries connected in series (6V).
Figure 6. Example sketch circuit
Figure 7. Servo motor control circuit
Figure 8. p05_ServoMoodIndicator
Code-Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/* Arduino Starter Kit example Project 5 - Servo Mood Indicator This sketch is written to accompany Project 5 in the Arduino Starter Kit Parts required: servo motor 10 kilohm potentiometer 2 100 uF electrolytic capacitors Created 13 September 2012 by Scott Fitzgerald http://www.arduino.cc/starterKit This example code is part of the public domain */ // include the servo library #include <Servo.h> Servo myServo; // create a servo object int const potPin = A0; // analog pin used to connect the potentiometer int potVal; // variable to read the value from the analog pin int angle; // variable to hold the angle for the servo motor void setup() { myServo.attach(9); // attaches the servo on pin 9 to the servo object Serial.begin(9600); // open a serial connection to your computer } void loop() { potVal = analogRead(potPin); // read the value of the potentiometer // print out the value to the serial monitor Serial.print("potVal: "); Serial.print(potVal); // scale the numbers from the pot angle = map(potVal, 0, 1023, 0, 179); // print out the angle for the servo motor Serial.print(", angle: "); Serial.println(angle); // set the servo position myServo.write(angle); // wait for the servo to get there delay(15); } |
Let’s briefly review what you can be done with a servo motor. The following two are the typical operations:
The servo motor can control the angle of its motion. This is why a servo motor is best for developing a mechanism that pushes buttons. You can make interesting devices like in the video below, and you can develop a variety of things just by pressing a button, switch in a room, etc.
In Use Arduino to Control a Motor Part 3 – Making an RC Car Using a Servo Motor for the Steering, we made an RC car using LEGO. We mounted the steering part using a servo motor. Servo motors can be used for various applications, but it’s commonly used for “moving” parts/objects, whether it be moving the robot car or robot arm.