Last lesson we learned to draw graphics on the screen. In practice, we need to adjust the display direction by device posture when showing content—e.g., images adapt synchronously as the device rotates—for a comfortable viewing experience. This lesson we’ll use the acceleration sensor to achieve automatic image orientation switching.
Project Objectives
Knowledge Objectives
1.Understand the function and principle of the accelerometer.
2.Understand the application of accelerometer in posture detection.
3.Master the on-board accelerometer of UNIHIKER K10.
4.Master the method to initialize the screen orientation of UNIHIKER K10.
Practical Objective
Based on acceleration data and the detected posture of UNIHIKER K10, realize automatic orientation switching of on-screen images.
Materials List
Preparations
Hardware
Connect the UNIHIKER K10 to the computer with a USB cable.

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’ll learn to read acceleration values in each coordinate direction of UNIHIKER K10, then realize automatic image rotation through acceleration threshold judgment.
Task 1: Reading Acceleration Data
In this task, we’ll read acceleration values and observe their characteristics in different directions by rotating the screen orientation.
Task 2: Automatic Image Rotation
In this task, we’ll learn to use the variation characteristics of acceleration to automatically adjust the orientation and position of images per the different postures of UNIHIKER K10.
Task 1: Reading Acceleration Data
Code
Acceleration measurement of UNIHIKER K10 relies on the accelerometer; its location is as follows.

Note: See Knowledge Base for the introduction to the accelerometer.
To read acceleration values, use “read acceleration (mg) (X)” block under the “Sensors” in the UNIHIKER K10 library, as shown below.

Complete reading code is as shown below.

Run the Code
Click the“Upload”button. After upload completes, rotate UNIHIKER K10 to change its posture and observe acceleration value changes.

By adjusting the posture of UNIHIKER K10 and observing acceleration value changes, draw the following conclusions:

We can judge if the posture is upright by the magnitude and sign of acceleration: when the absolute value of vertical acceleration is around 980mm/s², the direction is roughly upright; when horizontal acceleration is about 0, it is horizontally upright.
Code Review
Category | Blocks | Function |
![]() | Get the value of the on-board acceleration sensor (X- direction, Y-direction, Z- direction or intensity) in milligravity (m-g). | |
![]() | Display text of the specified color starting from the leftmost side of the first line on the UNIHIKER K10 screen. |
Task 2: Automatic Image Rotation
Code
Next, we will use the characteristics of X and Y direction acceleration values of the UNIHIKER K10 in four postures to realize automatic image rotation. The summary based on the known characteristics of acceleration value changes in Task 1 is as follows:
K10 Posture | ![]() | ![]() | ![]()
| ![]() |
X-axis | absolute value close to 0 | close to 980 | absolute value close to 0 | close to -980 |
Y-axis | close to 980 | absolute value close to 0 | close to -980 | absolute value close to 0 |
Based on the above rules, we can use "if()then()else()" block for logical judgment to display images in different directions. Due to the instability of manual operation, only approximate ranges are adopted for judgment conditions. For example, for values close to 0, take the absolute value with a threshold of 50; for other values, set the threshold to ±800.
Among them, image display requires using the "cache local image at X()Y()" block under the "Screen" of the UNIHIKER K10 , as shown in the figure below:

Image coordinate settings here only refer to the corresponding screen orientation coordinates set in initialization. Screen orientation initialization uses “initialize screen orientation(0)”block under the“Screen”of the UNIHIKER K10 library, as shown below.

Note: Screen orientation can only be set on initialization; no effect if set elsewhere. In the project, you can set different initial orientations by your selected image orientation. Coordinate details for other angles are as follows:

Complete reading code is as shown below.

Run the Code
Click the“Upload”button. After upload completes, you can change the UNIHIKER K10's posture and observe the image display orientation.
Code Review
Category | Blocks | Function |
![]() | Used to set the screen display direction, there are four display directions, the number indicates the number of degrees the screen rotates counterclockwise, each display direction corresponds to a different coordinate range. | |
![]() | Logical AND operation connects two conditions, with the overall result true only when both conditions hold. | |
| Calculate the absolute value of the input value, removing its sign and retaining only its magnitude. | ||
| Check if the left value is less than the right one, true if satisfied, false otherwise. | ||
![]() | Check if the left value is greater than the right one, true if satisfied, false otherwise. |
Knowledge Base
Next, let's learn and summarize the hardware knowledge used in this lesson.
Principle of the accelerometer
Acceleration is a physical quantity describing the rate and direction of an object's velocity change, whose core is reflecting whether the velocity changes and how fast it changes.
The acceleration sensor captures an object's acceleration-related physical states (velocity changes, tilt, vibration) and converts them into electronic device-recognizable signals, with its core function of capturing the object's motion changes. The UNIHIKER K10's accelerometer mainly detects acceleration components in 3 directions, as shown below.

When stationary, its three acceleration components correlate with gravitational acceleration components, showing stable value changes matching the placement angle; when in motion, the force is a mix of motion and gravity, with more complex readings.
Take the y-axis acceleration value variation as an example: hold the UNIHIKER K10 vertically – with the edge connector facing down, only vertical downward gravity acts on the y-axis, and the screen shows a value close to 980; with the edge connector facing up, the vertical downward gravity points to the onboard sensor side, and the screen shows a value close to -980.

When you place the UNIHIKER K10 horizontally on a table, since there is no influence of gravitational acceleration in the y-axis direction, the numerical value displayed on the screen is close to 0.

Accelerometers are widely used in daily life: screen rotation, anti-shake photography and step counting on mobile phones/tablets, fitness trackers, rehabilitation devices, collision avoidance of smart home robot vacuums, all rely on them to detect motion.
Application of Accelerometer in Attitude Detection
Application of accelerometers in attitude detection is divided into three steps:
STEP1:collect acceleration data
Can read acceleration values in X, Y and other directions, reflecting the device's current motion or tilt state; for example, the acceleration values read in Task 1 change with the placement and attitude of the UNIHIKER K10.
STEP2:determine device orientation
Through changes in acceleration values in different directions, the tilt direction of the UNIHIKER K10 can be identified.
STEP3:Support display adaptation
Based on attitude judgment results, use acceleration value ranges in each axis (e.g., Task 2) or direct attitude detection (below) to realize automatic rotation of screen images with the device's attitude, matching the display direction to viewing needs.
To enable automatic image rotation, use “Is (Forward tilt) gesture?”block under the “Sensors” category in the UNIHIKER K10 library, as follows:

Use“if() then() else()”block to judge the screen's tilt direction, then display the corresponding directional image to enable automatic rotation of the image with the UNIHIKER K10's attitude. The mapping between screen attitude and tilt direction is as follows:

Corresponding code as follows:

Both methods rely on the UNIHIKER K10's accelerometer. Their core logic is to judge the device's attitude by sensing acceleration changes, ultimately supporting screen display adaptation, and both need logical judgment commands to link attitude with display. They do, however, have differences.
Dimension | Acceleration Threshold Judgement Method | Direct Attitude Detection Block Method |
Implementation | Custom acceleration threshold judgement | Direct block call, no value processing |
Features | High flexibility, controllable precision | Easy operation, concise code |
Applicable Scenarios | personalized judgment and high-precision demands, such as AI data processing. | Rapid basic response, simple programming scenarios |
Challenge Task
In this project, we enabled automatic screen rotation. Next, you can use the accelerometer's features to create a dynamic ball that moves in real time with the screen's attitude changes.
Tip: Update the drawing position of the ball in the program in real time by setting variables, then cover the newly drawn ball.
Reference code as follows:























