This circuit is designed to perform various functions including sound detection, timekeeping, servo control, and Bluetooth communication. It features an Arduino UNO microcontroller at its core, interfaced with a KY-037 microphone sensor, a DS1307 real-time clock module, two MG996R servos, a Bluetooth HC-06 module, a 4-channel relay, LEDs, pushbuttons, resistors, diodes, and capacitors. The circuit is powered by a 9V battery and a 12V power supply. The Arduino UNO controls the components based on sensor inputs, time, and Bluetooth commands to perform actions such as dispensing food at scheduled times or upon receiving a command.
#include <Wire.h> // For I2C communication (RTC and other I2C devices)
#include <RTClib.h> // RTC library for DS1307
#include <Servo.h> // Servo library
// Pin Definitions
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int led4 = 5;
const int button1 = 6;
const int button2 = 7;
const int relay1 = 11;
const int relay2 = 12;
const int relay3 = 13;
const int relay4 = A0;
const int soundSensorPin = A1;
const int servo1Pin = 9;
const int servo2Pin = 10;
// Components
Servo servo1;
Servo servo2;
RTC_DS1307 rtc;
void setup() {
// Serial Communication for Bluetooth
Serial.begin(9600);
// Pin Modes
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(soundSensorPin, INPUT);
// Attach Servos
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set the RTC to the date & time this sketch was compiled
}
// Initial states
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
servo1.write(0); // Initial position for Servo 1
servo2.write(0); // Initial position for Servo 2
}
void loop() {
// Get current time from RTC
DateTime now = rtc.now();
// Feeding schedule (e.g., feed at 8:00 AM and 6:00 PM)
if ((now.hour() == 8 && now.minute() == 0) || (now.hour() == 18 && now.minute() == 0)) {
dispenseFood();
}
// LED Control (optional for indicator)
digitalWrite(led1, HIGH); // Turn on LED 1 to indicate system is active
delay(500); // Wait for 500ms
digitalWrite(led1, LOW); // Turn off LED 1
delay(500); // Wait for 500ms
// Check Button 1 for manual food dispensing
if (digitalRead(button1) == LOW) {
delay(50); // Debounce delay
while (digitalRead(button1) == LOW); // Wait for button release
dispenseFood(); // Manually dispense food when button is pressed
}
// Sound Sensor Detection for automatic dispensing (optional feature)
int soundLevel = analogRead(soundSensorPin);
if (soundLevel > 500) { // Example threshold for sound detection
Serial.println("Sound Detected!");
dispenseFood();
}
// Bluetooth Command (Send 'F' to feed or 'T' to get current time)
if (Serial.available()) {
char command = Serial.read();
if (command == 'F') {
dispenseFood(); // Bluetooth command to dispense food
} else if (command == 'T') {
sendCurrentTime();
}
}
}
// Function to dispense food using servos
void dispenseFood() {
Serial.println("Dispensing food...");
// Rotate Servo 1 to release food
servo1.write(90); // Move Servo 1 to 90 degrees
delay(1000); // Wait for food to dispense
servo1.write(0); // Move Servo 1 back to 0 degrees
delay(1000); // Wait
// Rotate Servo 2 to reset dispenser or perform additional task
servo2.write(90); // Move Servo 2 to 90 degrees
delay(1000);
servo2.write(0); // Reset Servo 2
delay(1000);
// Indicate that food has been dispensed
digitalWrite(led2, HIGH); // Turn