Creating a BLE App: Control LEDs with Nordic’s nRF Chips

Welcome to Episode 3 of our series on the nRF series of chips by Nordic Semiconductor. In this episode, we will create a simple Bluetooth Low Energy (BLE) application called “Blinky.” This application will allow you to control an LED on the nRF development board using a smartphone app. We will cover the fundamentals of BLE, understand GATT profiles, and learn how to advertise and connect to the nRF device.

Overview of BLE

Bluetooth Low Energy (BLE) is a wireless technology designed for low power consumption, making it ideal for battery-operated devices. BLE is widely used in applications such as fitness trackers, smart home devices, and medical sensors. It operates in the 2.4 GHz ISM band and is designed to provide short-range communication with minimal energy usage.

Key Features of BLE

  • Low Power Consumption: BLE is optimized for low energy use, allowing devices to run for extended periods on small batteries.
  • Fast Connection Times: BLE enables quick pairing and connection times, enhancing user experience.
  • Robust Range: BLE provides a reliable communication range, typically up to 100 meters in open space.

Understanding GATT Profiles

The Generic Attribute Profile (GATT) is a key component of BLE that defines how data is organized and exchanged between devices. GATT uses a client-server architecture, where the client (e.g., a smartphone app) requests data from the server (e.g., the nRF device).

GATT Components

  1. Services: A service is a collection of data and associated behaviors. For example, a “Battery Service” might provide information about the battery level.
  2. Characteristics: A characteristic is a value that can be read, written, or notified. Each characteristic is associated with a service. For example, a characteristic might represent the LED state (on/off).
  3. Descriptors: Descriptors provide additional information about a characteristic, such as its format or units.

Setting Up the Blinky Application

Step 1: Create a New Project

  1. Open SEGGER Embedded Studio.
  2. Create a new project based on the BLE example provided in the Nordic SDK. You can find this in the examples folder of the SDK.

Step 2: Configure the Project

  1. Set Up the SDK Path: Ensure that the project is configured to use the correct path to the Nordic SDK.
  2. Select the Target Device: Choose the appropriate target device (e.g., nRF52832 or nRF52840) in the project settings.

Step 3: Implement the Blinky Logic

  1. Define Services and Characteristics:
    • Create a service for controlling the LED. Define a characteristic for the LED state (e.g., LED_STATE_CHAR).Implement the logic to read and write to this characteristic.
    Example:
    static uint8_t led_state = 0;
    // 0: OFF, 1: ON // Function to handle characteristic write
    static void led_state_write_handler(uint16_t conn_handle, const ble_gatts_evt_write_t *p_evt_write) {
    if (p_evt_write->data[0] == 1) { nrf_gpio_pin_set(LED_PIN); // Turn LED ON led_state = 1; }
    else { nrf_gpio_pin_clear(LED_PIN); // Turn LED OFF led_state = 0;
    }
  2. Initialize BLE Stack: Initialize the BLE stack and set up advertising parameters. This allows the nRF device to be discoverable by smartphone apps.
    Example:
    ble_advertising_init_t init;
    memset(&init, 0, sizeof(init));
    init.advdata.name_type = BLE_ADVDATA_FULL_NAME; init.advdata.include_appearance = false; init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
  3. Start Advertising:
    Start advertising the device so that it can be discovered by BLE clients.Example:ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);

Step 4: Connect Using a Smartphone App

  1. Download a BLE Scanner App: Use a BLE scanner app like “nRF Connect” (available on iOS and Android).
  2. Scan for Devices: Open the app and scan for nearby BLE devices. You should see your nRF device listed.
  3. Connect to the Device: Tap on the device to connect. Once connected, you can explore the services and characteristics.
  4. Control the LED: Write to the LED characteristic to turn the LED on or off. You can send a value of 1 to turn it on and 0 to turn it off.

Example of Using nRF Connect

  • After connecting to your nRF device in the nRF Connect app:
    • Navigate to the LED service.
    • Find the characteristic for the LED state.
    • Write a value of 1 to turn on the LED and 0 to turn it off.

Conclusion

In this episode, we successfully created a simple BLE application called “Blinky” that allows us to control an LED on the nRF development board using a smartphone app. We covered the fundamentals of BLE, GATT profiles, and the steps to set up and implement the application.

Stay tuned for the next episode, where we will explore more advanced features of BLE, including notifications, data transfer, and integrating additional sensors into our applications!

Leave a comment