experiment measuring the rise time of a 10Hz sine wave
ESP32 sampling every milliSecond
// ESP32 determine sine wave rise time
// the data acquired must be the rising edge of signal - see example sine wave plot
#include <driver/dac.h>
#include <driver/adc.h>
#define RXPIN 16
// Timer0 Configuration Pointer (Handle)
hw_timer_t *Timer0_Cfg = NULL;
// array to save 100 ADC values
#define SAMPLES 100
volatile int counter = 0, adcsIndex = 0;
;
volatile unsigned int adcs[SAMPLES] = { 0 };
// The Timer0 ISR Function (Executes Every Timer0 Interrupt Interval)
void IRAM_ATTR Timer0_ISR() {
// read ADC output to DAC
unsigned int adc = analogRead(35); // read ADC pin 35
// save SAMPLE ADC values to array
if (adcsIndex < SAMPLES) adcs[adcsIndex++] = adc;
dac_output_voltage(DAC_CHANNEL_1, adc >> 4); // output to DAC
counter++;
}
// interrupt handler - change in level on RXPIN
void IRAM_ATTR handleInterrupt2() {
if (!digitalRead(RXPIN)) return; // look for rising edge
detachInterrupt(digitalPinToInterrupt(RXPIN)); // detach the interrupt
timerAlarmEnable(Timer0_Cfg); // start timer
}
void setup() {
Serial.begin(115200);
delay(2000);
pinMode(RXPIN, INPUT_PULLDOWN);
attachInterrupt(digitalPinToInterrupt(RXPIN), handleInterrupt2, CHANGE);
Serial.println("\nESP32 determine sine wave rise time");
// Configure Timer0 Interrupt 1000/second
Timer0_Cfg = timerBegin(0, 80, true);
timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR, true);
timerAlarmWrite(Timer0_Cfg, 1000, true);
//timerAlarmEnable(Timer0_Cfg);
// Enable DAC1 Channel's Output
dac_output_enable(DAC_CHANNEL_1); // display on DAC?
analogSetWidth(12); // ADC 12bit resolution
analogSetClockDiv(2); // converion time
while (adcsIndex < SAMPLES) ;
}
void loop() {
while (adcsIndex < SAMPLES) ; // wait for data samples
static char ch = '1';
if (ch == '1') Serial.println("enter 1 for data plus calculations, 2 for data only (for a plot)");
if (ch == '1' || ch == '2') {
// plot the ADC values in the array
for (int i = 0; i < SAMPLES; i++) {
if (ch == '1') {
Serial.print(i); // display array index
Serial.print(' ');
}
Serial.println(adcs[i]); // and data samples
}
}
if (ch == '1') { // display calculations as well a data samples
int min = 10000, minSample = 0, max = 0, maxSample = 0;
for (int i = 0; i < SAMPLES; i++) // find minimum sample value and array index
if (adcs[i] < min) {
min = adcs[i];
minSample = i;
if (adcs[i + 1] > min) break;
}
for (int i = minSample; i < SAMPLES; i++) // find maximum sample value and array index
if (adcs[i] > max) {
max = adcs[i];
maxSample = i;
}
// display values and voltages
Serial.printf("minSample %d min %d %fV\n", minSample, min, min * 3.3 / 4096);
Serial.printf("maxSample %d max %d %fV\n", maxSample, max, max * 3.3 / 4096);
// calculate amplidude of signal
int amplitude = max - min, rise_start, rise_end;
Serial.printf("ampliude %d\n", amplitude);
// calculate rising edge start and end signal amplitude (assume 10% and 90%)
Serial.printf("rise start %d rise end %d\n", rise_start = min + (amplitude / 10), rise_end = max - (amplitude / 10));
// find the nearest sample array index to rise start and end - could use interpolation
int rise_start_i = minSample, rise_end_i = minSample;
while (adcs[rise_start_i++] < rise_start) ;
while (adcs[rise_end_i++] < rise_end) ;
// each start is 1mSec therefore calculate rise time
Serial.printf("rise time %dmSec\n", rise_end_i - rise_start_i);
}
while (!Serial.available()) ;
ch = Serial.read();
}
Serial plotter output
serial monitor output gives a rise time of 30mSec
enter 1 for data plus calculations, 2 for data only (for a plot)
0 2429
1 2365
2 2298
3 2224
4 2145
5 2067
6 1989
7 1904
8 1819
9 1748
10 1671
11 1583
12 1491
13 1415
14 1334
15 1249
16 1178
17 1104
18 1023
19 959
20 890
21 821
22 763
23 707
24 656
25 603
26 561
27 525
28 482
29 453
30 435
31 411
32 391
33 380
34 372
35 373
36 376
37 387
38 401
39 425
40 446
41 471
42 506
43 557
44 576
45 642
46 693
47 745
48 811
49 873
50 941
51 1007
52 1085
53 1158
54 1232
55 1310
56 1392
57 1470
58 1559
59 1643
60 1723
61 1813
62 1883
63 1967
64 2047
65 2130
66 2198
67 2274
68 2351
69 2421
70 2485
71 2545
72 2611
73 2666
74 2722
75 2768
76 2819
77 2855
78 2905
79 2925
80 2950
81 2975
82 2995
83 3002
84 3007
85 3007
86 3007
87 2994
88 2981
89 2966
90 2931
91 2902
92 2864
93 2823
94 2784
95 2735
96 2679
97 2628
98 2559
99 2499
minSample 34 min 372 0.299707V
maxSample 84 max 3007 2.422632V
ampliude 2635
rise start 635 rise end 2744
rise time 30mSec
oscilloscope rise time

the data acquisition must start to capture the rising edge of signal
the above uses the rising edge of a square wave (same frequency as sine wave but out of phase) to start the timer interrupts, e.g.

may give you some ideas!
