How to generate a 1kHz interrupt for esp32?

Good afternoon to everybody,
I have a question and hope that someone will kindly find out the solution...I will be very thankful of that!!!

I have an esp32 and want to program an interrupt for the acquisition of a signal from a sensor.
Until now, everything ok...but I am not able to generate it at 1kHz..I am pretty new in this field, do you think is possible?

Thank you in advance!!

To generate a 1kHz interrupt on the ESP32, you can use one of its hardware timers. First, initialize the desired timer (e.g., TIMER_0) with the preferred settings, such as the 1kHz frequency and interrupt service routine (ISR). Set the timer's alarm value to trigger the interrupt at the desired interval (e.g., 1ms). Within the ISR, perform the necessary tasks. This approach ensures precise timing without burdening the CPU. Remember to configure the timer's clock source appropriately, and enable the interrupt. Finally, integrate the timer into your code to take advantage of the 1kHz interrupt for your application.

Hello student_897

Take a view into the BlinkWithoutDelay example to gain the knowledge.

Have a nice day and enjoy coding in C++.

Thank you very much!
Do you think that this could be correct?

  timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, 1000, true);
  timerAlarmEnable(timer);

have a look at this example

// ESP32 DAQ
// ESP32 timer interrupts 10000times/second to read ADC and output to DAC

// from ESP32 Sine Wave Example https://deepbluembedded.com/esp32-dac-audio-arduino-examples/

/*
* LAB Name: ESP32 Sine Wave Generation Example
* Author: Khaled Magdy
* DeepBlueMbedded 2023
* For More Info Visit: www.DeepBlueMbedded.com
*/

#include <driver/dac.h>
#include <driver/adc.h>
#include "esp_wifi.h"

// Timer0 Configuration Pointer (Handle)
hw_timer_t *Timer0_Cfg = NULL;

volatile int counter = 0;
volatile   unsigned short int adc=analogRead(35)>>4;   // adc/16 - 12bits to 8bits

// The Timer0 ISR Function (Executes Every Timer0 Interrupt Interval)
void IRAM_ATTR Timer0_ISR() {
  // read ADC output to DAC
  //unsigned int adc=analogRead(35)>>4;   // adc/16 - 12bits to 8bits
  dac_output_voltage(DAC_CHANNEL_1, adc);
  counter++;
}

void setup() {
  Serial.begin(115200);
  Serial.println("\nESP32 DAC sine wave");
  // Configure Timer0 Interrupt 10000/second
  Timer0_Cfg = timerBegin(0, 400, true);
  timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR, true);
  timerAlarmWrite(Timer0_Cfg, 10, true);
  timerAlarmEnable(Timer0_Cfg);
  // Enable DAC1 Channel's Output
  dac_output_enable(DAC_CHANNEL_1);
  analogSetWidth(12);    // ADC 12bit resolution
  analogSetClockDiv(255);  // fastest converion time
    esp_wifi_stop();
}

void loop() {
  adc=adc1_get_raw(ADC1_CHANNEL_7)>>4; // GPIO32
 // adc=analogRead(35)>>4;
  static long timer = millis();
  if (millis() - timer > 1000) {
    Serial.println(counter);
    counter = 0;
    timer = millis();
  }
}

sample output
image

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.