this generates a 1kHz sine wave using a ESP32 DAC
// ESP32 DAQ
// ESP32 timer interrupts 100000times/second to generate 1kHz sine wave using 100 sample lookup table
// 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>
// Timer0 Configuration Pointer (Handle)
hw_timer_t *Timer0_Cfg = NULL;
// Sine LookUpTable & Index Variable
uint8_t SampleIdx = 0;
const uint8_t sineLookupTable[] = {
128, 136, 143, 151, 159, 167, 174, 182,
189, 196, 202, 209, 215, 220, 226, 231,
235, 239, 243, 246, 249, 251, 253, 254,
255, 255, 255, 254, 253, 251, 249, 246,
243, 239, 235, 231, 226, 220, 215, 209,
202, 196, 189, 182, 174, 167, 159, 151,
143, 136, 128, 119, 112, 104, 96, 88,
81, 73, 66, 59, 53, 46, 40, 35,
29, 24, 20, 16, 12, 9, 6, 4,
2, 1, 0, 0, 0, 1, 2, 4,
6, 9, 12, 16, 20, 24, 29, 35,
40, 46, 53, 59, 66, 73, 81, 88,
96, 104, 112, 119
};
volatile int counter = 0;
// The Timer0 ISR Function (Executes Every Timer0 Interrupt Interval)
void IRAM_ATTR Timer0_ISR() {
// Send SineTable Values To DAC One By One
dac_output_voltage(DAC_CHANNEL_1, sineLookupTable[SampleIdx++]);
if (SampleIdx == 100) {
SampleIdx = 0;
}
counter++;
}
void setup() {
Serial.begin(115200);
Serial.println("\nESP32 DAC sine wave");
// Configure Timer0 Interrupt
Timer0_Cfg = timerBegin(10000000);
timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR);
timerAlarm(Timer0_Cfg, 100, true, 0);
// Enable DAC1 Channel's Output
dac_output_enable(DAC_CHANNEL_1);
}
void loop() {
static long timer = millis();
if (millis() - timer > 1000) {
Serial.println(counter);
counter = 0;
timer = millis();
}
}
what frequency range are you interested in?
give more details of the project?
if you require accurate sine waves have a look at DDS, e.g. ad9833-programmable-waveform-generator