Guest User

i2s

a guest
Dec 10th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include "driver/i2s.h"
  3.  
  4. #define I2S_PORT I2S_NUM_0
  5. #define ADC_CHANNEL ADC1_CHANNEL_6 // GPIO34
  6.  
  7. void setup() {
  8. Serial.begin(115200);
  9. Serial.println("Initializing I2S ADC...");
  10.  
  11. // Configure I2S for ADC mode
  12. i2s_config_t i2s_config = {
  13. .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_ADC_BUILT_IN),
  14. .sample_rate = 44100,
  15. .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
  16. .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
  17. .communication_format = I2S_COMM_FORMAT_I2S_MSB,
  18. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  19. .dma_buf_count = 4,
  20. .dma_buf_len = 1024
  21. };
  22.  
  23. esp_err_t err = i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
  24. if (err != ESP_OK) {
  25. Serial.printf("I2S driver install failed: %d\n", err);
  26. return;
  27. }
  28. Serial.println("I2S driver installed successfully.");
  29.  
  30. // Set ADC mode for I2S
  31. i2s_set_adc_mode(ADC_UNIT_1, ADC_CHANNEL);
  32. Serial.println("ADC mode set for I2S.");
  33.  
  34. // Enable I2S ADC mode
  35. err = i2s_adc_enable(I2S_PORT);
  36. if (err != ESP_OK) {
  37. Serial.printf("I2S ADC enable failed: %d\n", err);
  38. return;
  39. }
  40. Serial.println("I2S ADC enabled successfully.");
  41. }
  42.  
  43. void loop() {
  44. uint16_t adcBuffer[1];
  45. size_t bytesRead;
  46.  
  47. esp_err_t err = i2s_read(I2S_PORT, adcBuffer, sizeof(adcBuffer), &bytesRead, portMAX_DELAY);
  48. if (err != ESP_OK) {
  49. Serial.printf("I2S read failed: %d\n", err);
  50. delay(1000);
  51. return;
  52. }
  53.  
  54. uint16_t adcValue = adcBuffer[0] & 0xFFF;
  55. Serial.printf("ADC Value: %d\n", adcValue);
  56. delay(100);
  57. }
  58.  
  59.  
  60. I get this:
  61. Initializing I2S ADC...
  62. I2S driver installed successfully.
  63. ADC mode set for I2S.
  64. E (16) i2s(legacy): i2s_adc_enable(899): i2s built-in adc not enabled
  65. I2S ADC enable failed: 259
  66. E (28) i2s(legacy): i2s_read(1739): RX mode is not enabled
  67. I2S read failed: 258
  68.  
Advertisement
Add Comment
Please, Sign In to add comment