yahboom
HomeTutorialsProduct
Solution
Blog
Online Store
About Us
EN/中文
Store
ESP32 Core Board📖
1. ESP32 Introduction
  • 1. ESP32S3 Overview
  • 2. Environment Setup Introduction
  • 3. Dev Board Introduction
2. ESP-IDF Development Tutorial
1. Before Development
  • 1. Environment Setup (Windows)
  • 2. Environment Setup (Linux)
  • 3. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. DMA
  • 8. ADC
  • 9. PWM Breathing LED
  • 10. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. PSRAM External Memory Experiment
  • 5. Flash Read and Write Experiment
  • 6. USB Emulated Mass Storage Experiment
4. FreeRTOS operating system
  • 1.FreeRTOS Core Concepts Theory
  • 2.Task Creation and Management
  • 3.Queue
  • 4.Binary Semaphore
  • 5.Mutex
  • 6.Event Groups
  • 7.Software Timer
  • 8.Task Scheduler
  • 9.Low Power Mode
  • 10.Deadlock
  • 11.Priority Inversion
5. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. Socket Programming Basics
  • 9. HTTP Client Experiment
  • 10. HTTP Server Experiment
  • 11. MQTT IoT Communication Protocol
  • 12. WIFI OTA Upgrade
  • 13. Bluetooth Fundamentals
  • 14. Bluetooth Slave Mode
  • 15. Bluetooth Master Mode
6. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
7. Xiaozhi AI Voice
  • 1. Burn Xiaozhi Firmware
  • 2. Network and Configuration
  • 3. AI Agent Settings
  • 4. Program Analysis
  • 5. Free Chat
3. Arduino Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. ADC
  • 8. PWM Breathing LED
  • 9. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication
  • 2. I2C OLED Display
  • 3. SPI Bus Communication
  • 4. Flash Read and Write
  • 5. USB Emulated Mass Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Server Experiment
  • 5. UDP Client Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT IoT Communication Protocol
  • 11. WIFI OTA Upgrade
  • 12. Bluetooth Fundamentals
  • 13. Bluetooth Slave Mode
  • 14. Bluetooth Master Mode
5. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
4. MicroPython Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
  • 3. Thonny IDE
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. TIMER_IT
  • 5. Watchdog Timer
  • 6. ADC
  • 7. PWM Breathing LED
  • 8. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. Flash Read and Write Experiment
  • 5. Print Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT
  • 11. Bluetooth Fundamentals
  • 12. Bluetooth Slave Mode
  • 13. Bluetooth Master Mode
5. Audio Applications
  • 1. I2S Audio Playback Experiment
  • 2. Repeater Experiment
Download
  • Code
  • Hardware_Info
  • 3D_Model_File
  • Software_Tools
  • VMware
  • VM_File
Download ZIP

Welcome to ESP32 Core Board repository

6.Event Groups

Event Groups


Event Groups1. Event Groups Overview2. Core concepts2.1 How Event Groups Work2.2 Event Group API2.3 Wait Mode Comparison2.4 Bit Operations3. Project Architecture and Flow4. Hardware Dependencies5. Source Code Analysis5.1 Header Files and Macro Configuration5.2 vSetBit0Task() — Set BIT_05.3 vSetBit1Task() — Set BIT_15.4 vWaitTask() — Wait Synchronization5.5 app_main() — Main Entry6. Operation Flow6.1 Compilation and Flashing Flow6.2 Normal Operation7. FAQ


1. Event Groups Overview

Event Group is a mechanism in FreeRTOS for multi-event synchronization. It uses a 24-bit bitmap to represent multiple events, where tasks can block and wait for any one or all specified event bits to be set.

Compared to semaphores (which only represent one event), a key advantage of event groups is: can wait for multiple independent events to all be ready at once.

Importance and Application Scenarios: Event groups are indispensable in scenarios requiring "waiting for multiple independent conditions to be satisfied simultaneously". Typical applications include:

  • Sensor fusion: Wait for all multiple sensor data to be collected before fusion calculation;

  • Initialization synchronization: Wait for WiFi, Bluetooth, file system and other modules to all complete initialization before entering main business;

  • Fault detection: Simultaneously monitor communication timeout, voltage anomaly, watchdog and other fault flags, enter protection state when any triggers;

  • Multi-peripheral coordination: When multiple peripherals complete tasks at different rates, uniformly trigger next phase operation.

After mastering event groups, you will have the ability to handle complex multi-task synchronization scenarios, which is an important step from single-signal synchronization to system-level task scheduling.

This project demonstrates: two setter tasks run freely at different periods — BIT_0 is set every 500ms, BIT_1 is set every 700ms. Wait task blocks until both bits are ready simultaneously before waking up. After 5 synchronizations, wait task sets BIT_EXIT bit via event group to notify exit, all inter-task communication completed via event groups (no global variables involved).


2. Core concepts

2.1 How Event Groups Work

  vSetBit0Task ──(500ms)──► xEventGroupSetBits(BIT_0)
  vSetBit1Task ──(700ms)──► xEventGroupSetBits(BIT_1)
                                │
                    ┌───────────┘
                    ▼
            ┌───────────────┐
            │  Event Group  │
            │  BIT_0 BIT_1  │
            │   1     1     │  ← Both bits set
            └───────┬───────┘
                    │
                    ▼
  vWaitTask: xEventGroupWaitBits(BIT_0 | BIT_1, AND, CLEAR)
             ↑ Blocks until both bits are 1, auto-clear after consumption

2.2 Event Group API

​x
// Create event group
EventGroupHandle_t xEventGroupCreate(void);

// Set event bits
EventBits_t xEventGroupSetBits(
    EventGroupHandle_t xEventGroup,
    // Bits to set (can be multiple, OR together)
    EventBits_t uxBitsToSet
);

// Get current event bits (non-blocking)
EventBits_t xEventGroupGetBits(EventGroupHandle_t xEventGroup);

// Wait for event bits
EventBits_t xEventGroupWaitBits(
    EventGroupHandle_t xEventGroup,
    EventBits_t uxBitsToWaitFor,      // Which bits to wait for
    BaseType_t xClearOnExit,
    BaseType_t xWaitForAllBits,       // pdTRUE: AND / pdFALSE: OR
    TickType_t xTicksToWait           // Timeout
);

2.3 Wait Mode Comparison

xWaitForAllBitsSemanticsExample
pdTRUE (AND)Returns only when all specified bits are 1Wait for BIT_0 and BIT_1 simultaneously ready
pdFALSE (OR)Returns when any specified bit is 1Wait for BIT_0 or BIT_1 ready

2.4 Bit Operations

xxxxxxxxxx
#define BIT_0   (1 << 0)   // 0b0001, event bit 0
#define BIT_1   (1 << 1)   // 0b0010, event bit 1
#define BIT_2   (1 << 2)   // 0b0100, event bit 2
#define BIT_3   (1 << 3)   // 0b1000, event bit 3
// ... Up to 24 bits (bit 0 ~ bit 23)

// Wait for multiple bits
xEventGroupWaitBits(group, BIT_0 | BIT_1 | BIT_2, ...);

This project uses 3 event bits:

BitMacroSetterFunction
BIT_0(1 << 0)vSetBit0Task (every 500ms)Data bit
BIT_1(1 << 1)vSetBit1Task (every 700ms)Data bit
BIT_EXIT(1 << 2)vWaitTask (after sync complete)Exit bit

3. Project Architecture and Flow

xxxxxxxxxx
app_main()
  └─ xEventGroupCreate()  ← Create event group

  ┌────────────────┬────────────────┐
  ▼                ▼                ▼
vSetBit0Task    vSetBit1Task    vWaitTask
(Core 1, Prio2) (Core 1, Prio2) (Core 0, Prio2)
   │                │                │
   │ Every 500ms   │ Every 700ms   │ Block wait BIT_0 + BIT_1
   ▼                ▼                ▼
SetBits(BIT_0)   SetBits(BIT_1)   WaitBits(BIT_0|BIT_1, AND, CLEAR)
   │                │                │
   │ Loop check    │ Loop check    │ Both BIT ready → count+1
   │ BIT_EXIT       │ BIT_EXIT      │ 5 times → SetBits(BIT_EXIT)
   │ if→exit        │ if→exit       │          → self-delete

Event Bit Parameters:

BitMacroSetterDescription
BIT_0(1 << 0)vSetBit0Task (every 500ms)Data bit
BIT_1(1 << 1)vSetBit1Task (every 700ms)Data bit
BIT_EXIT(1 << 2)vWaitTaskExit signal

Sync Condition: BIT_0 and BIT_1 both ready → waiter count +1 and clear → exit after 5 times.


4. Hardware Dependencies

This project is a pure software demonstration, no hardware peripheral dependencies (only serial log output).

Required Components: freertos/FreeRTOS, freertos/task, freertos/event_groups, esp_log


5. Source Code Analysis

Full source code: Source Code → ESP-IDF → 4.FreeRTOS → 2. RTOS_Inter-task_communication → RTOS_EVENT_GROUP

5.1 Header Files and Macro Configuration

This code section introduces headers required for event groups, defines three event bit macros and global control variables. Exit notification is done via event group bit BIT_EXIT, without using ordinary global variables for flag passing.

xxxxxxxxxx
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_log.h"

static const char *TAG = "EVENT_GROUP_DEMO";

// Event group handle
static EventGroupHandle_t xEventGroup = NULL;

// Define event bits (each occupies 1 bit)
#define BIT_0       (1 << 0)
#define BIT_1       (1 << 1)
#define BIT_EXIT    (1 << 2)

#define MAX_LOOP 5
static int g_loop_count = 0;

5.2 vSetBit0Task() — Set BIT_0

This code runs freely at 500ms period, checks exit bit BIT_EXIT in event group via xEventGroupGetBits() at the beginning of each loop, self-deletes upon receiving exit signal.

xxxxxxxxxx
static void vSetBit0Task(void *pvParameters)
{
    while (1)
    {
        if (xEventGroupGetBits(xEventGroup) & BIT_EXIT) {
            ESP_LOGI(TAG, "Task1 exit");
            vTaskDelete(NULL);
        }

        xEventGroupSetBits(xEventGroup, BIT_0);
        ESP_LOGI(TAG, "Set BIT0");
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}

5.3 vSetBit1Task() — Set BIT_1

This code runs freely at 700ms period, structurally symmetric, checks exit signal via event group.

xxxxxxxxxx
static void vSetBit1Task(void *pvParameters)
{
    while (1)
    {
        if (xEventGroupGetBits(xEventGroup) & BIT_EXIT) {
            ESP_LOGI(TAG, "Task2 exit");
            vTaskDelete(NULL);
        }

        xEventGroupSetBits(xEventGroup, BIT_1);
        ESP_LOGI(TAG, "Set BIT1");
        vTaskDelay(pdMS_TO_TICKS(700));
    }
}

5.4 vWaitTask() — Wait Synchronization

This code blocks waiting for BIT_0 and BIT_1 to be ready simultaneously (AND mode, auto-clear), after count reaches MAX_LOOP, sets BIT_EXIT bit via event group to notify all tasks to exit.

xxxxxxxxxx
static void vWaitTask(void *pvParameters)
{
    while (1)
    {
        xEventGroupWaitBits(xEventGroup,
                            BIT_0 | BIT_1,
                            pdTRUE,
                            pdTRUE,
                            portMAX_DELAY);

        g_loop_count++;
        ESP_LOGI(TAG, "===== BOTH BITS READY! Count: %d =====", g_loop_count);

        if (g_loop_count >= MAX_LOOP)
        {
            xEventGroupSetBits(xEventGroup, BIT_EXIT);
            ESP_LOGI(TAG, "Wait task finish");
            vTaskDelete(NULL);
        }
    }
}

Why use event group bits instead of global variables for exit notification?

The core concept of event groups is all inter-task communication is completed via event bits. If using an ordinary global variable like bool g_finish for exit notification, it breaks the synchronization semantics of event groups — tasks both wait for event group bits and poll ordinary variables, mixing two communication mechanisms. After using BIT_EXIT event bit, the three tasks coordinate entirely through the event group.

5.5 app_main() — Main Entry

This code creates event group and starts three tasks, then blocks waiting for BIT_EXIT exit signal, ensuring app_main() only returns after all tasks end, avoiding system log Returned from app_main() being inserted into task output stream.

xxxxxxxxxx
void app_main(void)
{
    xEventGroup = xEventGroupCreate();

    if (xEventGroup != NULL)
    {
        ESP_LOGI(TAG, "Event Group created");

        xTaskCreatePinnedToCore(vSetBit0Task, "SetBit0", 2048, NULL, 2, NULL, 1);
        xTaskCreatePinnedToCore(vSetBit1Task, "SetBit1", 2048, NULL, 2, NULL, 1);
        xTaskCreatePinnedToCore(vWaitTask,   "Wait",    2048, NULL, 2, NULL, 0);

        xEventGroupWaitBits(xEventGroup, BIT_EXIT, pdFALSE, pdTRUE, portMAX_DELAY);
        vTaskDelay(pdMS_TO_TICKS(800));
        vEventGroupDelete(xEventGroup);
    }
}

6. Operation Flow

6.1 Compilation and Flashing Flow

For detailed flashing process: 1. Before Development → 3. Build and Flash

6.2 Normal Operation

  1. After flashing, power on, observe serial logs

  2. BIT_0 is set every 500ms, BIT_1 is set every 700ms (each runs independently)

  3. When both BITs are 1 simultaneously, WaitTask wakes up, consumes data bits and count +1

两个独立任务,个运行一次,事件组计数加一

  1. After 5 synchronizations complete → vWaitTask sets BIT_EXIT → both setters detect exit signal and self-delete

计数满5次,任务相继推出

Log example:

xxxxxxxxxx
I (275) EVENT_GROUP_DEMO: Event Group created
I (275) EVENT_GROUP_DEMO: Set BIT0
I (275) EVENT_GROUP_DEMO: Set BIT1
I (275) EVENT_GROUP_DEMO: ===== BOTH BITS READY! Count: 1 =====
I (775) EVENT_GROUP_DEMO: Set BIT0
I (975) EVENT_GROUP_DEMO: Set BIT1
I (975) EVENT_GROUP_DEMO: ===== BOTH BITS READY! Count: 2 =====
I (1275) EVENT_GROUP_DEMO: Set BIT0
I (1675) EVENT_GROUP_DEMO: Set BIT1
I (1675) EVENT_GROUP_DEMO: ===== BOTH BITS READY! Count: 3 =====
I (1775) EVENT_GROUP_DEMO: Set BIT0
I (2275) EVENT_GROUP_DEMO: Set BIT0
I (2375) EVENT_GROUP_DEMO: Set BIT1
I (2375) EVENT_GROUP_DEMO: ===== BOTH BITS READY! Count: 4 =====
I (2775) EVENT_GROUP_DEMO: Set BIT0
I (3075) EVENT_GROUP_DEMO: Set BIT1
I (3075) EVENT_GROUP_DEMO: ===== BOTH BITS READY! Count: 5 =====
I (3075) EVENT_GROUP_DEMO: Wait task finish
I (3275) EVENT_GROUP_DEMO: Task1 exit
I (3775) EVENT_GROUP_DEMO: Task2 exit
I (3875) main_task: Returned from app_main()

7. FAQ

SymptomCauseSolution
WaitTask never wakesEvent bits never set / bit mask errorConfirm BIT definitions match SetBits calls
Sync count exceeds expectationAuto-clear not enabled (pdFALSE)Change to pdTRUE, auto-clear after satisfaction
Only waited for one BITxWaitForAllBits set to pdFALSEChange to pdTRUE (wait for all bits)
Event group creation failed (NULL)Insufficient memoryCheck heap size
Mutex
Software Timer

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文