Raspberry Pi Zero & ESP32-CAM Based Object Detection System with Ultrasonic Distance Measurement and Audio Feedback
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit integrates a Raspberry Pi Zero with an ESP32-CAM module, an HC-SR04 Ultrasonic Sensor, and a Speaker. The primary function of this system is to capture images using the ESP32-CAM, process them for object detection, and provide audio feedback through the Speaker. The HC-SR04 Ultrasonic Sensor is used to measure the distance to objects. The Raspberry Pi Zero serves as the central processing unit, controlling the ESP32-CAM, processing sensor data, and driving the Speaker for audio output.
Component List
ESP32-CAM
- Description: A small-sized ESP32-based module with a camera and microSD card interface.
- Pins: 5V, GND, IO12, IO13, IO15, IO14, IO2, IO4, VOT, VOR, VCC, IO0, IO16, 3V3
Raspberry Pi Zero
- Description: A low-cost, compact version of the Raspberry Pi with a broad GPIO interface.
- Pins: 5V, 3.3V, GPIO2 (SDA), GND, GPIO3 (SCL), GPIO14, GPIO4, GPIO15, GPIO18, GPIO17, GPIO27, GPIO23, GPIO22, GPIO24, GPIO10, GPIO25, GPIO9, GPIO8, GPIO11, GPIO7, ID_SC, ID_SD, GPIO5, GPIO12, GPIO6, GPIO13, GPIO16, GPIO19, GPIO20, GPIO26, GPIO21
Speaker
- Description: An audio output device for emitting sound.
- Pins: +, -
HC-SR04 Ultrasonic Sensor
- Description: A sensor for measuring distance through ultrasonic sound waves.
- Pins: VCC, TRIG, ECHO, GND
Wiring Details
ESP32-CAM
- VOT connected to Raspberry Pi Zero GPIO15
- VOR connected to Raspberry Pi Zero GPIO14
- GND connected to Raspberry Pi Zero GND
- 3V3 connected to Raspberry Pi Zero 3.3V
Raspberry Pi Zero
- 5V connected to HC-SR04 Ultrasonic Sensor VCC
- GND connected to Speaker (-) and HC-SR04 Ultrasonic Sensor GND
- GPIO18 connected to Speaker (+)
- GPIO23 connected to HC-SR04 Ultrasonic Sensor TRIG
- GPIO24 connected to HC-SR04 Ultrasonic Sensor ECHO
Speaker
- (+) connected to Raspberry Pi Zero GPIO18
- (-) connected to Raspberry Pi Zero GND
HC-SR04 Ultrasonic Sensor
- VCC connected to Raspberry Pi Zero 5V
- TRIG connected to Raspberry Pi Zero GPIO23
- ECHO connected to Raspberry Pi Zero GPIO24
- GND connected to Raspberry Pi Zero GND
Documented Code
/*
* This Arduino Sketch is designed for a Raspberry Pi Zero connected to an ESP32 CAM,
* an HC-SR04 Ultrasonic Sensor, and a Speaker. The system captures images using the
* ESP32 CAM, processes them for object detection, and provides audio feedback via
* the speaker. Additionally, the HC-SR04 sensor measures distance to objects.
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <ESP32CAM.h>
#include <Ultrasonic.h>
#include <TTS.h>
// WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// ESP32 CAM URL
const char* esp32_cam_url = "http://<ESP32_CAM_IP_ADDRESS>/capture";
// Ultrasonic Sensor pins
#define TRIG_PIN 23
#define ECHO_PIN 24
// Speaker pin
#define SPEAKER_PIN 18
// Initialize Ultrasonic Sensor
Ultrasonic ultrasonic(TRIG_PIN, ECHO_PIN);
// Initialize TTS
TTS tts(SPEAKER_PIN);
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize ESP32 CAM
ESP32CAM.begin();
}
void loop() {
// Capture image from ESP32 CAM
HTTPClient http;
http.begin(esp32_cam_url);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
WiFiClient* stream = http.getStreamPtr();
ESP32CAM.capture(stream);
if (ESP32CAM.detectObject()) {
tts.speak("Object Detected");
} else {
tts.speak("No Object Detected");
}
} else {
Serial.println("Failed to capture image");
}
http.end();
// Measure distance using Ultrasonic Sensor
long distance = ultrasonic.MeasureInCentimeters();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Delay before next loop
delay(1000);
}
This code is designed to be uploaded to the Raspberry Pi Zero, which controls the ESP32-CAM for image capture and object detection, the HC-SR04 Ultrasonic Sensor for distance measurement, and the Speaker for audio feedback. The code includes setup for WiFi connectivity, initialization of the camera and sensor modules, and a loop that captures images, detects objects, measures distance, and provides audio output.