/* * Lab 1 - Task 3: ESP32 Hardware Timer Interrupt
* Objective: Blink a Yellow LED every 1 second using precise hardware timers.
* Note: This code uses the updated ESP32 Arduino Core v3.x.x API.
*/
const int ledPin = 19; // Yellow LED connected to GPIO 19
volatile bool ledState = false; // Volatile variable for ISR
// Create a hardware timer pointer
hw_timer_t * timer = NULL;
// Interrupt Service Routine (ISR) function
// IRAM_ATTR ensures this function is loaded into RAM for fast execution
void IRAM_ATTR onTimer() {
ledState = !ledState; // Toggle the LED state
digitalWrite(ledPin, ledState); // Update the LED
}
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
Serial.println("Task 3 Ready: Starting Hardware Timer...");
// 1. Initialize Timer
// Set timer frequency to 1,000,000 Hz (1 MHz).
// This means 1 timer tick = 1 microsecond.
timer = timerBegin(1000000);
// 2. Attach the ISR function to the timer
timerAttachInterrupt(timer, &onTimer);
// 3. Set the alarm
// Parameters: timer pointer, alarm value (1,000,000 us = 1 second), auto-reload (true), reload count (0 = infinite)
timerAlarm(timer, 1000000, true, 0);
}
void loop() {
// The main loop is completely empty!
// The ESP32 is toggling the LED entirely in the background using hardware interrupts.
}