Matter: Light switch

This light switch sample demonstrates the usage of the Matter (formerly Project Connected Home over IP, Project CHIP) application layer to build a basic controller device for the Matter light bulb sample. This device works as a Matter controller, meaning that it can control a light bulb remotely over a Matter network built on top of a low-power, 802.15.4 Thread network. You can use this sample as a reference for creating your own application.

Requirements

The sample supports the following development kits:

Hardware platforms

PCA

Board name

Build target

nRF52840 DK

PCA10056

nrf52840dk_nrf52840

nrf52840dk_nrf52840

nRF5340 DK

PCA10095

nrf5340dk_nrf5340

nrf5340dk_nrf5340_cpuapp

nRF21540 DK

PCA10112

nrf21540dk_nrf52840

nrf21540dk_nrf52840

For this sample to work, you also need the Matter light bulb sample programmed to another supported development kit.

Note

Matter requires the GN tool. If you are updating from the nRF Connect SDK version earlier than v1.5.0, see the GN installation instructions.

Overview

You must set the light switch in the test mode and pair it with a light bulb device to be able to send Matter light bulb control commands such as toggling the light and setting its brightness level.

Test mode

Unlike other samples, such as Matter door lock, this sample does not support Matter commissioning over Bluetooth® LE. To enable communication between the light switch and the light bulb devices, they must be initialized with the same static Thread network parameters and static Matter cryptographic keys.

Press Button 3 to activate the test mode before enabling the pairing phase on the device.

Pairing

The pairing phase starts when you have activated the test mode and both devices are initialized with the same network parameters. During this phase, the light bulb device periodically sends multicast messages with static content. Once the light switch device receives such a message, it becomes aware of the IP address of the nearby light bulb device and the pairing is done.

Configuration

See Configuring your application for information about how to permanently or temporarily change the configuration.

FEM support

You can add support for the nRF21540 front-end module to this sample by using one of the following options, depending on your hardware:

  • Build the sample for one board that contains the nRF21540 FEM, such as nrf21540dk_nrf52840.

  • Manually create a devicetree overlay file that describes how FEM is connected to the nRF5 SoC in your device. See Set devicetree overlays for different ways of adding the overlay file.

  • Provide nRF21540 FEM capabilities by using a shield, for example the nRF21540 EK shield that is available in the nRF Connect SDK. In this case, build the project for a board connected to the shield you are using with an appropriate variable included in the build command. This variable instructs the build system to append the appropriate devicetree overlay file. For example, to build the sample from the command line for an nRF52833 DK with the nRF21540 EK attached, use the following command within the sample directory:

    west build -b nrf52833dk_nrf52833 -- -DSHIELD=nrf21540_ek
    

    This command builds the application firmware. See Programming nRF21540 EK for information about how to program when you are using a board with a network core, for example nRF5340 DK.

Each of these options adds the description of the nRF21540 FEM to the devicetree. See Working with RF front-end modules for more information about FEM in the nRF Connect SDK.

To add support for other front-end modules, add the respective devicetree file entries to the board devicetree file or the devicetree overlay file.

User interface

LED 1:

Shows the overall state of the device and its connectivity. The following states are possible:

  • Solid Off - The device is not paired with any light bulb device.

  • Short Flash On (100 ms on/900 ms off) - The device is in the pairing phase.

  • Solid On - The device is paired with a light bulb device.

Button 1:

Initiates the factory reset of the device.

Button 2:

Toggles the light on the paired light bulb device.

Button 3:

Enables pairing for 10 seconds. It also starts the Thread networking in the test mode if the Thread network is not yet initialized.

Button 4:

Brightens the light on the paired light bulb device. The brightness level is increased gradually while the button is being pressed. When the button is pressed for too long, the brightness level wraps around and becomes the lowest possible.

SEGGER J-Link USB port:

Used for getting logs from the device or for communicating with it through the command-line interface.

Building and running

This sample can be found under samples/matter/light_switch in the nRF Connect SDK folder structure.

See Building and programming an application for information about how to build and program the application.

Testing

After building this and Matter light bulb samples, and programming them to your development kits, complete the following steps to test communication between the devices:

  1. Complete the following actions for both devices:

    1. Connect the kit to the computer using a USB cable. The kit is assigned a COM port (Windows) or ttyACM device (Linux), which is visible in the Device Manager.

    2. Connect to the kit with a terminal emulator (for example, PuTTY). See How to connect with PuTTY for the required settings.

  2. On both devices, press Button 1 to reset them to factory settings.

  3. Pair both devices by completing the following steps:

    1. On the light switch device, press Button 3 to enable pairing on this device. The light switch becomes the Thread network Leader. The following messages appear in the console for the light switch device:

      I: Device is not commissioned to a Thread network. Starting with the default configuration
      I: Starting light bulb discovery
      
    2. On the light bulb device, press Button 3 to enable pairing on this device. The following messages appear on the console for the light bulb device:

      I: Device is not commissioned to a Thread network. Starting with the default configuration
      I: Started Publish service
      

      At this point, the light bulb is discovered by the light switch device and the following messages appear on the console for the light switch device:

      I: Stopping light bulb discovery
      I: Pairing with light bulb fdde:ad00:beef:0:7b0:750e:6d96:49e9
      
  4. On the light switch device, press Button 2 to turn off the light on the light bulb device. The following message appears on the console for the light switch device:

    I: Toggling the light
    

    LED 2 on the light bulb device turns off.

  5. On the light switch device, press Button 2 to turn on the light again. LED 2 on the light bulb device turns back on.

  6. On the light switch device, press Button 4 and hold it for a few seconds. Messages similar to the following one appear in the console:

    I: Setting brightness level to 7
    

    The brightness of LED 2 on the light bulb device changes while the button is pressed.

Dependencies

This sample uses the Matter library that includes the nRF Connect SDK platform integration layer:

In addition, the sample uses the following nRF Connect SDK components:

The sample depends on the following Zephyr libraries: