Controlling The Servo Motor With The Keyboard

Controlling The Servo Motor With The Keyboard

Posted on

Servo Motor With The Keyboard – Servo motor is a motor that can be controlled to rotate to a certain degree. Generally, this servo motor can rotate from 0 degrees to 180 degrees. There are also those that can rotate up to 360 degrees. This motor has three cables. Two of them for power + and – and one for data. Data cable is used to control the rotation of the servo motor.

This servo motor is the main component in robots, RC cars and RC aircraft. To control the servo motor using Arduino is very easy. With the help of the Servo.h library, which already exists on the Arduino IDE, we only need to enter the number of rotation points of the motor, then the servo will move according to the number of degrees we want.

PWM concept of a Servo Motor works based on pulse width modulation (PWM). PWM is a method for obtaining analog or voltage values from digital values. 0-5 volt voltage is explained in digital values from 0-255. However, to show the amount of value, we use the concept of duty cycle in percent. Consider the following example:

PWM explanation

Arduino pins only have a maximum voltage of 5 volts. So, the width and smallness of the cycle will carry swinging voltage from 0-5 volts. Writing values if we use the “analogWrite ()” command is as follows:

  • analogwrite(0) = 0% = 0 volt
  • analogWrite(64) = 25% = 1.25 volt
  • analogWrite(127) = 50% = 2.5 volt
  • analogWrite(191) = 75% = 3.75 vol
  • tanalogWrite(255) = 100% = 5 volt

The frequency used in the cycle is 50Hz.  Generally, to drive a servo motor requires a maximum cycle time of 2ms. 1ms for 0°, 1.5ms for 90° and 2ms for 180°. But this is not a provision, every company can be different cycle times. Therefore, the servo motor needs to be calibrated before use. Look at the following picture:

pwm signal servo

Basic Program Servo Motor Using Arduino

The command to move this servo motor on the Arduino IDE is “analogWrite ()”. Following is the basic servo program:

#include <Servo.h> 

Servo myservo;

void setup() 
  myservo.write(90);  // set degree

void loop() {}

Schematic Arduino Servo

Please follow the schematic below.
servo serial monitor

Read other articles  Visualize The Value of The Joystick Output With Processing

Control the servo motor using the keyboard on the Serial Monitor.

If we send a number “90” from the serial monitor, to rotate 90 degrees, this won’t work. Because the value we receive from the serial monitor is the character data type or char. While the value we need to determine the position of the servo motor is the value of the integer data type. To change the data type from char to int we can use the command “Serial.parseInt ()”. For a tutorial on converting data types from serial monitors, please read here. Here are the basic commands for converting values ​​from serial monitors to integer values:

void setup(){

void loop(){
  while (Serial.available() != 0){
    int val = Serial.parseInt();

Combining programs above the servo base and value conversion we will use to drive the servo motor. The following is the program:

#include <Servo.h>

Servo myServo;

void setup() {

void loop() {
  while (Serial.available() != 0) {
    int val = Serial.parseInt();

Now upload the program above to your Arduino, then open the serial monitor, enter the value of the degree you want, then click send. The servo motor will rotate according to the degree that has been sent.

One thought on “Controlling The Servo Motor With The Keyboard

Leave a Reply

Your email address will not be published. Required fields are marked *