Servo Motor control using MicroPython

Keywords: DC Motor control, ESP8266, MicroPython, uPyCraft, GPIO control, PWM control using micro python, ESP32, Servo Motor, DC motor basics, Applications of DC motors

I am with Electrical background and have my love towards Motors. I wondered many times how to choose a low power DC motor in any Embedded Application. Here is my version of DC motors with Micro-controllers concept and how to control a ‘Servo motor’ using ESP8266 using MicroPython. Before that I want to explain basics like actuator, motor, BLDC, PMDC etc.

ACTUATOR: Actuator is a device which converts electrical signal into physical events. Actuators are used to control output function of a system.

DC Motors: A Direct Current motor is most commonly used actuator for producing continuous movement and whose speed of rotation can easily be controlled. A DC motor consists of two parts, a “Stator” which is the stationary part and a “Rotor” which is the rotating part. There are basically three types of DC Motors available.

  • Brushed DC Motor or Permanent magnet DC Motor
    • The rotor or armature of a DC machine consists of current carrying conductors connected together at one end to electrically isolated copper segments called the commutator. The commutator allows an electrical connection to be made via carbon brushes (hence the name “Brushed” motor) to an external power supply as the armature rotates.
    • PMDC motor rotates continuously.

  • Brushless DC Motor
    • The brushless DC motor (BDCM) is very similar to a permanent magnet DC motor, but does not have any brushes to replace or wear out due to commutator sparking. Therefore, little heat is generated in the rotor increasing the motors life.

    • BLDC motor rotates continuously.

  • Servo Motor
    • A servo motor consists of a DC motor, reduction gearbox, positional feedback device and some form of error correction. The speed or position is controlled in relation to a positional input signal or reference signal applied to the device.
    • The Servo Motor is basically a DC motor which does not run continuously for a longer period of time. It has a unique arrangement which allows the motor to rotate at a specific angle with greater accuracy and precision. This machine is controlled by a feedback system.
    • Servo motor rotation is limited to 180⁰



  • Brushed DC Motor
    • Depending on the connections of the field to the power supply, the speed and torque characteristics of a brushed motor can be altered to provide steady speed or speed inversely proportional to the mechanical load.
    • Brushed motors continue to be used for electrical propulsion, cranes, paper machines and steel rolling mills
  • Brushless DC Motor
    • Brushless motors have come to dominate many applications, particularly devices such as computer hard drives and CD/DVD players.
    • Small cooling fans in electronic equipment are powered exclusively by brushless motors.
    • They can be found in cordless power tools where the increased efficiency of the motor leads to longer periods of use before the battery needs to be charged.
    • Low speed, low power brushless motors are used in direct-drive turntables for gramophone records.
    • Application includes Transport, cordless power tools, Heating and ventilation, etc
  • Servo Motor 
    • The servo motor is most commonly used when the rotation requires great precision and accuracy where the output shaft can be moved at a particular angle.
    • Servo motors are mainly used in home electronics, toys, airplanes, robotic arms, legs or rudder control system and toy cars etc.

Now that we are done with the basics of DC motors I would go ahead to show how to control Servo motor using ESP8266.


All motors have three wires coming out of them. Out of which two will be used for Supply (positive and negative) and one will be used for the signal that is to be sent from the MCU.

Servo motor is controlled by PWM (Pulse with Modulation) which is provided by the control wires. There is a minimum pulse, a maximum pulse and a repetition rate. Servo motor can turn 90 degree from either direction form its neutral position. The servo motor expects to see a pulse every 20 milliseconds (ms) and the length of the pulse will determine how far the motor turns.

Servo motor works on PWM (Pulse width modulation) principle, means its angle of rotation is controlled by the duration of applied pulse to its Control PIN. Basically servo motor is made up of DC motor which is controlled by a variable resistor (potentiometer) and some gears. High speed force of DC motor is converted into torque by Gears. We know that WORK= FORCE X DISTANCE, in DC motor Force is less and distance (speed) is high and in Servo, force is High and distance is less. Potentiometer is connected to the output shaft of the Servo, to calculate the angle and stop the DC motor on required angle.

Servo motor can be rotated from 0 to 180 degree, but it can go up to 210 degree, depending on the manufacturing. This degree of rotation can be controlled by applying the Electrical Pulse of proper width, to its Control pin. Servo checks the pulse in every 20 milliseconds. Pulse of 1 ms (1 millisecond) width can rotate servo to 0 degree, 1.5ms can rotate to 90 degree (neutral position) and 2 ms pulse can rotate it to 180 degree. 


    • Servo  motor has a 3 wire system

Wire color


NodeMCU Pin










Pin description for ESP8266
Set the frequency to 50Hz (one cycle per 20ms) and the duty value to between 18(18/1023 * 20ms = 0.35ms) and 115(115/1023 * 20ms = 2.24ms). The duty cycle can be between 0 and 1023 inclusive.


  • ESP8266, 
  • Servo motor with 3 pin connector

Python code

This is the snapshot of the above executed program.

THANK YOU and many more programs coming your way!

Please put your queries in comment section, I will get back to you with answers.

Please like and share Techawarey.  Find and Like Techawarey on Facebook.😊

Related posts

3 Thoughts to “Servo Motor control using MicroPython”

  1. raman

    i want to write a program in micro python for servo motor that it will rotate from 0 to 180 degree and after 45 min it again rotate 0 to 180 degree. plz write this program i have today time only plz help me….

    1. Seetha Akella

      Hi Raman,

      You have you tweak two codes which are already provided in my posts. Here are the links for those.

      Stay Home! Stay Safe!

Leave a comment....