Generate single frequency(audio siren) without CPU ussage - Page 1
Forum section: electronics / general

author: BinaryWorld
author_rank: Newbie
timestamp: May 19, 2018, 04:33:23 pm
content: Hi everyone,I'm doing my project on SparkFun ESP32 Thing with ArduinoIDEhttps://botland.com.pl/plytki-zgodne-z-arduino-sparkfun/7911-sparkfun-esp32-thing-modul-wifi-i-bluetooth-ble-kompatybilny-z-arduino-ide.htmland i try to generate siren start procedure(ready .. go; 3 signals: low pitched, low pitched, higher pitched).When i add loop audio():Code:void audio(float frq){ // frq Hzif(sound == 0){sound = micros();}if(sound + (1/frq)*500000 >= micros() ){digitalWrite(buzzer, HIGH);}else{digitalWrite(buzzer, LOW);}if(sound + (1/frq)*1000000 <= micros() ){,sound = 0;digitalWrite(buzzer, HIGH);}}//2500//2500//3000 hzsound isn't clear. I don't use delay() in my program, but other things happen at the same time (reading expander pcf8574 via i2c, every 1ms).I read about pwm(dma?), in technical references:https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdfbut i don't know how to use it(without any library, becouse it should be easy).Will there be a helpful hand that will help me understand how to easily generate any frequency from 1-10 khz on gpio?Thank you for all ideas

author: rstofer
author_rank: Super Contributor
timestamp: May 19, 2018, 06:19:22 pm
content: Will this work?https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/

author: BinaryWorld
author_rank: Newbie
timestamp: May 19, 2018, 06:24:11 pm
content: Unfortunately, this feature does't work with esp library.I found this:https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWMPhase-Correct PWM look great. Its tutorial for ATmega.I am trying to translate this to esp32 (with esp32 register).I understand how here set(choose) prescaler value and cycles:Output A frequency: 16 MHz / 64 / 256 = 976.5625HzThe output frequency is the 16MHz system clock frequency, divided by the prescaler value (64), divided by the 256 cycles it takes for the timer to wrap around.

author: MarkF
author_rank: Super Contributor
timestamp: May 19, 2018, 06:56:06 pm
content: I'm not familiar with the SparkFun ESP32. However, you need to have a fixed update rate in order to generate the desired frequency. You will get this via some hardware function. Most liklely you are looking at a PWM function or some interrupt.It appears that you are trying to generate a square wave. I that case I would use one of the PWM channels and let the hardware do the timing for the updates.If you wish to generate a sine wave. You would store one period of a sine wave in memory. (The number of samples you have will be determined by cpu clock, desired frequency output, sine wave step size.) Then via an interrupt, sequence through the samples sending each to a DAC channel. The rate and how you step through the sine samples and the interrupt frequency will determine the frequency.

author: BinaryWorld
author_rank: Newbie
timestamp: May 19, 2018, 07:16:41 pm
content: If I understand you correctly, you are talking about the method used here:https://github.com/espressif/esp8266_mp3_decoderI2S bus + DAC. Propably it's best way to generate audio(like mp3) but it need interrupt.I'd rather avoid it.I want to set it once and do it without the processor (like PWM dma).I don't use interrupts to detect false start.My (extended) program is able to detect the change at the input gpio without interrups.Accuracy is below 1 ms, measurement is up to 1 ms - its ok.Probably the rectangular course will sound good. If it turns out that I need sine wave, maybe an external chip could be used?

author: MarkF
author_rank: Super Contributor
timestamp: May 19, 2018, 07:48:37 pm
content: In a word - No.I'm not familiar with the I2S interface.If you want a pure tone, you will need to generate a sine wave. A square wave contains many harmonics caused by the voltage steps. If you just want a siren sound, a square wave maybe good enough and the easiest to setup.In the sine wave generation, I'm talking about digital signal processing. Unless you use the processor, you will need to add hardware to create a sine wave. I would recommend you look at the AD9834 chip. Using the processor, you need a constant update rate (i.e. send a sample each time an interrupt occurs) to send sine wave samples to a D/A converter. Either the built-in D/A (if it has enough resolution) or some external D/A like a MCP4802.