Esp32 dac resolution

Hi
The ESP32's internal Digital-to-Analog Converters (DACs) have an 8-bit resolution.
Using this program with input 15 Hz the signal output of DAC is very bad, it is like a red line instead as blue line.
What is wrong?

/*
   Copyright (c) 2018, circuits4you.com
   All rights reserved.

   ESP32 DAC - Digital To Analog Conversion Example
*/

#define DAC1 25

void setup() {
  Serial.begin(115200);
}

void loop() { // Generate a Sine wave
  //int Value = 255; //255= 3.3V 128=1.65V
  int Value = analogRead(36);
  // dacWrite(DAC1, Value);
  dacWrite(DAC1, analogRead(36));
  Serial.print("    pin  =  ");
  Serial.print(analogRead(36));
  Serial.println( );
 
}

Why do you store your analogread in a variable,and never use the stored value?
What is the scale of the graph?
Is the red line from dac1 or from Serial?
Analogread of esp is not at its best near the end of the scale (near 0 and near 1023) (non-linear).

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

Never worked with the DAC of an ESP32, but this seeems wrong to me.
You are trying to write a 12-bit value to an 8-bit DAC.
Maybe convert it to 8-bit first, like this.
dacWrite(DAC1, analogRead(36) >> 4); // bitshift to 8-bit
Leo..

I want the DAC output to be the same as the input on pin 36

It is a huge improvement from 0 to 5 Hz.

Its look not bad up to 5 Hz, blue line on post #1, after that distortion is increasing, at 1 kHz is hard to see sine wave.

what is the maximum frequency on pin 36?

Audio signal up to 20 kHz

That is because you are not sampling fast enough. The print statements are drastically slowing down the sampling
Remove the print statements and you should at least be able to to 1KHz

Doesn't help much. That's 5 Hz on a serial plotter, poor sampling.

If you removed the print statements how are you printing to the plotter?

216 Hz

see post #11

Then I look at the oscilloscope.

So what the the scope look like?

Using the Arduino analogRead() I don't thing you can sample any faster than 20kHz. So to have a somewhat good looking sinewave out of the DAC you need about 10 samples per cycle, so a 2kHz sinewave is probably the limit.

I know it can go faster but I can't help with the code to do that

inputting 2kHz into an ESP32 ADC direct output to a DAC
(yellow trace 2kHz input cyan trace DAC output)

code

// ESP32 ADC input > DAC output

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

void setup() {
  Serial.begin(115200);
  Serial.println("\nESP32 ADC > DAC");
  // Enable DAC1 Channel's Output
  dac_output_enable(DAC_CHANNEL_1);
  analogSetWidth(12);  // ADC 12bit resolution
}

void loop() {
  unsigned int adc =  analogRead(35) >> 4;  // adc/16 - 12bits to 8bits
  dac_output_voltage(DAC_CHANNEL_1, adc);
}

1kHz

maybe worth asking the question on the ESP32 Arduino forum?

otherwise look at a faster microcontroller, e.g. Texas TMS320C6748 DSP development kit

For now it is enough. Now i will try to develop phase detector, for that I added square wave generator and want to output pin 25 be active when pulses on pin 0 are high.

// ESP32 ADC input > DAC output

#include <driver/ledc.h>
#include <driver/dac.h>
#include <driver/adc.h>

void setup() {
  Serial.begin(115200);
  Serial.println("\nESP32 ADC > DAC");
  // Enable DAC1 Channel's Output
  dac_output_enable(DAC_CHANNEL_1);
  analogSetWidth(12);  // ADC 12bit resolution

    ledc_timer_config_t timerConfig = {
    .speed_mode = LEDC_LOW_SPEED_MODE,
    .duty_resolution = LEDC_TIMER_10_BIT,
    .timer_num = LEDC_TIMER_0,
    .freq_hz = 200,
    .clk_cfg = LEDC_AUTO_CLK
  };
  ledc_timer_config(&timerConfig);


  ledc_channel_config_t channelConfig1 = {
    //.gpio_num = 32,
    //.gpio_num = 16,
    .gpio_num = 4,
    //.gpio_num = 12,
    .speed_mode = LEDC_LOW_SPEED_MODE,
    .channel = LEDC_CHANNEL_0,
    .timer_sel = LEDC_TIMER_0,
    .duty = 512,
    //.duty = 50,
    .hpoint = 0
  };
  ledc_channel_config(&channelConfig1);

  ledc_channel_config_t channelConfig2 = {
    .gpio_num = 0,
    .speed_mode = LEDC_LOW_SPEED_MODE,
    .channel = LEDC_CHANNEL_1,
    .timer_sel = LEDC_TIMER_0,
    .duty = 512,
    .hpoint = 256
  };
  ledc_channel_config(&channelConfig2);
  
}

void loop() {
  unsigned int adc =  analogRead(36) >> 4;  // adc/16 - 12bits to 8bits
  dac_output_voltage(DAC_CHANNEL_1, adc);
}



Detector is working, now i need to convert output of it to voltage.