Learn Arduino, lesson 23 of 27

Building a Smart Pet Door with Arduino: A DIY Guide

intermediate3 min read

Introduction

Pet owners often face the dilemma of ensuring their furry companions have access to the outdoors while maintaining security and control over their homes. A smart pet door offers a convenient solution, allowing pets to come and go freely while keeping unwanted intruders out. In this article, we'll explore how to create a smart pet door using Arduino, providing your pet with the freedom they desire and ensuring your home remains secure.

Working Principle

A smart pet door operates on a simple yet effective principle: it detects when your pet is near and grants them access. This is achieved through a combination of sensors, a microcontroller (in this case, Arduino), and a mechanism to control the door's lock.

Things You'll Need

Before you begin, gather the following components:

  1. Arduino Board: Any model will work, but Arduino Uno or Arduino Nano are popular choices.
  2. Ultrasonic Sensor: This sensor is used to detect your pet's presence.
  3. Servo Motor: To control the door's lock.
  4. Power Supply: A 9V power supply for the Arduino.
  5. Wood or Plastic: To build the frame for your pet door.
  6. Tools: Screwdrivers, wires, connectors, and a saw.
  7. Connectivity: Jumper wires for connecting components.

Circuit Diagram

To create the smart pet door, follow this basic circuit diagram:

Circuit Diagram

Working of the System with Arduino

  1. Build the Frame: Start by building the physical frame of the pet door using wood or plastic. Ensure it's the right size for your pet to comfortably pass through.
  2. Attach the Ultrasonic Sensor: Place the ultrasonic sensor on the outside of the door. It will detect when your pet approaches.
  3. Mount the Servo Motor: Secure the servo motor on the inside of the door, which will control the door's lock.
  4. Wiring: Connect the components to the Arduino using jumper wires. Make sure to connect the sensor's trigger and echo pins to specific digital pins on the Arduino, and the servo motor to a PWM (Pulse Width Modulation) pin.
  5. Programming: Write the Arduino code to control the system. Here's a simple example:
Loading the circuit…Arduino Uno
Press Run to upload the code to the Arduino.

Arduino Code

#include <Servo.h>
 
Servo servo;
int triggerPin = 2; // Define the trigger pin for the ultrasonic sensor.
int echoPin = 3;    // Define the echo pin for the ultrasonic sensor.
int doorOpen = 90;  // Servo angle to open the door.
 
void setup() {
  servo.attach(9);  // Attaches the servo to pin 9.
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}
 
void loop() {
  long duration, distance;
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
 
  if (distance < 10) {
    servo.write(doorOpen);
    delay(3000);  // Keep the door open for 3 seconds.
    servo.write(0);  // Close the door.
  }
}

Conclusion

Building a smart pet door with Arduino is an exciting project that combines electronics, mechanics, and programming. It allows your pet to enjoy outdoor freedom while keeping your home secure. This DIY solution is not only cost-effective but also customizable, enabling you to tailor it to your pet's needs. Remember to consider power sources, safety, and test the system thoroughly before allowing your pet to use it regularly. With some patience and a bit of tinkering, you'll have a smart pet door that your furry friend will love.