Last class, we mastered the use of mmwave presence sensors proficiently, which also marks the end of learning sensors related to UNIHIKER K10. This class will start learning actuator-related knowledge, beginning with fans. We will combine the on-board temp-humi sensor, aim to control the fan speed by temperature, and finally make a variable speed fan.
Project Objectives
Knowledge Objectives
1. Learn the principle of the temp-humi sensor.
2. Learn the principle of the DC motor.
3. Learn the types of motors.
4. Master the usage method of the temp-humi sensor of UNIHIKER K10.
Practical Objective
Make a variable speed fan: Control the fan with a temp-humi sensor.It starts when the temperature exceeds the min threshold; between the min and max thresholds, the speed adjusts gradually as the temperature approaches the max threshold; it runs at max speed when the temperature reaches or exceeds the max threshold.
Materials List

Preparations
Hardware
Connect the fan to P0 of UNIHIKER K10 with a module cable, then connect UNIHIKER K10 to the computer with a USB cable.

Note: The DC motor fan consists of two parts: the motor body and the fan blade. Before use, install the fan blade onto the motor shaft and gently bend the blade upward. Make sure the blade is not blocked by the circuit board, as this will prevent it from rotating. For more information about the motor, please refer to the Knowledge Base.

Software
Open Mind+ v2, 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 use the temp-humi sensor and fan to achieve the target functions in two steps.
Task 1: Read Data from the Temp-Humi Sensor
In this task, we will read temperature values under different environmental conditions via the temp-humi sensor.
Task 2: Make a Variable Speed Fan
In this task, we will simulate temperature changes. Based on the temperature ranges you set, the fan will run at different speeds, and we will test its functionality.
Task 3: Optimize the Start and Stop Function
In this task, we will refine the variable-speed fan behavior by optimizing the threshold values for starting and stopping the fan, making the device operate more stably.
Task 1: Read Data from the Temp-Humi Sensor
Code
Measuring ambient temperature relies on the temp-humi sensor of UNIHIKER K10, see below for its position:

Note: See the Knowledge Base for the introduction to the ttemp-humi sensor.
To read the temperature value, use "read (temperature (°C))" block under the "Sensors" category in the UNIHIKER K10 library, as shown in the figure below:

Complete reading code is as shown below:

Run the Code
Click the“Upload”button, wait for the upload to complete, and observe the temperature value; change different environmental conditions (e.g., cover the temp-humi sensor with hand to simulate temperature rise, blow hard at the sensor to simulate temperature drop), and observe the temperature value change.

Code Review
Category | Blocks | Function |
![]() | Read the temperature (in Celsius or Fahrenheit) or humidity data (relative humidity) of the current environment via the on-board light sensor. |
Task 2: Make a Variable Speed Fan
Code
Like the LED light, the fan is connected to P0 of UNIHIKER K10. Therefore, We can control the fan speed using PWM. Specifically, we will use "analog P0 output PWM()" under the “Pin Operations” in the UNIHIKER K10 library, as shown in the figure below:

Set the upper and lower thresholds for the fan to start and reach max speed (e.g., 25°C and 40°C) . Temperature values can’t match the PWM range (0-1023) directly, so we need to map the temperature to the output PWM range accordingly. Use”map(0) from [(0),(1023)] to [(0),(255)]”in the “Operator”, as shown below.

The specific mapping principle is shown in the figure below:

Therefore, we map the temperature values between the upper and lower thresholds to the PWM range to control the fan speed. The corresponding block is shown in the figure below:

To better observe the fan gradually slowing down and coming to a stop, we can first use a changing variable to simulate temperature changes. The complete control program is shown in the figure below.
Note: The “Repeat Until” block is used here to stop the variable from changing. It is only for temporary testing.

Run the Code
Click the“Upload”button, and you’ll see the temperature continuously drop from 40°C, while the fan speed gradually slows down.
However, you may notice that the fan stops spinning before the temperature reaches 25°C. This happens because when the PWM value is too low, the corresponding output is not strong enough to drive the motor—meaning the minimum PWM value for keeping the fan spinning should not be 0. So how can we optimize this?
Task 3: Optimize the Start and Stop Function
Code
You’ve probably already figured it out—the most straightforward solution is to raise the lower limit of the PWM mapping range, for example setting it to 400.
However, because different motors have slight variations and their operating conditions can change each time they run, we can’t guarantee that every motor will stop immediately and consistently using this method alone.
A more reliable approach is to define a clear rule: when the temperature drops below the lower threshold, the fan must stop completely.
Note: We set PWM to 0 to stop the fan instead of switching to digital control. This is because using both digital output and PWM (analog output) on the same pin may cause unstable or abnormal PWM behavior.

To prevent the PWM value from exceeding its maximum limit, you can click the “+” button on the “If–else” block to add an additional condition that controls the PWM output when the temperature exceeds 40°C.

Finally, we can replace the simulated temperature-changing program with real temperature readings to complete all the functions of the variable-speed fan. The full program is shown below:

Run the Code
Click the“Upload”button, and then change the ambient temperature and observe how the fan speed changes to understand the relationship between temperature and fan speed.
Knowledge Base
Next, let's learn and summarize the hardware knowledge used in this lesson.
Working Principle of Temp-Humi Sensors
1.Definition
A temp-humi sensor is an electronic device integrating temperature and humidity detection elements, whose core function is to simultaneously collect ambient temperature (℃, ℉) and relative humidity (RH%).
2.Working Principle
Raw electrical signals from sensitive elements are weak; the sensor’s internal signal conditioning circuit amplifies, filters and calibrates them, finally converting the conditioned signals to digital/analog signals (e.g., voltage, current) for subsequent devices to read and use.
3.Temperature and Humidity Sensor(AHT20)
The UNIHIKER K10's on-board temp-humi sensor is AHT20, featuring high precision: temp accuracy ±0.3°C, humidity accuracy ±2% RH, resolution up to 0.01°C and 0.024%RH respectively. Operate within -40~85°C (temp) and 0~100% RH (humidity), and keep away from strong interference sources for accurate detection.
Also, the on-board temp-humi sensor may be affected by the board's circuit temp. For more accurate ambient temp measurement, an external DHT11/22 temp-humi sensor can be used, as shown below.

Note that the above temp-humi sensors are not waterproof. For measuring temp and humidity in liquid, soil and other such environments, a special waterproof temp-humi sensor is required, as shown in the figure below.

Working Principle of DC Motor
1.Concept of Electric Motor
An electric motor is a general energy conversion device that converts electrical energy into mechanical energy, outputting power via shaft rotation to drive various devices.
2.Types of Electric Motor
Electric motors are mainly divided into two core types: DC motors and AC motors.
(1)DC motors
A DC motor needs DC power (e.g., from batteries, DC regulated power supplies) to work. Its commutator ensures continuous unidirectional shaft rotation, enables precise speed control, suits stable power scenarios, and is widely used in toy car motors, drones and other equipment.
(2)AC motors
AC motors need AC power (e.g., household mains, AC generator power) to work. Their stator creates a rotating magnetic field to drive rotor rotation, enables efficient electrical-to-mechanical energy conversion, suits high-power and continuous operation scenarios, and is widely used in electric fans, washing machines, large machine tools and other devices.
Besides the above classifications, motors are divided into three types by control mode (analog/digital/intelligent control motors) and three by application scenario (power/positioning/micro motors).
3.Working Principle of DC Motor
(1)Names of Core Components
Core components of a DC motor: commutator, carbon brushes, magnets, coils (as shown below). It works by the energized coil rotating under force in a magnetic field.

(2)Working Principle


DC motor fan
The DC motor fan for this project has two parts—a DC motor and fan blades (as shown below); insert the fan blades into the DC motor for use, and the blade part can be replaced with other small high-speed structures. Note that this DC motor has a high rotational speed but cannot drive heavy objects.
According to this lesson’s content, complete the wiring and upload the program, and the motor will run cyclically by accelerating from low to maximum speed then decelerating gradually, and it also supports speed adjustment via PWM (Pulse Width Modulation).

Challenge Task
In summer, temperature impacts comfort while high humidity causes stuffiness and stickiness when working/studying at home. Build a "Smart Office Fan" with humidity adaptation (added function) based on this lesson, matching daily use better.
Tip: If humidity≥60%RH, fan speed+1 gear (PWM+200, max ≤1023) regardless of temperature range. Humidity does not affect fan speed when temp>40℃ (speed at max), so only modify two "if" conditions for temp <40℃.
Partial reference code is shown below:
(1)temp<25℃:

(2)Temp between 25℃ and 40℃.











