Skip to content

2.4_2.8inch_Lesson09_Zigbee_Light_Control

1. Course Introduction

In this lesson, the ESP32-H2 wireless module runs two ZigBee example programs: one acts as the remote light (end device) and the other as the switch coordinator. Learners can control the on/off state of the remote light using a button, and observe pairing, binding, and state-change information in the serial output.

The ESP32-H2 wireless module comes with a built-in Type-C interface, allowing it to connect directly to a computer to flash the ZigBee example code. You can also compile it using the example project provided in this lesson; the configuration method is the same as the official Arduino IDE example.

2. Learning Objectives

  • Be able to distinguish the roles of a ZigBee end device and a coordinator.
  • Be able to select the correct ZigBee mode and partition scheme for the ESP32-H2 wireless module.
  • Be able to complete button-based pairing, binding, and state switching.
  • Be able to determine from the serial output whether the network has been bound successfully.

3. Preparation

  • Two CrowPanel Advance 2.4-inch / 2.8-inch development boards.
  • Two ESP32-H2 wireless modules.
  • Type-C USB data cables that support data transfer.
  • esp32 by Espressif Systems version 3.3.8 installed in the Boards Manager.
  • Arduino IDE with an ESP32 Arduino development environment that has ZigBee support installed.
  • Official example: File > Examples > Zigbee > Zigbee_On_Off_Light
  • Official example: File > Examples > Zigbee > Zigbee_On_Off_Switch
  • CrowPanel serial-forwarding example: zigbee_2.4_2_8.ino

Code and Resource Download

Code download: - lesson-09/zigbee_2.4_2_8

4. Software Operation Steps

4.1 Selecting the Build Method

This lesson provides two ways to prepare the code:

  1. Use the ZigBee examples built into the Arduino IDE. Open File > Examples > Zigbee in sequence, and select the two examples Zigbee_On_Off_Light and Zigbee_On_Off_Switch.

2.4_2.8_Arduino-09_01

  1. Open the code using the Arduino IDE. Open the following files separately:
lesson-09\Zigbee_On_Off_Light\Zigbee_On_Off_Light.ino
lesson-09\Zigbee_On_Off_Switch\Zigbee_On_Off_Switch.ino

4.2 Flashing the ESP32-H2 Wireless Module Examples

The ESP32-H2 wireless module is equipped with a Type-C interface, so it can be connected directly to a computer using a Type-C cable, and you can select the port corresponding to that H2 module in the Arduino IDE for flashing.

  1. Open the switch coordinator project Zigbee_On_Off_Switch.ino using the Arduino IDE.

image-20260729164308462

  1. Connect the first ESP32-H2 wireless module to the computer using a Type-C USB cable that supports data transfer. The H2 wireless module has a Type-C interface and can be connected directly to the computer to flash the ZigBee example code without going through the CrowPanel.

IMG_8124

  1. Complete the switch coordinator configuration in the Tools menu in order:
Board: ESP32H2 Dev Module
Core Debug Level: Info
Erase All Flash Before Sketch Upload: Enabled
Flash Size: 4MB (32Mb)
Partition Scheme: Zigbee ZCZR 4MB with spiffs
Zigbee Mode: Zigbee ZCZR (coordinator/router)

2.4_2.8_Arduino-09_04

Note: For the switch coordinator, you must select Zigbee ZCZR (coordinator/router) and Zigbee ZCZR 4MB with spiffs. Changing only the Tools menu will not alter the firmware already flashed into the module; after making changes, you need to click Upload again.

  1. Click Upload in the top-left corner of the Arduino IDE to compile and wait for the upload to finish. If it gets stuck at Connecting..., check the Type-C cable and port occupancy, and re-enter the download mode as required by the module.

image-20260729165434276

image-20260729165506715

  1. Open the end-device light project Zigbee_On_Off_Light.ino using the Arduino IDE.

image-20260729165539335

  1. Connect the second ESP32-H2 wireless module to the computer using a Type-C USB cable, or switch to the serial port corresponding to the other H2 module.

image-20260729165558700

  1. Complete the end-device light configuration in the Tools menu in order:
Board: ESP32H2 Dev Module
Core Debug Level: Info
Erase All Flash Before Sketch Upload: Enabled
Flash Size: 4MB (32Mb)
Partition Scheme: Zigbee 4MB with spiffs
Zigbee Mode: Zigbee ED (end device)

2.4_2.8_Arduino-09_08

Note: For the end-device light, you must select Zigbee ED (end device) and Zigbee 4MB with spiffs. If you mistakenly select coordinator mode, the two modules will not be able to complete the switch linkage as described in this lesson.

  1. Click Upload in the top-left corner of the Arduino IDE to compile and wait for the upload to finish.

image-20260729165802680

image-20260729165837536

4.3 Flashing the CrowPanel Serial-Forwarding Example

If you need to view the information output by the H2 module in the CrowPanel main-controller serial port, open the zigbee_2.4_2_8.ino provided in this lesson. This example forwards the data received on the wireless module's serial port to the Arduino IDE Serial Monitor.

  1. Open the CrowPanel serial-forwarding project zigbee_2.4_2_8.ino using the Arduino IDE.

image-20260729170244823

  1. Connect the CrowPanel to the computer using a Type-C USB cable that supports data transfer.

IMG_8125

  1. Complete the CrowPanel main-controller configuration in the Tools menu in order:

  2. Board: ESP32S3 Dev Module

  3. Port: Select the serial port corresponding to the current development board

  4. Flash Size: 16MB (128Mb)

  5. PSRAM: OPI PSRAM

  6. Partition Scheme: Huge APP (3MB No OTA/1MB SPIFFS)

image-20260729151754125

Note: The CrowPanel main controller uses the ESP32S3 Dev Module configuration, while the H2 wireless module uses the ESP32H2 Dev Module configuration. The two firmware flashing targets are different, so do not apply the H2's Zigbee Mode configuration to the CrowPanel main-controller project.

  1. Click Upload in the top-left corner of the Arduino IDE to compile and wait for the upload to finish. If it gets stuck at Connecting..., check the port occupancy and use the BOOT/RESET download mode as required by the board.

image-20260729170643253

image-20260729170800255

  1. Flash the code to the other CrowPanel Advance development board using the same flashing steps.

  2. Insert the two H2 wireless modules into the two CrowPanel Advance development boards, open Serial Monitor in the top-right corner, set the baud rate to 115200, and press the RST button on the wireless module to confirm that you can see the bound-device and light-state-change information forwarded by the H2 module.

image-20260730163857963

image-20260729171119057

5. Hardware Operation Steps

  1. Install the H2 wireless module that has the switch coordinator program flashed onto one CrowPanel Advance development board. (A 2.4-inch CrowPanel Advance development board is used here.)

  2. Install the H2 wireless module that has the end-device light program flashed onto the other CrowPanel Advance. (A 2.8-inch CrowPanel Advance development board is used here.)

  3. Connect the development board to the computer using a USB cable that supports data transfer.

IMG_8126

  1. After both boards are powered on, wait for the ZigBee network to be established and binding to complete.

  2. Press the BOOT button on the switch coordinator side and observe whether the end-device light toggles its state.

  3. When you open the serial monitor, if you see messages such as Bound devices, Device on endpoint, and Light state changed to 0/1, it indicates that the binding and control flow are working correctly.

image-20260729171228868

  1. If you need to re-pair, you can first re-flash and erase the Flash, then let the coordinator side reopen the network.

6. Key Code Explanation

ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT);
ZigbeeSwitch zbSwitch = ZigbeeSwitch(SWITCH_ENDPOINT_NUMBER);

These two objects represent the ZigBee devices on the light side and the coordinator side, respectively. They determine the device's role in the network and what capabilities it can expose to the peer.

zbLight.onLightChange(setLED);
zbSwitch.onLightStateChange(onLightStateChange);

These two lines tie the network state to the on-board behavior. When the light state changes, it directly drives the LED; when the coordinator receives a state report, it prints a log directly. This lets learners see both the "network action" and the "hardware action" at the same time.

The CrowPanel serial-forwarding example uses the following code to read the H2 module's serial port:

Serial1.begin(115200, SERIAL_8N1, SERIAL1_RX, SERIAL1_TX);

The main-controller USB serial port is used for computer debugging, while Serial1 is used to receive the wireless module's output. This way, even if the H2 module handles ZigBee communication on its own, you can still observe the experiment's information through the CrowPanel's Serial Monitor.

7. Experimental Observations

After the light side is powered on, it waits for ZigBee network connection; once the coordinator side completes binding, the serial port displays the bound-device information. When the coordinator button is pressed, the remote light toggles on or off accordingly, and the serial output shows Light state changed to 0 or Light state changed to 1. To repeat the experiment, you can erase the Flash and re-flash both ends' examples.

image-20260729171507008

8. Code Download

Code download: lesson-09/zigbee_2.4_2_8