yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
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

4. ESP_TIMER

ESP_TIMER


ESP_TIMER1. ESP_TIMER Overview2. Core Concepts2.1 esp_timer vs Hardware Timer2.2 Key APIs2.3 volatile for Cross-Task Variables3. Hardware Dependencies4. Project Architecture and Flow5. Source Code Details5.1 Global Variables5.2 Timer Callback Function5.3 setup() — Create and Start Timer6. Operation Flow6.1 Build and Flash6.2 Expected Behavior6.3 Verification Methods7. Common Issues


1. ESP_TIMER Overview

ESP_TIMER is a high-precision software timer API provided by ESP-IDF, implemented based on a 64-bit hardware timer with microsecond resolution. Callbacks are executed in a separate Timer Task (not ISR context), allowing the use of Serial.print, which is much more convenient than hardware timer ISRs. This experiment creates a periodic timer that triggers every second, printing the trigger count in the callback.

Typical Application Scenarios:

Application TypeExample
Periodic SamplingTimed reading of temperature/humidity sensors, battery voltage inspection
Communication HeartbeatMQTT Keep-Alive, Bluetooth heartbeat, CAN bus node monitoring
Watchdog FeedingTask-level timed feeding to prevent hardware WDT false resets
LED IndicationTimed blinking, waterfall light rhythm control
Timeout DetectionCommunication response timeout, sensor data update timeout

2. Core Concepts

2.1 esp_timer vs Hardware Timer

Featureesp_timerHardware Timer (hw_timer_t)
Callback ContextRTOS Timer TaskISR
Can Use Serial.printYesNo
Timing PrecisionMicrosecond-level (with RTOS scheduling jitter)Microsecond-level (deterministic delay)
Auto-reloadesp_timer_start_periodic automatictimerAlarm auto_reload=true
Use CaseMost application timing needsJitter-sensitive precise timing

2.2 Key APIs

esp_timer_create(&args, &handle) — Creates a timer.

const esp_timer_create_args_t timer_args = {
    .callback = esp_timer_callback,      // Callback function pointer
    .name = "Esp_Timer",                  // Debug name
    // .dispatch_method defaults to ESP_TIMER_TASK → callback runs in timer task
};
esp_timer_handle_t timer_handle;
esp_timer_create(&timer_args, &timer_handle);

esp_timer_start_periodic(handle, period_us) — Starts a periodic timer. period_us is in microseconds, 1000000 = 1 second. The timer can be stopped at any time using esp_timer_stop() after creation.

2.3 volatile for Cross-Task Variables

The callback executes in the Timer Task, while loop() executes in the Arduino Task. Shared variables between different tasks require the volatile modifier, otherwise the compiler may cache the variable in each task's CPU registers, causing stale values to be read.


3. Hardware Dependencies

This project is a pure software experiment with no external hardware required. Only a USB cable is needed to connect to the serial port for viewing output.


4. Project Architecture and Flow

​x
setup()
  ├─ Serial.begin(115200)              // Initialize serial port
  ├─ esp_timer_create(&args, &handle)  // Create timer
  └─ esp_timer_start_periodic(handle, 1000000) // Start: 1s period

loop()                                 // Empty loop
  └─ (Timer callback runs independently in background Timer Task)

[Timer Task Background]
  └─ esp_timer_callback()              // Called automatically every second
       ├─ trigger_count++              // Increment counter
       └─ Serial.printf(...)           // Print trigger info

5. Source Code Details

Complete source code: Source Code → Arduino → 1.Basic Peripherals → 4.ESP_TIMER → 4.ESP_TIMER.ino

5.1 Global Variables

trigger_count is written in the callback and read in loop() (if needed). Cross-task sharing requires volatile.

xxxxxxxxxx
volatile uint32_t trigger_count = 0;   // Timer trigger count counter

If loop() doesn't need to read this variable, volatile can be omitted, but keeping it is good practice for future extensibility.

5.2 Timer Callback Function

The callback runs in the Timer Task, not ISR, so Serial.printf is safe to use.

xxxxxxxxxx
void esp_timer_callback(void* arg) {
    trigger_count++;
    Serial.printf("Trigger count: %u | Current time: %lu ms\n", trigger_count, millis());
}

5.3 setup() — Create and Start Timer

First configure the esp_timer_create_args_t structure, then create the timer, and finally start it.

xxxxxxxxxx
void setup() {
    Serial.begin(115200);
    Serial.println("ESP_TIMER Started (1000ms Period)");

    const esp_timer_create_args_t timer_args = {
        .callback = esp_timer_callback,
        .name = "Esp_Timer"
    };

    esp_timer_handle_t timer_handle;
    esp_timer_create(&timer_args, &timer_handle);
    esp_timer_start_periodic(timer_handle, 1000000);  // 1,000,000 us = 1s
}

void loop() {}  // Empty loop — all work done by timer callback

loop() is empty because there is no polling logic needed. The timer runs independently in the background with callbacks triggering automatically, which is the perfect example of event-driven programming.


6. Operation Flow

6.1 Build and Flash

Arduino flash process: See Arduino → 1. Before Development → 2. Build and Flash.

6.2 Expected Behavior

Open the Serial Monitor (115200 bps) and one line is printed every second:

xxxxxxxxxx
====================================
ESP_TIMER Started (1000ms Period)
====================================
Trigger count: 1 | Current time: 1026 ms
Trigger count: 2 | Current time: 2026 ms
Trigger count: 3 | Current time: 3026 ms
Trigger count: 4 | Current time: 4026 ms
...

Serial Output

6.3 Verification Methods

ActionExpected Result
Observe serial port after power onOne line per second, trigger_count increments
Change 1000000 to 500000Triggers every 500ms

7. Common Issues

SymptomCauseSolution
No serial outputloop() is empty but Serial not initializedVerify Serial.begin(115200)
Callback trigger interval inaccurateRTOS scheduling jitter (normal)Use hardware timer for extreme precision requirements
Compilation error esp_timer.h not foundMissing IDF componentNo additional action needed, Arduino ESP32 Core includes it by default
Peripheral Interrupts
GPTIMER

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/中文