[EN] Arduino: ESP32-S2’s DAC&ADC.

After testing the DAC and ADC of both the ESP32, SAM-D21, LGT8F328P and STM32L432KC, This time, it’s the time of the ESP32-S2 that we have. The test performance is still the same as before using the DAC. Three types of waves are sent out: zigzag, triangular, and sine waves and connect to the ADC pin to read the value and test the graph to see what it looks like.

In this experiment, DAC1 is connected to the ADC as shown in Figure 1. The ESP32-S2 has 2 ports of 8-bit DAC called DAC1 and DAC2. The ADC has a 12-bit resolution, which is seen to have the same properties as the ESP32, but whether the results are the same or not, you can tell that they are probably different because they use different microcontrollers. The ESP32-S2 uses the same one as the ESP32-S3 with only one core and no BLE.

Figure 1 Neucleo L432KC with A3 connected to D3

DAC

  • DAC1
  • DAC2

The settings for the ESP32-S2 board are as shown in Figure 2.

Figure 2 Arduino IDE setting

Example Code

Examples of 3 types of graph drawing programs are zigzag, triangular and sine wave graphs. There is working code as follows:

Zigzag graph

The zigzag graph is transmitted 0 to the maximum possible value of the DAC sector, after which it loops through again. The code for ESP32-S2 is as follows, and the result is as shown in Figure 3.

#include <Arduino.h>

#define pinSpk DAC1
#define pinMic 1
#define MAX_DAC_VALUES 200
#define MAX_ADC_VALUES 4096

int adcValue;

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

void loop() {
  for (int i = 0; i < MAX_DAC_VALUES; i++) {
    dacWrite( pinSpk, i );
    adcValue = analogRead(pinMic);
    Serial.println(adcValue);
  }
}
Figure 3 Zigzag graph

Triangular graph

The triangular graph works similarly to the zigzag principle by adding a loop to return the maximum value to 0 as shown in the ESP32-S2 code as follows, and the resulting example is shown in Figure 4.

#include <Arduino.h>

#define pinSpk DAC1
#define pinMic 1
#define MAX_DAC_VALUES 200 // 256
#define MAX_ADC_VALUES 4096

int adcValue;

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

void loop() {
for (int i = 0; i < MAX_DAC_VALUES; i++) {
    dacWrite( pinSpk, i );
    adcValue = analogRead(pinMic);
    Serial.println(adcValue);
  }
  for (int i = MAX_DAC_VALUES-1; i >= 0; i--) {
    dacWrite( pinSpk, i );
    adcValue = analogRead(pinMic);
    Serial.println(adcValue);
  }
}
Figure 4 Triangular graph

Sine wave graph

Constructing a sinusoidal wave graph uses a loop designation to increase the degree angle from 0 to 359, with each cycle doing the following:

  1. Convert degrees to radians
  2. Calculate the sine from the radian angle.
  3. Converts decimal values to integers in the value range supported by the DAC.
  4. Read the value from ADC
  5. Send data through serial port to program Serial Plotter.

The code for the ESP32-S2 is as follows, and the example output is shown in Figure 5.

#include <Arduino.h>

#define pinSpk DAC1
#define pinMic 1
#define MAX_DAC_VALUES 200 // 256
#define MAX_ADC_VALUES 4096

int adcValue;

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

void loop() {
  int degree = 0;
  float radian = 0.0;
  float sineValue = 0.0;
  int dValue = 0;

  for (degree = 0; degree < 360; degree++) {
    radian = (float)degree * (2.0 * 3.1415926) / 360.0;
    sineValue = sin(radian);
    dValue = (int)((1.0 + sineValue) * (MAX_DAC_VALUES/2));
    dacWrite( pinSpk, dValue );
    adcValue = analogRead(pinMic);
    Serial.println(adcValue);
  }
}
Figure 5 Sine wave graph

Conclusion

From this article, it can be seen that the graph obtained from the ESP32-S2’s ADC is quite different from the ESP32 and looks linear. As for the selection, you have to look at other elements, such as how much accuracy you want, what level of state conversion speed is required? Need to connect to a network or not, etc. Finally, have fun with programming.

(C) 2020-2022, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2022-01-29