NOT able to generate clock pulse

#include <Arduino.h>
#include <esp32-hal-timer.h>

// Pin definitions
const int buttonPin = 34;    // Pin connected to the button
const int clockPin = 18;    // Pin connected to the clock output

// Timer variables
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
volatile bool clockRunning = false;

// Interrupt service routine for the timer
void IRAM_ATTR onTimer() {
  digitalWrite(clockPin, !digitalRead(clockPin)); // Toggle clock pin state
}

// Interrupt service routine for the button
void IRAM_ATTR buttonISR() {
  // Debouncing delay
  delay(50);
  
  // Check button state again to ensure it's still pressed
  if (digitalRead(buttonPin) == LOW) {
    // Toggle clock running state
    clockRunning = !clockRunning;
    
    if (clockRunning) {
      // Start the timer
      timerAlarmEnable(timer);
      Serial.println("Clock started.");
    } else {
      // Stop the timer
      timerAlarmDisable(timer);
      digitalWrite(clockPin, LOW);   // Set clock pin low
      Serial.println("Clock stopped.");
    }
  }
}

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Configure button pin
  pinMode(buttonPin, INPUT_PULLUP);

  // Configure clock pin
  pinMode(clockPin, OUTPUT);
  digitalWrite(clockPin, LOW);  // Start with clock pin low

  // Create a timer
  timer = timerBegin(0, 80, true); // Timer 0, divider 80 (1MHz), count up
  timerAttachInterrupt(timer, &onTimer, true); // Attach timer interrupt
  timerAlarmWrite(timer, 100, true); // 100 microsecond period (10 MHz frequency)
  
  // Attach interrupt to the button pin
  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonISR, FALLING);
}

void loop() {
  // Nothing to do here because the action is handled by interrupts
}

there was no error in compiling

But i am not able to generate the clock pulse.

On what example did you base your code ?
A delay() and Serial.print() in an ISR is unusual.

That's 10KHz NOT 10MHz

You never enabled the Alarm
// Start an alarm
timerAlarmEnable(timer);

Are you still using version 3.0.2 or have you reverted back to version 2.n.n

1 Like

ok, i will note it.

I reverted back to version 2.n.n.
Thank you for informing about the version.

Did it work?

can you please check this code and tell if any mistakes are there in this code, i have modified it.

@jim-p

#include <Arduino.h>
#include <esp32-hal-timer.h>

// Define the LED pin
const int ledPin = 18;  // GPIO 2
const int switchPin = 34;   // GPIO pin where the switch is connected
volatile bool button = false;

// Define the timer
hw_timer_t *timer = NULL;

void IRAM_ATTR switchInterrupt() 
{
  button = !button;
}

void IRAM_ATTR onTimer() 
{    
  // Create a timer with a 1 second period
  timer = timerBegin(0, 8, true);  // Timer 0, divider 8, count up  # default is 10 MHz the mddle value is divider
  timerAlarmWrite(timer, 500000, true);  // 1 second period, autoreload
  digitalWrite(ledPin, HIGH);  // Turn on the LED  
}

void setup() 
{
  pinMode(ledPin, OUTPUT);                  // LED pin as output
  pinMode(switchPin, INPUT_PULLUP);         // Switch pin as input with pull-up resistor
  attachInterrupt(digitalPinToInterrupt(switchPin), switchInterrupt, FALLING);  // Attach interrupt on falling edge (switch press)
  timerAttachInterrupt(timer, &onTimer,true);
}
  

void loop() 
{
  if (button)
  {
    timerAlarmEnable(timer);
  }
  else
  {
    timerAlarmDisable(timer);
  }

}