Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP32-C3 Controlled Wi-Fi Smart Camera with OLED Display and Interactive LED Indicators

Image of ESP32-C3 Controlled Wi-Fi Smart Camera with OLED Display and Interactive LED Indicators

Circuit Documentation

Summary

The circuit in question appears to be a smart device that utilizes an ESP32-C3 microcontroller to interface with various peripherals including an OLED display, a pushbutton, multiple red LEDs, a breadboard power supply module, and potentially a camera module. The ESP32-C3 is responsible for handling Wi-Fi connectivity, user input via the pushbutton, and displaying information on the OLED screen. The red LEDs serve as indicators, likely for status or output from the device's operations.

Component List

Microcontroller

  • ESP32-C3: A Wi-Fi and Bluetooth enabled microcontroller that serves as the brain of the circuit, controlling the OLED display, reading the pushbutton input, and driving the LEDs.

Display

  • OLED 1.3": A small display that shows information to the user, connected to the ESP32-C3 via I2C communication.

Input Device

  • Pushbutton: A user interface component that allows the user to interact with the circuit, triggering certain actions when pressed.

Indicators

  • LED: Two Pin (red): These are used as visual indicators, likely to represent different statuses or outputs based on the microcontroller's logic.

Power Supply

  • MB102 Breadboard Power Supply Module 3.3V/5V: Provides regulated power to the circuit components from an external power source.

Camera (Not connected in the provided net list)

  • OV2640 Camera Module: A camera module that could be used for image capture, though it is not wired in the provided net list.

Wiring Details

ESP32-C3

  • GND: Connected to the ground (GND) of the OLED display and the breadboard power supply module.
  • 3.3V: Powers the OLED display and is also connected to the breadboard power supply module.
  • GPIO10 / MOSI: Connected to the SCL pin of the OLED display for I2C communication.
  • GPIO9 / MISO: Connected to the SDA pin of the OLED display for I2C communication.
  • GPIO7 / SCL: Connected to the input pins of the pushbutton.
  • GPIO5 / A3 - GPIO2 / A0: Connected to the anodes of individual red LEDs.
  • GPIO21 / TX, GPIO6 / SDA: Also connected to the anodes of individual red LEDs.

OLED 1.3"

  • GND: Connected to the ground (GND) of the ESP32-C3 and the breadboard power supply module.
  • VCC: Powered by the 3.3V output from the breadboard power supply module.
  • SCL: Connected to GPIO10 / MOSI of the ESP32-C3.
  • SDA: Connected to GPIO9 / MISO of the ESP32-C3.

Pushbutton

  • Pin 1 (in) / Pin 2 (in): Connected to GPIO7 / SCL of the ESP32-C3.
  • Pin 3 (out) / Pin 4 (out): Connected to the cathodes of all red LEDs and the ground (GND) of the breadboard power supply module.

LED: Two Pin (red)

  • Cathode: Connected to the output pins of the pushbutton and the ground (GND) of the breadboard power supply module.
  • Anode: Connected to various GPIO pins on the ESP32-C3 for control.

MB102 Breadboard Power Supply Module 3.3V/5V

  • GND: Provides ground to the ESP32-C3, OLED display, and red LEDs.
  • VCC: Powers the ESP32-C3 and OLED display.
  • 3.3V / 5V: Outputs for providing regulated voltage, connected to their respective ground (GND) pins.

Documented Code

#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define BUTTON_PIN D6
#define INDICATOR_LED D5
#define LED_A D0
#define LED_B D1
#define LED_C D2
#define LED_D D3
#define LED_E D4

#define I2C_SCL D10
#define I2C_SDA D9

const char* ssid = "ssid";
const char* password = "wifi password";
const char* api_endpoint = "https://api.openai.com/v1/chat/completions";
const char* api_key = "API KEY";

void setup() {
  // Initialize Serial, Wi-Fi, OLED, and pins
  Serial.begin(115200);
  Wire.begin(I2C_SDA, I2C_SCL);

  // Initialize the OLED display
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Adjust address if needed
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.display();
  delay(2000); // Pause for 2 seconds
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);

  pinMode(INDICATOR_LED, OUTPUT);
  pinMode(LED_A, OUTPUT);
  pinMode(LED_B, OUTPUT);
  pinMode(LED_C, OUTPUT);
  pinMode(LED_D, OUTPUT);
  pinMode(LED_E, OUTPUT);
  
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  // Connecting to Wi-Fi
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Connecting to WiFi");
  display.display();

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    display.print(".");
    display.display();
  }
  Serial.println("Connected to Wi-Fi");

  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Ready for Input");
  display.display();
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    digitalWrite(INDICATOR_LED, HIGH); // Turn on indicator LED

    // Display phase: Capture image
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print("Capturing Image...");
    display.display();

    // Capture and send image, then get answer
    String answer = captureAndSendImage();

    // Display answer phase
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print("Answer: ");
    display.setCursor(8, 0);
    display.print(answer);
    display.display();

    // Light up corresponding answer LED
    resetLEDs();
    if (answer == "A") digitalWrite(LED_A, HIGH);
    else if (answer == "B") digitalWrite(LED_B, HIGH);
    else if (answer == "C") digitalWrite(LED_C, HIGH);
    else if (answer == "D") digitalWrite(LED_D, HIGH);
    else if (answer == "E") digitalWrite(LED_E, HIGH);

    // Display phase: Process complete
    display.setCursor(0, 1);
    display.print("Process Complete");
    display.display();

    delay(1000); // Keep LEDs on for a second
    digitalWrite(INDICATOR_LED, LOW); // Turn off indicator LED
    
    // Reset screen after a delay
    delay(2000);
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print("Ready for Input");
    display.display();
  }
}

void resetLEDs() {
  digitalWrite(LED_A, LOW);
  digitalWrite(LED_B, LOW);
  digitalWrite(LED_C, LOW);
  digitalWrite(LED_D, LOW);
  digitalWrite(LED_E, LOW);
}

String captureAndSendImage() {
  // Display phase: Sending to ChatGPT
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Sending to ChatGPT");
  display.display();

  HTTPClient http;
  http.begin(api_endpoint);
  http.addHeader("Content-Type", "application/json");
  http.addHeader("Authorization", String("Bearer ") + api_key);

  // Placeholder for actual image data - replace <encoded_image_data> with real data
  String payload = "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"Analyze the image and respond with one of the options: A, B, C, D, or E.\"}]}";

  int httpResponseCode = http.POST(payload);

  String answer;
  if (httpResponseCode > 0) {
    String response = http.getString();
    answer = parseAnswer(response);  // Extract the answer from the response
  } else {
    Serial.println("Error in HTTP request");
    answer = "Error";  // Default in case of error
    display.setCursor(0, 1);
    display.print("Error: No Response");
    display.display();
  }

  http.end();
  return answer;
}

String parseAnswer(String response) {
  // Parse JSON response to extract "A", "B", "C", "D", or "E"
  int start = response.indexOf(":") + 2;
  int end = response.indexOf("}") - 1;
  return response.substring