3.14 Servo

3.14.1 Overview

The 9g servo features small size but high performance and precision and is with good torque and accuracy, so it is perfect for small machines. With up to 180 degrees rotation angle, it enables extremely precise rotation and control and can be started fast with low noise.

3.14.2 Schematic Diagram

Angle range: 180°(there are 360°, 180° and 90°)

Drive voltage: 3.3V / 5V

Usually three wires:

6-14

GND: grounded, in brown

VCC: connect to +5v (3.3V) power, in red

S: signal pin to control PWM signal, in orange

6-14

Control principle: The rotation Angle of the servo is controlled by adjusting the duty cycle of the PWM (pulse width modulation) signals. Theoretically, the period of the standard PWM signal is fixed at 20ms (50Hz), so the pulse width should be 1ms ~ 2ms. But in fact, it is 0.5ms ~ 2.5ms, corresponding to the servo angle of 0° ~ 180°. Note that the angle for the same signal varies from servo brands.

3.14.3 Code Blocks

  1. j43

    This block controls servo angle. You only need to set a pin, channel, degree(range of 0-180) and delay(1000=1S).

  2. j45

    This block reads servo angle.

  3. j6

    This is a repeat block with repeating times. You can set the times. For instance, here it is 10, so codes in it will repeatedly execute for 10 times.

3.14.4 Test Code

3.14.4.1 Build Code

There are two ways to upload the code: directly open the code file we provide; or manually build blocks.

Directly open the code file we provide:

  1. Click and choose Load from your computer

  1. We have already downloaded the codes on computer desktop, so open the file and choose 3-14-servo.sb3

Manually build blocks:

  1. In events, find j1 block.

  2. In control, find and place j2 block under j1.

6-1-4-1-1

  1. In servo, put j43 in block j2 and set degree to “0” and delay to “2000”.

  2. Duplicate the block twice and set degree to “90” and “180” respectively.

Complete Test Code

3.14.4.2 Test Result

After uploading code, the servo rotates to 0 degree, 90 degree and 180 degree accordingly, with each position staying for 2 seconds, and then it back to 0 degree. It repeats these rotations.

3.14.5 Extension

We control servo to repeatedly and gradually rotate from 0 degree to 180 degree and then gradually back to 0 degree.

3.14.5.1 Test Code

There are two ways to upload the code: directly open the code file we provide; or manually build blocks.

Directly open the code file we provide:

  1. Click and choose Load from your computer

  1. We have already downloaded the codes on computer desktop, so open the file and choose 3-14-servo2.sb3

Manually build blocks:

  1. In events, find j1 block.

  2. In control, find and place j2 block under j1.

6-1-4-1-1

  1. In variable, drag j7 and put it under j1

6-18

  1. In control, drag j6 to set times to 180, and put it into j2

  2. In variable, put j10into 6-14

6-14

  1. In , drag and put under j10

  2. In variable, set j8 to the degree in block , and set delay to 10 (unit: ms)

    6-14

  3. Duplicate 6-18 and change “++” into “- -” inj10

Complete Test Code

6-14

3.14.5.1 Test Result

After uploading code, the servo repeatedly and gradually rotates from 0 degree to 180 degree and then gradually back to 0 degree.