# Peak detector

#### victoriomariani

Joined Nov 8, 2023
7
How I can measure this peaks with 1kHz. I am projecting a superdiode peak detector, but I don’t know with for diferent amplitude peak I am Measure the peak.

I would like to using an Esp32 to calculate the median of 100 peaks and identify a standard desvian

Last edited by a moderator:

#### crutschow

Joined Mar 14, 2008
34,671
Assuming the Esp32 A/D conversion rate is at least 100k samples/sec. you can just rapidly sample the 1kHz signal and look for the peak signal in each of the samples over the 1kHz period.

#### Michal Podmanický

Joined May 11, 2019
67
You need to create a signal when the peak occurs so ESP knows exact time when to trig an ADC.
This signal can be made from precisely calculated RC differentiator. The edge before peak seems to be very sharp so you can use just RC with very short time constant.
There is also possible to use a comparator like LM393 with reference set to about 1/2 of smallest peak. After comparator signal rise delay the ADC start so you sample in the middle of peak.

Btw, there is no need a rectifier. Just use a small RC low pass so peaks become flat a little to measure. If you need to measure positive and negative peaks also shift the Gnd to Adc/2.

If the negative is no needed clamp it with diode.

Last edited:

#### victoriomariani

Joined Nov 8, 2023
7
Assuming the Esp32 A/D conversion rate is at least 100k samples/sec. you can just rapidly sample the 1kHz signal and look for the peak signal in each of the samples over the 1kHz period.
Great idea! Do you know any code for this?

#### victoriomariani

Joined Nov 8, 2023
7
You need to create a signal when the peak occurs so ESP knows exact time when to trig an ADC.
This signal can be made from precisely calculated RC differentiator. The edge before peak seems to be very sharp so you can use just RC with very short time constant.
There is also possible to use a comparator like LM393 with reference set to about 1/2 of smallest peak. After comparator signal rise delay the ADC start so you sample in the middle of peak.

Btw, there is no need a rectifier. Just use a small RC low pass so peaks become flat a little to measure. If you need to measure positive and negative peaks also shift the Gnd to Adc/2.

If the negative is no needed clamp it with diode.
Eu preciso usar o amostrador e criar este sinal ou apenas um dos dois?

Moderator edit: This is an English language website. Please use English only.

Do I need to use the sampler and create this signal or just one of the two?

#### Liam_Lucas

Joined Apr 23, 2024
3
Assuming the Esp32 A/D conversion rate is at least 100k samples/sec. you can just rapidly sample the 1kHz signal and look for the peak signal in each of the samples over the 1kHz period.
Thank you so much for giving the awesome idea!

#### Michal Podmanický

Joined May 11, 2019
67
I would do this synchronizing with simple analog circuit , without using Adc. Otherwise, you have to sample very often.

#### victoriomariani

Joined Nov 8, 2023
7
I am using a superdiode and this circuit. The transistor is active by esp32.

but I think my code is not ideal, if you have a suggestion, please write me.
Code:
//Função para pulsar os LEDs
float tensao;
unsigned long inicio_loop;
const unsigned long periodo = 500;
float soma_tensao = 0.0;
pinMode(pin_LED, OUTPUT);
pinMode(pin_transistor, OUTPUT);
pinMode(pin_fotodiodo, INPUT);

for (int i = 0; i < 128; i++) {
inicio_loop = micros();
digitalWrite(pin_LED, HIGH);

// Aguarda até que o período definido se passe
while(micros() - inicio_loop < periodo){
}
// Desliga o LED após o período definido
digitalWrite(pin_LED, LOW);
delayMicroseconds(10);
digitalWrite(pin_transistor, HIGH);

digitalWrite(pin_transistor, LOW);
// Aguarda o restante do período
while(micros() - inicio_loop < 2 * periodo){
}
}

tensao = (soma_tensao * 3.3) / 4095; // Converte o valor ADC em tensão
float media_tensoes = tensao / 128;
Serial.print("Tensão medida: ");
Serial.println(media_tensoes);
}

void setup() {
Serial.begin(9600); //Para comunicação USB
//lerArquivo(); //Quando for descarregar os dados é necessário habilitar esta linha
}

void loop() {
// Gera 128 pulsos de 1 kHz no pino 19 que alimenta o circuito NIR, lê a tensão medida pelo fotodiodo e descarrega o capacitor
}