Project 2:Button-Controlled LED

Various ways to control lights daily—wall switches, mobile apps—convenient interaction for flexible use. Last class, we learned digital output signals with external LEDs. Today we go further: use UNIHIKER K10's onboard AB keys to manually control LEDs.
 

Project Objectives
 

Knowledge Goals
 

1.Learn the concept of digital input 
2.Read digital pin values on the UNIHIKER K10. 
3.Use the on-board buttons to control LEDs.
 

Practical Goals 
 

Use the on-board A/B buttons to interactively turn the LED on and 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 pin of the UNIHIKER K10 via module cable, then connect the UNIHIKER K10 to the computer using a USB Cable.


Software


Open Mind+ v2, switch to "Upload Mode", then connect the UNIHIKER K10 as shown below and load the UNIHIKER K10 library.

Hands-on Practice


Next, we'll learn LED on/off via button control with two methods.

Code


Method 1: Button Event-Triggered Control


In UNIHIKER K10's graphical instruction "Sensors" category, find “when button (A) (pressed) “ block, drag to the scripts area, set digital pin P0 to high level below. Then pressing button A lights the LED. Full code is shown in the right figure below.

Similarly, right-click “when button (A) (pressed)” block, copy-paste the whole code, and modify the button and output level. This allows button A to light the LED and button B to turn it off.

Method 2: Continuous Detection Control


“Button (A) pressed ?” This block can also achieve the same effect by using the“ if () then“ instruction in the "Control" command library to judge the key status.

Button status detection can't run just once but must be judged continuously to stably detect if the key is pressed. Hence the following code:

Run the Code


Click the“Upload”button,wait for completion, then press button A/B to observe the LED's on/off status.

Block Review

Category

Blocks

Function

An interrupt is triggered when the on-board key A/B is pressed/ released.Infinite loops and long delays should not be used in interrupts, and the code in the interrupt should be as short as possible to ensure system stability.
Detect the status of onboard button A/B (pressed/released).
This block is used to check whether a certain condition is met.
You need to place the condition between “if” and “then.”


Knowledge Base


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

Button


What is a button? It is a manual controller, also called a pushbutton switch. It only connects or disconnects current briefly, so it is often used in control circuits to send command signals to other electrical components/equipment for control.


UNIHIKER K10 On-board Buttons


In the previous practice, we used UNIHIKER K10's on-board buttons A and B, located at the top-left corner of the UNIHIKER K10's front side. These buttons are surface-mount tactile pushbuttons, mainly composed of a switch cap, plunger, return spring, base, etc.

How does it work? When pressing button A/B, the plunger pushes the return spring down to contact fixed terminals on both sides, closing the circuit. When released, the spring rebounds upward due to elasticity, opening the circuit.

When reading button A/B status, detect if pressed via program and execute corresponding actions based on the result.

Buttons have only two states: pressed and released. Pressing a button equals manual signal input; the UNIHIKER K10 program processes it, outputs high level to pin P0, and lights the LED. The input during pressing/releasing is a digital signal (also called digital input signal).


Digital Input Signal


What is a digital input signal?


In Project 1, we learned digital signals are represented by 0 and 1. A digital input signal is collected by digital modules with only two states: low/high level (electronic circuit perspective) or 0/1 (computer perspective). For our buttons: 0 = released, 1 = pressed.


How to read digital input signals?


Button A can be read via “Button (A) pressed?” for direct judgment, or by checking if it equals 1.

But this is not a universal method. The general approach is the “read digital (P0)” block—select to read signals from digital pins in the dropdown menu.

Externally connectable digital pins of UNIHIKER K10: P0~P10, P12~P15. Among them, P0 and P1 are two on-board digital pins. Usage of other pins will be covered in subsequent courses.
If you have other buttons, connect them externally—e.g., a button to P0 (digital input) and an LED to P1. You can then create another light control case as shown below.

Challenge Task


In the previous project, you've controlled the LED on/off with Button A/B. Now, design another light effect (e.g., blink 4 times) to trigger when Buttons A and B are pressed simultaneously (A+B)!
The reference code is as follows:

icon Project 2.zip 660KB Download(0)
License
All Rights
Reserved
licensBg
0