### Project 15:Servo ![](./media/Python_c4b5e57b.png) 1\. **Description** The DIY smart cars usually contain the function of automatic obstacle avoidance. In the DIY process, we need a servo to control the ultrasonic module to rotate left and right, and then detect the distance between the car and the obstacle, so as to control the car to avoid the obstacle. If other microcontrollers are used to control the rotation of the servo, we need to set a certain frequency and width of pulse to control the servo angle. But if the micro:bit main board is used to control the servo angle, we only need to set the control angle in the development environment where the corresponding pulse will be automatically set to control the servo rotation. In this project, you will learn how to control the servo to rotate back and forth between 0° and 90°. Servo motor is a position control rotary actuator, which mainly consists of housing, circuit board, core-less motor, gear and position sensor. Its working principle is that the servo receives the signal sent by MCU or receiver, and produces a reference signal with a period of 20ms and width of 1.5ms, then compares the acquired DC bias voltage to the voltage of the potentiometer and obtains the voltage difference output. For the servo used in this project, the brown wire is the ground, the red one is the positive wire, and the orange one is the signal wire. ![](./media/Python_69be9581.png) 2\. **Information of the Servo** The rotation angle of servo motor is controlled by regulating the duty cycle of PWM (Pulse-Width Modulation) signal. The standard cycle of PWM signal is 20ms (50Hz). Theoretically, the width is distributed between 1ms-2ms, but in fact, it's between 0.5ms-2.5ms. The width corresponds to the rotation angle from 0° to 180°. But note that for different brand motor, the same signal may have different rotation angle.  ![](./media/Python_0982cb7b.png) After measurement, the pulse range of the servo is 0.65ms~2.5ms. For a 180 degree servo, the corresponding control relationship is as follow: |Time on High Level|Angle of the Servo|Reference Signal Cycle Time(20ms)| |-|-|-| |0.65ms|0 degree|0.65ms high level+19.35ms low level| |1.5ms|90 degrees|1.5ms high level+18.5ms low level| |2.5ms|180degrees|2.5ms high level+17.5ms low level| 3\. **Parameters** - Working voltage: DC 4.8V ~ 6V - Operating angle range: about 180 ° (at 500 → 2500 μsec) - Dimension: 22.9\*12.2\*30mm - Pulse width range: 500 → 2500 μsec - No-load speed: 0.12 ± 0.01 sec / 60 (DC 4.8V), 0.1 ± 0.01 sec / 60 (DC 6V) - No-load current: 200 ± 20mA (DC 4.8V), 220 ± 20mA (DC 6V) - Stopping torque: 1.3 ± 0.01kg · cm (DC 4.8V) ,1.5 ± 0.1kg · cm (DC 6V) - Stop current: ≦ 850mA (DC 4.8V) ≦ 1000mA (DC 6V) - Standby current: 3 ± 1mA (DC 4.8V), 4 ± 1mA (DC 6V) - Weight: 9±1g (without servo horn) - Working temperature: -30℃~60℃ **It should be noted that do not use a computer for power supply, because if the current demand is greater than 500mA, the servo may be burned out. It is recommended to use an external battery for power supply.** 4\. **Preparation** - Insert micro:bit board into the slot of keyestudio 4WD Mecanum Robot CarV2.0 - Place batteries into battery holder - Dial power switch to ON end - Connect micro:bit to computer via an USB cable - Open the offline version of Mu. 5\. **Test Code** Enter Mu software and open the file“Servo\.py”to import code. You can also input code in the edit window yourself. (**Note: All English words and symbols must be written in English**.) Click“Check”to examine errors in the code. The program proves wrong if underlines and cursors are shown. If the code is correct, connect the micro:bit to your computer and click“Flash”to download the code to the micro:bit board. ![](./media/Python_eecf365e.png) ```python from microbit import * class Servo: def __init__(self, pin, freq=50, min_us=600, max_us=2400, angle=180): self.min_us = min_us self.max_us = max_us self.us = 0 self.freq = freq self.angle = angle self.analog_period = 0 self.pin = pin analog_period = round((1/self.freq) * 1000) # hertz to miliseconds self.pin.set_analog_period(analog_period) def write_us(self, us): us = min(self.max_us, max(self.min_us, us)) duty = round(us * 1024 * self.freq // 1000000) self.pin.write_analog(duty) sleep(100) self.pin.write_analog(0) def write_angle(self, degrees=None): if degrees is None: degrees = math.degrees(radians) degrees = degrees % 360 total_range = self.max_us - self.min_us us = self.min_us + total_range * degrees // self.angle self.write_us(us) Servo(pin14).write_angle(0) display.show(Image.HAPPY) while True: Servo(pin14).write_angle(0) sleep(1000) Servo(pin14).write_angle(45) sleep(1000) Servo(pin14).write_angle(90) sleep(1000) Servo(pin14).write_angle(135) sleep(1000) Servo(pin14).write_angle(180) sleep(1000) ``` 4\. **Test Result** After downloading the code to the board successfully, **external power supply(turn the DIP switch to ON)**,and press the reset button on micro:bit. ![Img](./media/Python_bb3e1312.png) The LED dot matrix shows a smiley pattern and the servo rotates in the pattern 0°~45°~90°~135°~180°~0°.