espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.61k stars 7.27k forks source link

How can I control speaker volume for MAX98357 I2S (IDFGH-9884) #11201

Closed waruqi closed 1 year ago

waruqi commented 1 year ago

Answers checklist.

General issue report

I have a MAX98357 I2S module, I want to control it's volume.

How do I read and write its gain gpio to control the volume?

L-KAYA commented 1 year ago

Do you mean controlling the gain of MAX98357? According to the datasheet, it seems to be controlled by pull-up/pull-down resistors.

waruqi commented 1 year ago

Do you mean controlling the gain of MAX98357? According to the datasheet, it seems to be controlled by pull-up/pull-down resistors.

What api should I use to control it? pwm? gpio? ledc?

waruqi commented 1 year ago

chatgpt tell me:

#include "driver/gpio.h"
#include "driver/dac.h"

void set_volume(int volume) {
    gpio_set_direction(GPIO_NUM_25, GPIO_MODE_OUTPUT);
    int voltage = volume * 255 / 100;
    dac_output_voltage(DAC_CHANNEL_1, voltage);
}

or

    gpio_set_direction(6, GPIO_MODE_OUTPUT);
    gpio_set_level(6, 1);
    ets_delay_us(900);
    gpio_set_level(6, 0);
    ets_delay_us(100);

Is it correct?

L-KAYA commented 1 year ago

The ChatGPT didn't give a correct answer... You can search the datasheet of MAX98357 online or the official website of Analog Devices

Here is the datasheet I found: https://www.analog.com/media/en/technical-documentation/data-sheets/MAX98357A-MAX98357B.pdf

In the section Gain Selection, the table shows how to set the gain by pull-up or pull-down resistor: image

That means the gain should NOT controlled by a GPIO theoretically, it's supposed to be fixed according to your PCB design. If you have to control it by a GPIO, you may try to tie a GPIO to the GAIN_SLOT pin of MAX98357 and set the gpio pulled up/ pulled down or float to select the different gain. But I can't guarantee it will work, please read the MAX98357 datasheet carefully to see if it support to be controlled by a GPIO.

waruqi commented 1 year ago

thanks!

ekrem commented 11 months ago

maybe problem solve whit 100k pot. Because manual says need analog voltage