TEST_ sensor69 — ESP32-S3 simulation

An interactive ESP32-S3 circuit simulation you can run free in your browser on Velxio, by aekkachai-c2545.

Components

Sketch code

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <driver/i2s.h>

// --- กำหนดขา I2C สำหรับ ADXL345 ---
#define I2C_SDA 8
#define I2C_SCL 9
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

// --- กำหนดขา I2S สำหรับ ReSpeaker Lite (ไมโครโฟน / เสียงออก) ---
#define I2S_PORT      I2S_NUM_0
#define I2S_SCK       12  // BCLK (Bit Clock)
#define I2S_WS        14  // WS (Word Select / LRC)
#define I2S_DIN       6   // ข้อมูลเสียงเข้า (Data In - จากไมค์)
#define I2S_DOUT      7   // ข้อมูลเสียงออก (Data Out - ไปลำโพง)

void setupI2S() {
  i2s_config_t i2s_config = {
    // ตั้งค่าเป็น SLAVE เพื่อรับสัญญาณ Clock จากบอร์ด ReSpeaker Lite
    .mode = (i2s_mode_t)(I2S_MODE_SLAVE | I2S_MODE_RX | I2S_MODE_TX),
    .sample_rate = 16000,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
    .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_I2S),
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 4,
    .dma_buf_len = 512,
    .use_apll = false,
    .tx_desc_auto_clear = false,
    .fixed_mclk = 0
  };

  i2s_pin_config_t pin_config = {
    .bck_io_num = I2S_SCK,
    .ws_io_num = I2S_WS,
    .data_out_num = I2S_DOUT,
    .data_in_num = I2S_DIN
  };

  // ติดตั้งและเริ่มใช้งานไดรเวอร์ I2S
  i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
  i2s_set_pin(I2S_PORT, &pin_config);
  i2s_start(I2S_PORT);
}

void setup() {
  Serial.begin(115200);
  delay(1000);

  // เริ่มต้นการทำงานระบบ I2C และเซนเซอร์ ADXL345
  Wire.begin(I2C_SDA, I2C_SCL);
  if (!accel.begin()) {
    Serial.println("❌ ไม่พบเซนเซอร์ ADXL345! กรุณาตรวจสอบการต่อสาย SDA/SCL");
    while (1);
  }
  
  // ตั้งค่าช่วงการวัดและความละเอียดของ ADXL345
  accel.setRange(ADXL345_RANGE_16_G);
  accel.setDataRate(ADXL345_DATARATE_100_HZ);
  Serial.println("✅ ADXL345 พร้อมใช้งาน!");

  // เริ่มต้นระบบเสียง I2S สำหรับ ReSpeaker Lite
  setupI2S();
  Serial.println("✅ ReSpeaker Lite I2S พร้อมใช้งาน!");
}

void loop() {
  // 1. อ่านค่าความเร่งจากเซนเซอร์ ADXL345
  sensors_event_t event;
  accel.getEvent(&event);
  
  Serial.print("Accel [m/s^2] -> X: "); Serial.print(event.acceleration.x);
  Serial.print(" | Y: "); Serial.print(event.acceleration.y);
  Serial.print(" | Z: "); Serial.print(event.acceleration.z);

  // 2. อ่านค่าสัญญาณเสียง I2S จากบอร์ด ReSpeaker Lite
  int32_t buffer[32];
  size_t bytesRead = 0;
  i2s_read(I2S_PORT, &buffer, sizeof(buffer), &bytesRead, portMAX_DELAY);
  
  // คำนวณหาค่าเฉลี่ยความดังของเสียงในบัฟเฟอร์
  int32_t sum = 0;
  int samplesRead = bytesRead / sizeof(int32_t);
  if (samplesRead > 0) {
    for (int i = 0; i < samplesRead; i++) {
      sum += abs(buffer[i]); // รวมค่าแอมพลิจูด
    }
    sum = sum / samplesRead; // หาค่าเฉลี่ย
  }

  Serial.print("\t| Bytes: "); Serial.print(bytesRead);
  Serial.print("\t| Mic Volume: "); Serial.println(sum);

  delay(100);
}

More projects by aekkachai-c2545