Project 3: Breathing Light

Daily light control is more than on/off—flexibly adjust brightness (e.g., bedtime dimming, reading-friendly light). Last lesson, we used UNIHIKER K10's Buttons A/B for LED on/off. This lesson adjusts LED brightness smoothly via buttons, realizing advanced control from "on/off" to "dim/bright".


Project Objectives


Knowledge Goals


1.Understand analog signals and analog output. 
2.Learn PWM control. 
3.Use PWM output on the UNIHIKER K10. 
4.Use variables in simple programs.


Practical Goals


With UNIHIKER K10, control the LED to gradually light up and turn off.
 

Materials List

HARDWARE LIST
1 UNIHIKER K10
1 LED Module
1 Module Cable
1 USB Cable

Preparations


Hardware


Connect the LED module to the P0 of the UNIHIKER K10 via module connection cable, then connect the UNIHIKER K10 to the computer using 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 complete two tasks to learn LED brightness control methods and simplify the breathing light program with variables.
Task 1: Gradually Light Up the LED
Use PWM control of UNIHIKER K10 to gradually light up the LED.
Task 2: Improve the Breathing Light Effect
Use variables to simplify the program and improve the breathing light effect.


Task 1: Gradually Light Up the LED


Code


To control LED brightness, use PWM. In the“Pin Operation”of“UNIHIKER K10”, drag ”analog (P0) output PWM (512)“ to the scripts area. Click the number section and drag the slider to see the value range of 0–1023, as shown below.

Now, you can achieve gradual LED brightening by copying, pasting and modifying parameters. To clearly observe the brightening process with the naked eye, update the PWM value at regular intervals. See the full code below.
Note: See the Knowledge Hub for the PWM control principle.


Run the Code


Click the“Upload”button, wait for the upload to complete, and observe the gradual cyclic brightening of the LED.

Have a Try


In the previous practice, we only saw the LED cycle to brighten but not dim. Try adding this step yourself to achieve the breathing light effect of gradual brightening and dimming.


Code Review

Category

Blocks

Function

Sets the specified analog pin output PWM value with a value in the range of 0-1023.


Task 2: Improve the Breathing Light Effect


Code


In Task 1, we achieved gradual LED brightening by copying and modifying parameters. However, PWM values change too fast. To slow down the change, copying and pasting is obviously cumbersome. Is there a better method?
Using regular variable changes is an ideal method. First, create the variables “lightness” and “direction” under the“Variables”before we analyze it further.

Note: See the Knowledge Base for variable concepts and usage.
The specific logic is to replace the PWM value with a variable that gradually increases or decreases.Take a change interval of 64 as an example. The specific variation process is as follows:

The corresponding program is as follows:

We can combine them. Let's optimize the brightness change: convert increasing to 1*64 and dimming to -1*64. Now, use the "direction" variable multiplied by 64—only the "direction" needs to be modified. Details are as follows:

The complete program code is shown below:

Run the Code


Click the“Upload”button.After the upload completes, you will see the LED slowly brighten and then dim gradually for the breathing light effect.


Code Review

Category

Blocks

Function

This block assigns values to real-number variables. Click the variable name to select it.
This block increments a variable by the entered value. A positive input increases the variable; a negative input decreases it.

Knowledge Base


Next, let's learn and summarize the hardware knowledge used in this lesson.


Analog Signal


In this practice, the LED has not just on/off states but supports multi-level continuous dimming.
Signals with continuous values within a range are called analog signals. Most natural signals (e.g., sound, temperature) are inherently analog with infinitely many continuous values. However, limited by hardware processing capability, discrete values are used to approximate their continuous changes.
For example, the UNIHIKER K10 features 10-bit analog signal output resolution (2¹⁰), enabling continuous brightness simulation via 1024 discrete levels from 0 to 1023.


Differences Between Analog Signals and Digital Signals


Digital signals have only two values (0V & 3.3V), which correspond to high and low levels on the UNIHIKER K10—high level is digital 1 (3.3V), low level is digital 0 (0V). In other words, controlling an LED with digital signals only allows two states: on or off.

Analog signals have infinite values within 0–3.3V. Thus, controlling an LED with them enables multiple brightness levels, which vary continuously with the analog value (gradually brightening to dimming).

Then, does analog output adjust brightness by directly outputting different voltage values via the UNIHIKER K10? No. The UNIHIKER K10 uses PWM (Pulse Width Modulation) to achieve analog output. What is PWM?


PWM


PWM is a technology that obtains analog quantities via digital methods. Digital control generates a square wave, which has only two states (on/off, corresponding to the high/low levels of digital pins). Adjusting the on-off time ratio simulates a voltage varying between 0 and 3.3V. The duration of the high level is called pulse width—hence the name Pulse Width Modulation.
Understand PWM more intuitively through the following five square waves.

-The green vertical lines in the figure represent one cycle of the square wave.
-Duty Cycle:the ratio of the high-level duration to the total cycle duration in percentage.


PWM is widely used to adjust LED brightness and motor rotation speed, which also enables easy control of wheel speed driven by motors. Its advantages are particularly evident when operating small cars.


variable


We used variables to adjust PWM values. A simple explanation of a variable is a changeable quantity—it can store different values and be read/modified during program execution.We can compare it to a box, as shown in the figure below.

In the graphical programming of UNIHIKER K10, there are two variable types:
-Numeric Variable:store real numbers(Range: -3.4×1038 ~3.4×1038).
-String Variable:store character strings(hold non-numeric data such as text and symbols).

Challenge Task


In the previous project, we used loops and PWM to control the LED to continuously brighten and dim. Can we use buttons to adjust its brightness? Try it: press Button A to brighten the LED and Button B to dim it.
Tip: Achieve this by using Buttons A/B to control variable changes. See the reference code below:

icon project 3.zip 661KB Download(0)
License
All Rights
Reserved
licensBg
0