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:

GND: grounded, in brown
VCC: connect to +5v (3.3V) power, in red
S: signal pin to control PWM signal, in orange

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

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

This block reads servo angle.

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:
Click
and choose Load from your computer

We have already downloaded the codes on computer desktop, so open the file and choose
3-14-servo.sb3
Manually build blocks:
In
, find
block.In
, find and place
block under
.

In
, put
in block
and set degree to “0” and delay to “2000”.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:
Click
and choose Load from your computer

We have already downloaded the codes on computer desktop, so open the file and choose
3-14-servo2.sb3
Manually build blocks:
In
, find
block.In
, find and place
block under
.

In
, drag
and put it under 

In
, drag
to set times to 180, and put it into 
In
, put
into 

In
, drag and put
under 
In
, set
to the degree in block
, and set delay to 10 (unit: ms)
Duplicate
and change “++” into “- -” in
Complete Test Code

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.