In the last class, we learned the principle and application of TT motors using a car. In this class, we will learn about servos.We will use an expansion board to connect the UNIHIKER K10 and a servo to make a Lunch Picker, helping people with difficulty making choices decide what to eat for lunch.
Project Objectives
Knowledge Objectives
1.Learn basic control methods of servo motors.
2.Control servos on the UNIHIKER K10.
3.Learn how to use the buttons on the UNIHIKER K10.
Practical Objective
Make a Lunch Picker with a servo: Turn on the expansion board power switch, the system generates a random angle value between 0 and 180, controls the servo to rotate to the corresponding position of the "Lunch Selection Turntable", and the screen displays the lunch category information corresponding to the current position pointed to by the servo to complete lunch selection.
Materials List
Preparations
Hardware
(1) Insert the servo into servo port S1 with correct polarity: align the yellow signal wire with the green pin on the expansion board.

(2) Stand the UNIHIKER K10 upright, and insert it into the expansion board slot with the screen facing forward.

(3) Connect the UNIHIKER K10 to the computer with a USB cable, and connect the expansion board to the computer with its power cable separately (the UNIHIKER K10 only needs to connect to the computer when uploading programs). Then switch the power to ON.

Turntable Making
Cut out the turntable for the Lunch Picker according to the diagram below.Each turntable is divided equally into 4 parts, with each part corresponding to an angle of 45°.

Software
Open Mind+ 2.0, switch to“Upload Mode”, connect the UNIHIKER K10 as shown in the figure below, and load the UNIHIKER K10 library.

Hands-on Practice
Next, we will make a Lunch Picker with the help of an expansion board and a servo to realize the function of free lunch selection.
Task 1: Rotate the Servo to a Specific Angle
First,we'll control the servo to rotate to a specific angle and show it on the screen.Also,we'll adjust the servo horn's mounting direction based on the angle.
Task 2: Make a Lunch Chooser
Then, we'll control the servo to rotate to any angle, select on the turntable and display the result on the screen to make a Lunch Picker.
Task 1: Rotate the Servo to a Specific Angle
Code
STEP1:Load the expansion board
Click on "Extension", find "motor:bit" unde r"Expansion Board", click to complete the addition. After clicking "Return", you can find the motor:bit in the "Instruction Area".

STEP2:Rotate servo to specific angle
To control the servo to rotate to a specific angle, we need to use the block "set pin (S1) servo to (90) degree" block under the "(0.0.1)motor:bit" module , as shown in the figure below:
Note: The rotation angle range of the servo is 0° to 180°. For the safety of the servo, the rotation angle generally does not include the boundary values.
The complete code code is shown below:

Run the Code
Click the“Upload”button. After the upload is complete, disconnect the UNIHIKER K10 from the computer, observe the rotation angle of the servo horn, and fine-tune it (e.g., when the servo is set to rotate 90°, the pointer of the servo horn should be perpendicular to the servo).

Code Review
Category | Blocks | Function |
![]() | ![]() | Set the servo on the specified pin to rotate to the set angle. Among them, the pins can be selected from S1 to S8, and the angle can be selected from 0° to 180°. |
Task 2: Make a Lunch Chooser
Code
To make the servo's rotation degree random, use the "pick random (0) to (10)" block in the“Operators”. To protect the servo, set it to pick a random number between 10°and 170°, which won't affect dish selection.
Also, it is necessary to determine the corresponding food displayed on the screen based on the rotation degree.The rotation degree will be used twice, so use a variable to record it for easy reuse when displaying content on the screen.
The corresponding code is as follows:

When determining the screen-selected image by rotation degree,we need to use the "If()then()else()" block, which is located in the“Control”. The specific angle classification is as follows, taking staple food as an example.

The corresponding code is as follows:

After selecting the staple food, complete the codes for choosing vegetables, meat, and soup to finish the whole lunch matching.
After completion, we find the main code too lengthy. So, we use the previously- learned code-encapsulation method to encapsulate the selection of four dish types into functions: staple, vegetable, meat, and soup in turn to simplify the main code. The encapsulation result is as follows (taking staple food selection as an example):

Also, to prevent the "loop execution" part of the main code from controlling the servo to rotate continuously, we set a variable“number”and use the value of number to achieve the goal that "each time the expansion board power switch is turned on, only one cycle of lunch selection is performed".
The complete code code is shown below:

Run the Code
Click the“Upload”button, disconnect the UNIHIKER K10 from the computer. The servo will rotate to random degrees in turn, and at the same time, the selected dishes will be displayed on the screen in turn.

Knowledge Base
Next, let's learn and summarize the hardware knowledge used in this lesson.
Servos
1.What is a Servo?
A servo, also called a servo motor, is an actuator controlling object rotation to a specified position (angle). Common types are 180°and 360° servos, and this project uses a 180°servo.
2.Types of Servos
Classified by "angle range" (the most essential classification):
- 180° servo: most commonly used. Precise 0°- 180° positioning. Suitable for small robot arms, toy car steering wheels, and adjustable-angle lamp brackets. In our kit, 180° servo is used.
- 90° servo: small angle range, simple structure, low cost. Suitable for robot finger joints, simple robot arm elbows, smart door locks, and small valves.
- 360°continuous rotation servo: no fixed angle positioning. Can rotate forward or reverse continuously with adjustable speed. Suitable for robot movement, automatic curtains, and small conveyor belts.
- 270°/multi-angle servos: Balance positioning and large-angle requirements. Suitable for bionic robots, gimbals, and complex robot arm joints.
3.Control Principle of Servos
(1)Components
Before learning the principle, let’s first take a look at the internal structure of a servo.It mainly consists of three parts: the reduction gear set, the circuit board, and the motor (commonly called a motor), as shown in the figure below:

These components work together, allowing the servo to precisely control the rotation of its output shaft based on the input signal.
- The reduction gear set converts the motor’s rotational motion into the required output torque and speed.
- The circuit board receives and processes the control signal to drive the motor to perform the corresponding action.
(2)Control Principle
The UNIHIKER K10, the "brain" of the servo, sends a command to the servo to rotate to a specified angle. Inside the servo, a "angle ruler" (potentiometer) measures its current angle in real - time and feeds the information back to the "brain".
The "brain" compares the target and current angles, and drives the servo's internal motor with precise PWM signals. The motor drives the servo shaft via the reduction gear set for smoother and more accurate rotation.
When the servo reaches the target angle, the "angle ruler" gives feedback, and the "brain" stops sending signals, halting the motor. If there's an angle error, the "brain"makes fine adjustments. This closed-loop control ensures servo accuracy.
(3)Relationship Between Servo Angle and PWM
Taking a standard small servo as an example, the relationship between them is as follows:

Rule: Every 45-degree angle increase causes a 0.5-ms increase in the high-level time of the PWM signal. The period of the whole PWM signal equals the sum of high- and low-level pulse durations. By adjusting pulse widths, we can precisely control the servo's angular rotation.
Challenge Task
This weekend, you’re meeting your good friends for a casual get-together at a cafe.Everyone can’t decide which afternoon tea set (drink+snack) to choose.Please modify the lunch picker mentioned above to make an "afternoon tea picker".
Tip: Press Button A to randomly select an afternoon tea combination; the screen will display the images of the chosen drink and snack at the same time.Press Button B to return the servo to its initial position (10°). The screen will clear the selection result and show "Press A to choose".
The turntable is made as follows:

Some of the reference code are as follows:











