.. _servo_classic: Driving a Servo --------------- Servo Principle --------------- A servo is a position (angle) servo driver, consisting of an automatic control system made up of a DC motor, a reduction gear set, a sensor and a control circuit. By sending a signal, you specify the rotation angle of the output shaft. Generally there is a maximum rotation angle (such as 180 degrees). The difference from an ordinary DC motor is that an ordinary DC motor rotates continuously, while a servo can only turn within a certain angle and cannot rotate continuously (but a digital servo can switch between servo mode and motor mode). An ordinary DC motor cannot feed back the rotation angle information, whereas a servo can. .. image:: /_static/products/mpython-s3/_static/image/classic/Servo.jpg :scale: 70 % :align: center A standard servo has 3 leads: the ground wire GND (the brown wire of the servo cable), the power wire Vcc (the red wire of the servo cable), and the control signal wire (the orange wire of the servo cable). .. image:: /_static/products/mpython-s3/_static/image/classic/Servo1.jpg :scale: 80 % :align: center The servo system is controlled by pulses of variable width, and the control wire is used to transmit the pulses. The pulse parameters have a minimum value, a maximum value, and a frequency. Generally, the reference signal period of a servo is 20ms and the width is 1.5ms; the position defined by this reference signal is the middle position, and the angle is produced by the continuous pulses coming from the control wire. The high-level part of the pulse is generally the angle control pulse part within the range of 0.5ms-2.5ms, with a total interval of 2ms. This control method is called pulse modulation; the length of the pulse determines how large an angle the servo turns. .. image:: /_static/products/mpython-s3/_static/image/classic/Servo2.png :scale: 100 % :align: center For example: a 1.5 millisecond pulse will turn to the middle position (for a 180° servo, this is the 90° position). When the control system issues a command to move the servo to a certain position and keep it at that angle, external forces will not change its angle at this time; but this has an upper limit, which is its maximum torque. Unless the control system continuously sends pulses to stabilize the servo angle, the servo angle will not remain unchanged. The control relationship corresponding to the 180-degree angle servo is: .. image:: /_static/products/mpython-s3/_static/image/classic/Servo3.jpg :scale: 100 % :align: center .. Attention:: Different servos have different pulse width ranges, so pay attention when controlling them! Controlling a Servo with the Accelerometer ------------------------------------------ We combine the accelerometer to make a servo whose angle is controlled by tilting the mPython Board left and right:: from mpython import * from servo import Servo myServo=Servo(0,min_us=500, max_us=2500) # Build the Servo object while True: y=accelerometer.get_y() # Get the acceleration of the Y axis if y<=1 and y>=-1: angle=int(numberMap(y,1,-1,0,180)) # Map the Y-axis acceleration value myServo.write_angle(angle) # Write the servo angle oled.DispChar("angle:%d" %angle,40,25) oled.show() oled.fill(0) sleep_ms(10) The connection between the servo and the mPython Board requires the mPython expansion board. You can consult :ref:`mPython Board interface pin description` to learn about the pins available for PWM analog output. Here we use pin P0. Insert the mPython Board into the mPython expansion board, and connect the servo to the expansion board using female-to-female jumper wires: the power wire Vcc (the red wire of the servo cable) connects to the power port "V" of the expansion board, the ground wire GND (the brown wire of the servo cable) connects to the ground port "G" of the expansion board, and the control signal wire (the orange wire of the servo cable) connects to pin "0" of the expansion board. .. image:: /_static/products/mpython-s3/_static/image/classic/Servo1.jpg :scale: 60 % :align: center Before use, import the mpython and servo modules:: from mpython import * from servo import Servo Build the Servo object and set the servo pulse width parameters:: myServo=Servo(0,min_us=500, max_us=2500) .. Note:: Servo(pin, min_us=750, max_us=2250, actuation_range=180) is used to build a Servo object, and uses an SG90 servo by default. Different servos have different pulse width parameters and angle ranges, so set them yourself according to the servo model. ``pin`` sets the servo PWM control signal pin, ``min_us`` sets the minimum width of the servo PWM signal pulse in microseconds, with a default of min_us=750, ``max_us`` sets the minimum width of the servo PWM signal pulse in microseconds, with a default of max_us=2250 (the upstream source text describes ``max_us`` as the *minimum* pulse width; in the current firmware it is in fact the maximum pulse width paired with ``min_us``), and ``actuation_range`` sets the maximum rotation angle of the servo. .. Attention:: You can set ``actuation_range`` to match the actual range of motion observed for the given ``min_us`` and ``max_us``. You can also extend the pulse width above and below these limits, but the servo mechanism may stop, buzz, and draw extra current when stalled. Test carefully to find the safe minimum and maximum values. When the mPython Board is detected tilting in the Y-axis direction (range -1g to +1g), map the Y-axis acceleration value (range -1 to 1) onto the rotation angle of the servo output shaft (range 0 to 180):: if y<=1 and y>=-1: angle=int(numberMap(y,1,-1,0,180)) Output the servo angle and display it on the OLED screen:: myServo.write_angle(angle) # Write the servo angle oled.DispChar("angle:%d" %angle,40,25) oled.show()