Skip to content

4.3_5.0_7.0inch_Lesson09_Zigbee_Light_Control

1. Course Introduction

This lesson uses two ESP32-H2 wireless modules running the ZigBee example programs: one acts as an end-device light, and the other acts as a switch coordinator. After the coordinator button is pressed, the end-device light toggles its state, and pairing, binding, and state-change information can be observed in the serial port.

In this lesson, the large-size CrowPanel Advance 4.3inch / 5.0inch / 7.0inch boards are mainly used to host the H2 wireless modules, and can run the zigbee_7.0.ino serial forwarding example to forward the information output by the H2 module to the computer's Serial Monitor.

Note: The ESP32-H2 wireless module has a built-in Type-C interface and can be connected directly to a computer to flash the ZigBee example code; the CrowPanel main controller is an ESP32-S3, and only the serial forwarding project is flashed. The board type, partition scheme, and ZigBee Mode of the H2 module and the CrowPanel main controller must not be mixed.

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 pairing, binding, and state toggling on the H2 module.
  • Be able to flash the CrowPanel main controller serial forwarding example and observe the H2 module output.
  • Be able to determine from the serial output whether the network has been successfully bound.

3. Required Materials

  • CrowPanel Advance 4.3inch / 5.0inch / 7.0inch large-size development boards, 2 units.
  • ESP32-H2 wireless modules, 2 units.
  • A Type-C USB data cable that supports data transfer.
  • Arduino IDE.
  • Install esp32 by Espressif Systems 3.3.8 in Boards Manager.
  • Official example: File > Examples > Zigbee > Zigbee_On_Off_Light.
  • Official example: File > Examples > Zigbee > Zigbee_On_Off_Switch.
  • CrowPanel serial forwarding project: lesson-09/zigbee_7.0/zigbee_7.0.ino.

Note: ZigBee wireless modules belong to the wireless-module link. For large-size boards, the Function Keys / function DIP switch must be switched to 01, which corresponds to WM (wireless module). If the DIP switch is not at 01, the H2 module may fail to communicate properly with the main controller's serial port after being plugged into the CrowPanel.

Code and Resource Download

Code download: - lesson-09/zigbee_7.0

4. Software Operation Steps

4.1 Selecting the Compilation Method

This lesson offers two ways to prepare the H2 module code:

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

Open ZigBee official example

  1. If the H2 example project is already provided in the course code, you can also open the corresponding .ino file directly. The configuration method is the same as for the Arduino IDE official example.

4.2 Flashing the ESP32-H2 Switch Coordinator

The ESP32-H2 wireless module has a Type-C interface and can be connected directly to a computer using a Type-C data cable, then flashed in Arduino IDE by selecting the port corresponding to that H2 module.

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

Open switch coordinator project

  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 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 sequence:
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)

Switch coordinator configuration

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

  1. Click Upload to compile and wait for the upload to complete. If it stalls at Connecting..., check the Type-C data cable and port occupancy, and enter the download state as required by the module.

image-20260729165434276

4.3 Flashing the ESP32-H2 End-Device Light

  1. Open the end-device light project Zigbee_On_Off_Light.ino using 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 another H2 module.

IMG_8124

  1. Complete the end-device light configuration in the Tools menu in sequence:
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)

End-device light configuration

Note: The end-device light must select Zigbee ED (end device) and Zigbee 4MB with spiffs. If it is mistakenly set to coordinator mode, the two modules will not be able to complete the switch linkage according to this lesson's procedure.

  1. Click Upload to compile and wait for the upload to complete.

image-20260729165802680

image-20260729165837536

4.4 Flashing the CrowPanel Serial Forwarding Example

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

  1. Open the CrowPanel serial forwarding project using Arduino IDE:
lesson-09\zigbee_7.0\zigbee_7.0.ino

image-20260731190053998

  1. Connect the CrowPanel Advance 7.0 inch development board to the computer using a Type-C USB cable that supports data transfer.

IMG_8167

  1. Complete the CrowPanel main controller configuration in the Tools menu in sequence:
Board: ESP32S3 Dev Module
Port: Select the serial port corresponding to the current development board
Flash Size: 16MB (128Mb)
PSRAM: OPI PSRAM
Partition Scheme: Huge APP (3MB No OTA/1MB SPIFFS)
Serial Monitor baud: 115200

WeCom screenshot_17854958431477

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 types are flashed to different targets, so do not apply the H2's ZigBee Mode configuration to the CrowPanel main controller project.

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

image-20260731190443174

  1. Insert the flashed ESP32-H2 wireless modules into two CrowPanel Advance development boards of different sizes respectively. (You may use CrowPanel Advance development boards of different sizes to flash their corresponding code.)

  2. Open the Serial Monitor in the upper-right corner, set the baud rate to 115200, press the RST button on the wireless module, and confirm that you can see the bound device and light state-change information forwarded by the H2 module.

image-20260730163848689

image-20260729171119057

5. Hardware Operation Steps

  1. Install the H2 wireless module with the switch coordinator program flashed onto one CrowPanel Advance 7.0 inch development board.

  2. Install the H2 wireless module with the end-device light program flashed onto another CrowPanel Advance 5.0 inch development board.

  3. Switch the function DIP switch on both large-size boards to 01.

  4. Connect the development board and the computer using a USB cable that supports data transfer.

IMG_8168

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

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

  3. If you see messages such as Bound devices, Device on endpoint, Light state changed to 0/1 in the serial port, it indicates that the binding and control flow is working normally. image-20260729171228868

  4. If you need to re-pair, you can first erase the Flash and re-flash the examples on both ends, then have the coordinator re-open the network.

6. Key Code Explanation

The core objects in the ZigBee official example are as follows:

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, as well as what capabilities can be exposed to the peer later.

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, so learners can simultaneously see both the "network action" and the "hardware action."

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

#define SERIAL1_RX 19
#define SERIAL1_TX 20

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

The main controller's USB serial port is used for computer debugging, while Serial1 is used to receive the wireless module's output. The large-size project uses GPIO19 as RX and GPIO20 as TX, and both serial ports operate at 115200 baud.

if (Serial1.available()) {
  String message = Serial1.readStringUntil('\n');
  Serial.print("The message from serial port was received.: ");
  Serial.println(message);
}

Each time the H2 module outputs a line of text ending with a newline, the CrowPanel main controller forwards it to the USB serial monitor. If the sender does not end with a newline, readStringUntil('\n') will wait for a timeout, which manifests as output delay.

7. Experimental Observations

After the light side is powered on, it waits for the Zigbee network connection; after 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 Light state changed to 0 or Light state changed to 1 appears in the serial port. If you need to re-run the experiment, you can erase the Flash and re-flash the examples on both ends.

image-20260729171507008

8. Code Download

Code download link:lesson-09/zigbee_7.0