We learned to control LED brightness last class. In daily life, however, we usually use intuitive physical interactions like knobs to turn lights on/off and adjust brightness. Based on the previous lesson, this class will add knob control to create a brightness-adjustable LED project.
Project Objectives
Knowledge Objectives
1. Understand analog input signals.
2. Learn how Rotation Sensor (knob) work.
3.Read analog pin values on the UNlHlKER K10.
4. Read data quickly through serial output.
5. Convert values across different scales.
Practical Objective
Turn the knob to adjust the LED brightness in real time, based on the read analog voltage.
Materials List
Preparations
Hardware
Connect the knob and LED module to pins P0 and P1 of UNIHIKER K10 via module connection cable respectively, then link UNIHIKER K10 to the computer with a USB cable.
Note: See Knowledge Hub for the introduction to the rotation sensor (knob).

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 first learn to read the knob value, then use the knob to create a adjustable desk lamp effect.
Task 1: Read Knob Data
First, we will read the knob value and observe its changes via the serial monitor.
Task 2: Build a Adjustable Desk Lamp
In this task, we will read the knob value, map it to the PWM range for LED brightness control, and achieve real-time match between knob rotation angle and light brightness.
Task 1: Read Knob Data
Code
To read the knob value, use “read analog (P0)” block under the "Pin Operation" of “UNIHIKER K10”, as shown below.

How to continuously turn the knob and observe its value changes? Remember our serial monitor window?
We can use (serial0) write string“hello”(Wrap) under the "serialport operation" of “UNIHIKER K10” to read the values.
Note: See Knowledge Hub for the serial port introduction.

The complete program code is shown below:

Run the Code
Click the“Upload”button, wait for the upload to complete,then open the serialport.
Then turn the knob and observe the value changes.
Code Review
Category | Blocks | Function |
![]() | Read the analog input signal at the specified pin.
| |
![]() | Serial port output content. |
Task 2: Build a Adjustable Desk Lamp
Code
In Task 1, we read the knob’s analog value and found its range does not match the PWM range. We need to scale the knob’s analog value using “map” block, which is the second to last under the "Operators" .
Note: See Knowledge Base for the implementation principle of mapping.

Just modify the analog pin P1 mapping range from 0–4095 to 0–1023.

Next, based on the Task 1 code, implement PWM output as follows:
Have you noticed you read the analog value twice each time? Using a variable lets you read it only once per cycle, ensuring the displayed value perfectly matches the one controlling the LED.

Run the Code
Click the“Upload”button, wait for the upload to complete, then rotate the knob slowly to see the LED brightness change gradually.
Code Review
Category | Blocks | Function |
This block scales a data value proportionally from one range to another. |
Knowledge Base
Next, let's learn and summarize the hardware knowledge used in this lesson.
Analog Input Signal
The analog input signal refers to the 0–4095 analog value detected by the analog sensor. For the UNIHIKER K10, unlike the 0–1023 PWM value of analog output, it maps the 3.3V working voltage proportionally to the 0–4095 range.
So far, we've learned analog signal input and output. How to quickly identify analog signal modules? Similar to digital modules, the A mark on the module helps identify them quickly, e.g., rotation sensor, soil moisture sensors, etc.

Rotation Sensor
An angle sensor converts rotation angle into measurable electrical signals for angle measurement. The potentiometer type used today acts as a variable resistor: connected to a power supply at both ends, rotating it adjusts resistance, which changes voltage. The analog voltage value is read via analog port 1—this is the entire control process.

The voltage change principle: a potentiometer splits into two series resistors R1 and R2 at the adjustment position with constant current. Rotating it changes their resistances, altering the output voltage accordingly. Imagine it as cutting a cake—more cake (resistance) you get, more energy (Vout) you have. Voltage changes are directly reflected by the analog port reading (0~4095).

How to feed back the angle value——Calibrate the relation between rotation angle and analog reading, and linearly map the collected voltage value (0~4095) to the actual angle. The angle sensor we use ranges 0-300°;use the mapping block to proportionally convert the real-time reading from 0~4095 to 0~300°for the current rotation angle. If the rotation direction is reversed, just adjust the mapping target values .

Serial Port
A serial port or serial communication is a simple way for a main control board to communicate with the outside world.
What kind of communication do we commonly use the serial port for?
The most common is communication between the main control board and computer, e.g., printing debug info on the UNIHIKER K10 via serial port to view real-time sensor readings or program running status.
In addition, serial ports enable data exchange between main control boards and modules like sensors, known as UART communication—an asynchronous serial protocol for diverse data transmission via TX (transmit) and RX (receive) lines. Serial ports allow both acquiring sensor data and sending control blocks to modules; the millimeter wave radar sensor in the provided equipment supports this communication method.

UNIHIKER K10 module serial port communication setup block is as follows:

Challenge Task
In the last project, you learned to use a knob to control lamp brightness. For an adjustable desk lamp, its control logic can be further optimized—e.g., use A/B buttons for lamp on/off, and the rotation sensor for brightness adjustment (no blackout at minimum). Try it and complete this function.
Tip: Use variables to implement A/B button control. Besides, a pin cannot output digital and analog signals simultaneously, so lamp on/off cannot be achieved via digital output.
Reference implementation code is as follows:














