1e41f4b71Sopenharmony_ci# LED Peripheral Control<a name="EN-US_TOPIC_0000001054530966"></a>
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview<a name="section14639174516337"></a>
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciBased on the Hi3861 platform, the OpenHarmony WLAN module provides abundant peripheral operation capabilities, including the I2C, I2S, ADC, UART, SPI, SDIO, GPIO, PWM, and flash memory. This document describes how to control GPIO pins by calling the OpenHarmony native development kit \(NDK\) interface to implement LED blinking. For details about how to control other IoT peripherals, see the API guide.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Development<a name="section13857170163412"></a>
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci1.  Complete the operations related to the mini system described in [Quick Start Overview](../quick-start/quickstart-overview.md).
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci    LED control examples are stored in the file **applications/sample/wifi-iot/app/iothardware/led\_example.c**.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci2.  Understand the cable connections by referring to the schematic diagram of the development board. You can learn that LED of hispark pegasus is connected to pin 9 of the chip.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci    ```
16e41f4b71Sopenharmony_ci    #define LED_TEST_GPIO 9
17e41f4b71Sopenharmony_ci    ```
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci    >![](../public_sys-resources/icon-note.gif) **NOTE**
20e41f4b71Sopenharmony_ci    >
21e41f4b71Sopenharmony_ci    >For details about the schematic diagram of the development board, contact the Hi3861 customer service personnel.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci3.  Initialize the GPIO pin, specify the pin usage, and create a task that turns on or off the LED periodically to implement LED blinking.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci    ```
26e41f4b71Sopenharmony_ci    static void LedExampleEntry(void)
27e41f4b71Sopenharmony_ci    {
28e41f4b71Sopenharmony_ci        osThreadAttr_t attr;
29e41f4b71Sopenharmony_ci    
30e41f4b71Sopenharmony_ci        /* Initialize the GPIO pin. */
31e41f4b71Sopenharmony_ci        IoTGpioInit(LED_TEST_GPIO);
32e41f4b71Sopenharmony_ci        /* Set pin 9 as the output direction. */
33e41f4b71Sopenharmony_ci        IoTGpioSetDir(LED_TEST_GPIO, IOT_GPIO_DIR_OUT);
34e41f4b71Sopenharmony_ci    
35e41f4b71Sopenharmony_ci        attr.name = "LedTask";
36e41f4b71Sopenharmony_ci        attr.attr_bits = 0U;
37e41f4b71Sopenharmony_ci        attr.cb_mem = NULL;
38e41f4b71Sopenharmony_ci        attr.cb_size = 0U;
39e41f4b71Sopenharmony_ci        attr.stack_mem = NULL;
40e41f4b71Sopenharmony_ci        attr.stack_size = LED_TASK_STACK_SIZE;
41e41f4b71Sopenharmony_ci        attr.priority = LED_TASK_PRIO;
42e41f4b71Sopenharmony_ci    
43e41f4b71Sopenharmony_ci        /* Start the task. */
44e41f4b71Sopenharmony_ci        if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
45e41f4b71Sopenharmony_ci            printf("[LedExample] Failed to create LedTask!\n");
46e41f4b71Sopenharmony_ci        }
47e41f4b71Sopenharmony_ci    }
48e41f4b71Sopenharmony_ci    ```
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci4.  Use a cyclic task in which the LED periodically turns on and off to implement LED blinking.
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci    ```
53e41f4b71Sopenharmony_ci    static void *LedTask(const char *arg)
54e41f4b71Sopenharmony_ci    {
55e41f4b71Sopenharmony_ci        (void)arg;
56e41f4b71Sopenharmony_ci        while (1) {
57e41f4b71Sopenharmony_ci            switch (g_ledState) {
58e41f4b71Sopenharmony_ci                case LED_ON:
59e41f4b71Sopenharmony_ci                    IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
60e41f4b71Sopenharmony_ci                    usleep(LED_INTERVAL_TIME_US);
61e41f4b71Sopenharmony_ci                    break;
62e41f4b71Sopenharmony_ci                case LED_OFF:
63e41f4b71Sopenharmony_ci                    IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
64e41f4b71Sopenharmony_ci                    usleep(LED_INTERVAL_TIME_US);
65e41f4b71Sopenharmony_ci                    break;
66e41f4b71Sopenharmony_ci                case LED_SPARK:
67e41f4b71Sopenharmony_ci                    IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
68e41f4b71Sopenharmony_ci                    usleep(LED_INTERVAL_TIME_US);
69e41f4b71Sopenharmony_ci                    IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
70e41f4b71Sopenharmony_ci                    usleep(LED_INTERVAL_TIME_US);
71e41f4b71Sopenharmony_ci                    break;
72e41f4b71Sopenharmony_ci                default:
73e41f4b71Sopenharmony_ci                    usleep(LED_INTERVAL_TIME_US);
74e41f4b71Sopenharmony_ci                    break;
75e41f4b71Sopenharmony_ci            }
76e41f4b71Sopenharmony_ci        }
77e41f4b71Sopenharmony_ci    }
78e41f4b71Sopenharmony_ci    ```
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci5.  Call **SYS\_RUN\(\)** of OpenHarmony to start the service. \(**SYS\_RUN** is defined in the **ohos\_init.h** file.\)
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci    ```
83e41f4b71Sopenharmony_ci    SYS_RUN(LedExampleEntry);
84e41f4b71Sopenharmony_ci    ```
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci6.  Add **led\_example.c** to the **applications/sample/wifi-iot/app/BUILD.gn** file for building.
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci    ```
89e41f4b71Sopenharmony_ci    import("//build/lite/config/component/lite_component.gni")
90e41f4b71Sopenharmony_ci    lite_component("app") {
91e41f4b71Sopenharmony_ci        features = [
92e41f4b71Sopenharmony_ci            "iothardware:led_example"
93e41f4b71Sopenharmony_ci        ]
94e41f4b71Sopenharmony_ci    }
95e41f4b71Sopenharmony_ci    ```
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci## Verification<a name="section1949121910344"></a>
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ciFor details about the compilation and burning processes, see [Building Source Code](../quick-start/quickstart-ide-3861-build.md) and [Burning an Image](../quick-start/quickstart-ide-3861-burn.md).
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ciAfter the preceding two steps are complete, press the **RST** button to reset the module. If the LED blinks periodically as expected, the verification is passed.
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci**Figure  1** LED blinking<a name="fig20768175218527"></a>  
105e41f4b71Sopenharmony_ci![](figures/led-blinking.gif "led-blinking")
106e41f4b71Sopenharmony_ci
107