Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Based Voice Assistant with Battery-Powered Microphone and Speaker

View and Edit Project In Cirkit DesignerImage of ESP32-Based Voice Assistant with Battery-Powered Microphone and Speaker

Circuit Documentation

Summary

This circuit is designed to interface various components including microphones, speakers, sensors, and microcontrollers to create a functional embedded system. The system includes an ESP32 microcontroller for processing and communication, an INMP441 microphone for audio input, a Max98357 amplifier for audio output, an IR sensor for proximity detection, a four-pin LED for visual indicators, and a TP4056 module for battery management. The circuit is powered by a Polymer Lithium Ion Battery and includes a toggle switch for power control.

Component List

  1. INMP441 FRONT MIC

    • Description: Digital MEMS microphone
    • Pins: L/R, WS, SCK, SD, VDD, GND
  2. Polymer Lithium Ion Battery - 850mAh

    • Description: Rechargeable battery
    • Pins: GND, VCC
  3. 8 OHM 20W SPEAKER

    • Description: Speaker for audio output
    • Pins: + speaker, - speaker
  4. IR Sensor

    • Description: Infrared sensor for proximity detection
    • Pins: out, gnd, vcc
  5. TP4056 Battery Charging Protection Module (Type C)

    • Description: Battery charging and protection module
    • Pins: OUT +, B+, B-, OUT -, +, -
  6. Toggle Switch

    • Description: Switch for power control
    • Pins: L1, COM, L2
  7. ESP32 (30 pin)

    • Description: Microcontroller with Wi-Fi and Bluetooth capabilities
    • Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3
  8. LED: Four Pin (Common Cathode)

    • Description: RGB LED with common cathode
    • Pins: red anode, common cathode, green anode, blue anode
  9. Max98357

    • Description: I2S audio amplifier
    • Pins: LRC, BLCK, DIN, GAIN, SD, GND, VIN, -, +

Wiring Details

INMP441 FRONT MIC

Polymer Lithium Ion Battery - 850mAh

8 OHM 20W SPEAKER

IR Sensor

TP4056 Battery Charging Protection Module (Type C)

Toggle Switch

ESP32 (30 pin)

LED: Four Pin (Common Cathode)

Max98357

Documented Code

ESP32 (Instance 1)

#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

// Wi-Fi credentials
const char* ssid = "Atharv";
const char* password = "Atharv844";

// Gemini API settings
const char* Gemini_Token = "AIzaSyBBt67xV8mscC1pF1kUOf9_WCgbRnO9GrM";
const char* Gemini_Max_Tokens = "100";

// Helper variables
String res = "";

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

  // Initialize Wi-Fi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("\nWi-Fi Connected!");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // Request Speech-to-Text from Python
  Serial.println("stt");
  delay(500); // Wait for Python module to process

  // Receive query text from Python
  String userQuery = "";
  while (Serial.available() == 0); // Wait until data is received
  while (Serial.available()) {
    userQuery += (char)Serial.read();
    delay(10);
  }
  userQuery.trim();

  if (userQuery.isEmpty()) {
    Serial.println("No speech recognized, skipping...");
    delay(1000);
    return;
  }

  Serial.println("User Query: " + userQuery);

  // Prepare Gemini API request
  HTTPClient https;
  String payload = "{\"contents\": [{\"parts\":[{\"text\":\"" + userQuery + "\"}]}],\"generationConfig\": {\"maxOutputTokens\": " + (String)Gemini_Max_Tokens + "}}";

  // Make HTTP POST request to Gemini API
  String apiURL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=" + String(Gemini_Token);
  https.begin(apiURL);
  https.addHeader("Content-Type", "application/json");
  int httpResponseCode = https.POST(payload);

  String answer = "";
  if (httpResponseCode == HTTP_CODE_OK) {
    String response = https.getString();
    DynamicJsonDocument doc(1024);
    deserializeJson(doc, response);

    // Parse the Gemini API response and convert it into a String
    String answer = doc["candidates"][0]["content"]["parts"][0]["text"].as<String>();
    answer.trim();

    // Log the answer
    Serial.println("Gemini API Response: " + answer);

    // Send Text-to-Speech request to Python
    Serial.print("tts ");
    Serial.println(answer);
  } else {
    answer = "Sorry, I couldn't process your query.";
  }
  https.end();

  Serial.println("Gemini API Response: " + answer);

  // Send Text-to-Speech request to Python
  Serial.print("tts ");
  Serial.println(answer);

  delay(5000); // Wait for speech to play