Untitled
unknown
plain_text
10 months ago
1.7 kB
11
Indexable
#include <Wire.h>
#include <driver/i2s.h>
// I2S Microphone (INMP441) Pins
#define I2S_WS 15 // Word Select (L/R)
#define I2S_SD 32 // Serial Data (DOUT)
#define I2S_SCK 14 // Serial Clock
// LED Pin
#define LED_PIN 2
// I2S Configuration
void setupMic() {
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 16000, // Sample rate
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S,
.intr_alloc_flags = 0,
.dma_buf_count = 8,
.dma_buf_len = 64,
.use_apll = false
};
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_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM_0, &pin_config);
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
setupMic();
}
void loop() {
int16_t audio_data[64];
size_t bytes_read;
// Read data from microphone
i2s_read(I2S_NUM_0, &audio_data, sizeof(audio_data), &bytes_read, portMAX_DELAY);
// Compute average volume level
long sum = 0;
for (int i = 0; i < 64; i++) {
sum += abs(audio_data[i]);
}
int average = sum / 64;
// Print the microphone data
Serial.print("Mic Level: ");
Serial.println(average);
// Blink LED based on sound level
if (average > 500) { // Adjust this threshold if needed
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
delay(100);
}
Editor is loading...
Leave a Comment