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

Arduino UNO Based Obstacle Detection and Directional Audio Alert System

View and Edit Project In Cirkit DesignerImage of Arduino UNO Based Obstacle Detection and Directional Audio Alert System

Circuit Documentation

Summary of the Circuit

This circuit is designed to interact with the physical environment using an Arduino UNO as the central processing unit. It features an HC-SR04 Ultrasonic Sensor for distance measurement, an SG90 servo motor for directional movement, a speaker for audio output, and an SD module for audio file storage and playback. The primary function of the circuit is to detect objects at a specific distance (2 meters) using the ultrasonic sensor. Upon detection, the speaker will output a verbal warning, and the servo motor will pivot to check for clear paths on either side. The speaker will then instruct on which direction to turn based on the sensor readings from the servo's adjusted positions.

Component List

Arduino UNO

HC-SR04 Ultrasonic Sensor

SG90 Servo Motor

Speaker

SD Module

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

SG90 Servo Motor

Speaker

SD Module

Documented Code

/*
 * This Arduino sketch controls a servo motor and a speaker using an ultrasonic
 * sensor. When the sensor detects an object exactly 2 meters away, the speaker
 * will say "roadblocks detected, stop". The servo motor will then check its
 * right and left to determine which direction is clear of obstacles. The
 * speaker will then announce "turn right" or "turn left" based on the clear
 * direction.
 */

#include <Servo.h>
#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>

#define TRIG_PIN 9
#define ECHO_PIN 10
#define SERVO_PIN 5
#define SPEAKER_PIN 6
#define SD_ChipSelectPin 4

TMRpcm tmrpcm;
Servo myServo;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(SPEAKER_PIN, OUTPUT);
  myServo.attach(SERVO_PIN);
  Serial.begin(9600);

  // Initialize SD card
  if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println("SD card initialization failed");
    return;
  }
  Serial.println("SD card initialized");

  // Initialize TMRpcm
  tmrpcm.speakerPin = SPEAKER_PIN; // Pin connected to the speaker or amplifier
  // ... other setup code ...
}

void loop() {
  long duration, distance;
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = (duration / 2) / 29.1;

  if (distance == 200) { // 2 meters
    speak("roadblocks detected, stop");
    delay(1000);
    int rightDistance = checkDirection(90); // Check right
    int leftDistance = checkDirection(0); // Check left
    if (rightDistance > leftDistance) {
      speak("turn right");
    } else {
      speak("turn left");
    }
  }
  delay(500);
}

int checkDirection(int angle) {
  myServo.write(angle);
  delay(1000);
  long duration, distance;
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = (duration / 2) / 29.1;
  return distance;
}

void speak(const char* message) {
  Serial.println(message);
  if (strcmp(message, "roadblocks detected, stop") == 0) {
    tmrpcm.play("stop.wav"); // Play "stop.wav" from the SD card
  } else if (strcmp(message, "turn right") == 0) {
    tmrpcm.play("turnright.wav"); // Play "right.wav" from the SD card
  } else if (strcmp(message, "turn left") == 0) {
    tmrpcm.play("turnleft.wav"); // Play "left.wav" from the SD card
  }
}

This code is responsible for the operation of the circuit. It initializes the components, reads the distance from the ultrasonic sensor, and controls the servo and speaker based on the sensor's readings. The speak function plays audio files from the SD card corresponding to the detected situation.