Notifications
Clear all

Get the mic output from a INMP441, MAX98357 and ESP32

14 Posts
4 Users
5 Reactions
4,371 Views
(@isuru)
Member
Joined: 1 year ago
Posts: 5
Topic starter  

Hi

I am doing a project that gets the voice input from INMP441 mic module into ESP-WROOM-32 and outputs the audio in real-time into a speaker using MAX98357. I created the following code, in this code serial plotter shows clearly the sound is detected but the output is not coming how to fix this issue please help.

 

#include <driver/i2s.h>
#include <SPI.h>


// Connections to INMP441 I2S microphone
#define I2S_WS 25
#define I2S_SD 33
#define I2S_SCK 32


// Use I2S Processor 0
#define I2S_PORT I2S_NUM_0


// Define input buffer length
#define bufferLen 64
int16_t sBuffer[bufferLen];


// Connections to MAX98357 amplifier
#define MAX98357_DOUT 26
#define MAX98357_BCLK 27
#define MAX98357_LRC 14


void i2s_install() {
// Set up I2S Processor configuration
const i2s_config_t i2s_config = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 44100,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
.intr_alloc_flags = 0,
.dma_buf_count = 8,
.dma_buf_len = bufferLen,
.use_apll = false
};


i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
}


void i2s_setpin() {
// Set I2S pin configuration
const i2s_pin_config_t pin_config = {
.bck_io_num = I2S_SCK,
.ws_io_num = I2S_WS,
.data_out_num = I2S_PIN_NO_CHANGE,
.data_in_num = I2S_SD
};


i2s_set_pin(I2S_PORT, &pin_config);
}


void max98357_setup() {
// Configure MAX98357 amplifier pins
pinMode(MAX98357_DOUT, OUTPUT);
pinMode(MAX98357_BCLK, OUTPUT);
pinMode(MAX98357_LRC, OUTPUT);
}


void max98357_write(uint8_t reg, uint16_t data) {
// Write data to MAX98357 amplifier via SPI protocol
digitalWrite(MAX98357_LRC, HIGH);
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
SPI.transfer(0x70 | (reg >> 7)); // Set control bits to address mode
SPI.transfer(reg << 1);
SPI.transfer16(data);
SPI.endTransaction();
digitalWrite(MAX98357_LRC, LOW);
}


void max98357_init() {
// Initialize MAX98357 amplifier
max98357_write(0x01, 0x21); // Power up
max98357_write(0x08, 0x00); // Enable DAC
}


void setup() {
// Set up Serial Monitor
Serial.begin(115200);
Serial.println(" ");


delay(1000);


// Set up I2S
i2s_install();
i2s_setpin();
i2s_start(I2S_PORT);


// Set up MAX98357 amplifier
max98357_setup();
max98357_init();


delay(500);
}


void loop() {
// False print statements to "lock range" on serial plotter display
int rangelimit = 3000;
Serial.print(rangelimit * -1);
Serial.print(" ");
Serial.print(rangelimit);
Serial.print(" ");


// Get I2S data and place in data buffer
size_t bytesIn = 0;
esp_err_t result = i2s_read(I2S_PORT, &sBuffer, bufferLen * 2, &bytesIn, portMAX_DELAY);


if (result == ESP_OK) {
int16_t samples_read = bytesIn / 2;
if (samples_read > 0) {
float mean = 0;
for (int16_t i = 0; i < samples_read; ++i) {
mean += sBuffer[i];
}


// Average the data reading
mean /= samples_read;


// Write audio data to MAX98357 amplifier
max98357_write(0x0A, static_cast<uint16_t>(mean)); // Amplify the sound (adjust gain)


// Print to serial plotter
Serial.println(mean);
}
}
}

   
Quote
(@davee)
Member
Joined: 3 years ago
Posts: 1762
 

Hi @isuru,

   Sorry, I don't have any I2S peripherals, so I can't test your code .. maybe someone else will be able to help.

However, in case you are totally stuck, from your description, you have the microphone side working, but maybe you haven't tested the output amplifier side. Of course, you might have tested it separately .. I am only guessing.

However, if the output system isn't tested, at first glance, I suspect you have enough hardware to duplicate the Internet radio Bill (@dronebot-workshop) described in the second part of

https://dronebotworkshop.com/esp32-i2s/

So, if you haven't already done it, I suggest you carefully go through this whole video + blog, and try to get the Internet radio part to work. It will be an opportunity to check all of your electronics is functional, and knowing the quality of Bill's work, you will almost certainly learn something useful, maybe even enough to fix your problem.

Good luck and best wishes, Dave


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7326
 

@isuru This sounds similar to another member who had a problem, and the wiring diagram pointed out the problem. Maybe a few photos plus a schematic might help. Also, test the most simple circuit to prove out various components.

I would gladly do that, but everything is on its way to our new apartment, and I won't get to it until close to the end of June.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@isuru)
Member
Joined: 1 year ago
Posts: 5
Topic starter  

Hi @davee 
I am really great full for your reply. I checked these two sides from the video mic input side and the radio output side they are working properly but I cannot get the mic output as this. This is my 1st year final project in my university I am only having 2 weeks please help me guys 😥 .


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7326
 

@isuru FYI @davee Whose name goes on the degree?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
DaveE reacted
ReplyQuote
(@isuru)
Member
Joined: 1 year ago
Posts: 5
Topic starter  

Hi @zander 
I am


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7326
 

@isuru You got some gall.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@isuru)
Member
Joined: 1 year ago
Posts: 5
Topic starter  

@zander I didn't get that 😥


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7326
 

@isuru And that is the saddest part. You don't even know.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
Isuru reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2548
 

@isuru 

I think what Ron is saying is that it's unfair to the rest of your class and also to all of the other folks who have already graduated with that degree for you to ask us to help you solve this problem. The degree you are working for is supposed to be an indication to the rest of the world that YOU have mastered a basic skill set associated with that degree.

When Ron asks whose name will be on the diploma, he's pointing out that you will not have EARNED the right to say that you have done the work for it since you will not have completed the project yourself. Having the forum solve your problems while the rest of the class has to do their own projects and solve their own problems is unfair to them.

Anything seems possible when you don't know what you're talking about.


   
huckOhio and Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7326
 

@will Thanks, Will. I know I am a bit of a pest about this, but my life experience includes watching over the shoulder of University students copying the work of other University students (all from the same village in ?????). Thank goodness the software the Universities use today to detect plagiarism will catch that sort of cheating. In one of the most bizarre cases I have ever encountered, I once saw the secretary wife of a male IT employee doing his work. In that case, at least nobody was cheated. They just needed to switch jobs.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@isuru)
Member
Joined: 1 year ago
Posts: 5
Topic starter  

@zander I didn't ask you to do my entire assignment for me. This is only a minor portion of my project. Also, when we complete a project, we always refer to the internet, but I can't find the appropriate procedure for this, so I'm asking for help. Please don't think this is cheating. I am very disappointed. If you can't help please tell me that you cannot do this. Thank you so lot, people. I had finally found a solution to this problem.

This post was modified 1 year ago by Isuru

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7326
 

@isuru I am happy you found it on your own. Using the Internet to read information is no different than going to the library like we did before the internet, but asking people to help is very different even if it is only a small part of the entire project.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1762
 

Hi @isuru,

   Please try to understand that first year projects are supposed to be part of the 'exam' system, so asking someone to fix your project is similar to getting someone else to do part of one your exam papers for you. Hence, we have to be very careful to avoid that situation.

-----------------

  I too am pleased you have found an answer to your problem .. sometimes just describing the problem to someone else is enough to enable you to 'see' what you were previously missing.

 I wish you the best of luck with your degree result and your future career.

Dave

 


   
ReplyQuote