Is there an esp-idf equivalent call like arduino IDE's tone() function to beep on a pin?

zliudr
Posts: 198
Joined: Thu Oct 03, 2019 5:15 am

Is there an esp-idf equivalent call like arduino IDE's tone() function to beep on a pin?

Postby zliudr » Fri Aug 07, 2020 5:13 pm

Before I start looking all over the place, I'd like to see if there's already existing calls to cause a pin to beep out a certain frequency like arduino's tone(frequency, duration) call? The arduino version uses an avr mcu's counter and comparetor. My goal is to play some tone and led patterns when esp32 receives the patterns, not to reproduce some .wav files etc with DAC. Thanks.

[UPDATE]: The latest arduino-esp doesn't support tone(). I just tried to compile an example sketch with tone() in arduino IDE 1.8.x.

Here's something I'm looking into but it's not esp-idf so needs some adaptation.

https://techtutorialsx.com/2017/07/01/e ... -with-pwm/
Last edited by zliudr on Fri Aug 07, 2020 11:36 pm, edited 1 time in total.


zliudr
Posts: 198
Joined: Thu Oct 03, 2019 5:15 am

Re: Is there an esp-idf equivalent call like arduino IDE's tone() function to beep on a pin?

Postby zliudr » Mon Aug 10, 2020 3:44 pm

Thanks. I'm using ESP-IDF as I'm posting this under ESP32 IDF. I don't find a simple way to port the code from this arduino framework.Does this file exist in ESP-IDF at all? I think not. There's no instruction on how to use the code. I hate to do this. Everything is esp-idf except for pinMode, which requires the whole shebang of arduino definition files.

zliudr
Posts: 198
Joined: Thu Oct 03, 2019 5:15 am

Re: Is there an esp-idf equivalent call like arduino IDE's tone() function to beep on a pin?

Postby zliudr » Mon Aug 10, 2020 4:41 pm

There is no way I can use this code without dragging the whole arduino-esp32 core with it. Such a nightmare!

chegewara
Posts: 1211
Joined: Wed Jun 14, 2017 9:00 pm

Re: Is there an esp-idf equivalent call like arduino IDE's tone() function to beep on a pin?

Postby chegewara » Mon Aug 10, 2020 7:20 pm

I am not telling you to use or port arduino code. It was just example to show how it is implemented in arduino using ledc. It is just esp-idf ledc code wrapped and used in arduino-esp32.

https://docs.espressif.com/projects/esp ... /ledc.html

zliudr
Posts: 198
Joined: Thu Oct 03, 2019 5:15 am

Re: Is there an esp-idf equivalent call like arduino IDE's tone() function to beep on a pin?

Postby zliudr » Tue Aug 11, 2020 4:01 am

OK, here is my implementation:


I have one thing I don't know how, disable the output and return the GPIO to say input state. Any advice?

Code: Select all

void esp32_beep(unsigned char key_num, unsigned char dur_hms)
{
	ledc_timer_config_t ledc_timer;
	ledc_channel_config_t ledc_channel;

	ledc_timer.duty_resolution = LEDC_TIMER_13_BIT;	// resolution of PWM duty
	ledc_timer.freq_hz = notes[key_num];						// frequency of PWM signal
	ledc_timer.speed_mode = LEDC_HS_MODE;			// timer mode
	ledc_timer.timer_num = LEDC_HS_TIMER;			// timer index

	// Set configuration of timer0 for high speed channels
	ledc_timer_config(&ledc_timer);
	
	ledc_channel.channel    = LEDC_HS_CH0_CHANNEL;
	ledc_channel.duty       = 4096;
	ledc_channel.gpio_num   = ESP32Buzzer;
	ledc_channel.speed_mode = LEDC_HS_MODE;
	ledc_channel.hpoint     = 0;
	ledc_channel.timer_sel  = LEDC_HS_TIMER;

	ledc_channel_config(&ledc_channel);
	vTaskDelay(pdMS_TO_TICKS(dur_hms*100));
	ledc_stop(LEDC_HS_MODE,LEDC_HS_CH0_CHANNEL,0);
}
The function takes note names like NOTE_C4 etc.

Code: Select all

const uint16_t notes[]  ={
	0,31,33,35,37,39,41,44,46,49,52,55,58,62,65,69,73,78,82,87,93,98,104,110,117,123,131,139,147,156,165,175,185,196,208,220,
	233,247,262,277,294,311,330,349,370,392,415,440,466,494,523,554,587,622,659,698,740,784,831,880,932,988,1047,1109,1175,1245,1319,1397,1480,1568,1661,
	1760,1865,1976,2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,4186,4435,4699,4978};

typedef enum
{
	NO_NOTE	= 0,
	NOTE_A0	,
	NOTE_AS0	,
	NOTE_B0	,
	NOTE_C1	,
	NOTE_CS1	,
	NOTE_D1	,
	NOTE_DS1	,
	NOTE_E1	,
	NOTE_F1	,
	NOTE_FS1	,
	NOTE_G1	,
	NOTE_GS1	,
	NOTE_A1	,
	N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