How to Ring a Doorbell Automatically with Object Detection
In this article
This guide shows you how to build a system that automatically rings a doorbell when it detects a person or package at your door. You'll use a camera, a computer with Python, and a smart doorbell chime. By the end, you'll have a working prototype that sends a notification and plays a chime whenever motion is detected.
What You'll Need
- A camera (USB webcam or Raspberry Pi camera module)
- A computer (Raspberry Pi 4/5 or a laptop with Python installed)
- A smart plug or relay module that can be controlled via Wi-Fi (e.g., a Sonoff or TP-Link Kasa smart plug)
- A doorbell chime or buzzer that can be powered through the smart plug
- Python 3.7+ with pip installed
- OpenCV and a pre-trained object detection model (YOLOv4 or MobileNet SSD)
- Optional: a speaker for audible alerts
Step-by-Step
- Set up the camera and computer. Position the camera so it clearly views the area in front of your door. Connect it to your computer (or Raspberry Pi). Test that the camera feed works with a simple app like Cheese (Linux) or Camera (Windows).
- Install Python and required libraries. Open a terminal and run:
pip install opencv-python numpy imutils. For object detection, also installpip install tensorflowif you plan to use a TensorFlow model, or download the YOLOv4 weights and config files from the official darknet GitHub repository. - Download a pre-trained model. For YOLOv4, download the
yolov4.weightsandyolov4.cfgfiles. For MobileNet SSD, download thessd_mobilenet_v1_coco_2017_11_17model from TensorFlow's model zoo. Place them in a folder namedmodel. - Write the detection script. Create a Python file (e.g.,
detect.py). Use OpenCV to read frames from the camera. Load the model usingcv2.dnn.readNetfor YOLO orcv2.dnn.readNetFromTensorflowfor MobileNet. Run detection on each frame and check if any detected object belongs to classes like 'person' or 'bicycle' (COCO class IDs 0 and 1). - Add a cooldown timer. To avoid ringing the bell repeatedly for the same person, track the last time the bell was triggered. Only trigger again after, say, 30 seconds have passed. Use
time.time()to compare timestamps. - Control the smart plug. Install the appropriate library for your smart plug. For TP-Link Kasa, use
python-kasa(pip install python-kasa). For Sonoff, use thesonofflibrary or control via MQTT. Write a function that turns the plug on for 1 second, then off, to simulate a button press. - Wire the doorbell chime. Plug the chime into the smart plug. Ensure the chime operates on mains voltage (check its label). If it's a low-voltage doorbell, you'll need a relay module instead. Connect the relay's control pins to the Raspberry Pi's GPIO (e.g., GPIO17) and use the
RPi.GPIOlibrary to pulse the relay. - Run the script. Execute
python detect.py. The script will open a window showing the camera feed with bounding boxes around detected objects. When a person is detected, the script will trigger the smart plug or relay, ringing the chime. - Test and adjust. Walk in front of the camera to verify detection. Adjust the confidence threshold (e.g., 0.5) and the cooldown time as needed. Also consider setting a region of interest (ROI) to ignore motion outside the door area.
Common Mistakes
- Using the wrong model input size. YOLOv4 expects 416x416 input. If you resize the frame incorrectly, detection accuracy drops. Use
cv2.dnn.blobFromImage(frame, 1/255.0, (416, 416), swapRB=True). - Not checking class IDs. The COCO dataset has 80 classes. 'Person' is class 0, but if you accidentally use class 1 (bicycle), you'll ring the bell for bikes. Always filter by the correct class ID.
- Ignoring the cooldown. Without a cooldown, the bell will ring continuously while a person stands in view. This is annoying and may damage the chime.
- Powering a low-voltage doorbell directly from a smart plug. Smart plugs output mains voltage, which can fry a 12V doorbell. Use a relay for low-voltage circuits.
- Running the script on a laptop that goes to sleep. The detection stops when the computer sleeps. Use a Raspberry Pi or disable sleep mode.
Tips
- Use a dedicated Raspberry Pi. It's low-power and always-on, perfect for this project.
- Add a notification. Use
pushbulletortelegramto send a message to your phone when the bell rings. - Record snapshots. Save a frame when detection occurs to see who was at the door.
- Optimize for speed. Reduce the frame resolution to 640x480 and skip every other frame for faster processing.
- Use a USB camera with good low-light performance. Nighttime detection is crucial for a doorbell.
Troubleshooting
- No detection at all. Check that the camera feed is working. Print the frame shape to confirm. Ensure the model files are correctly loaded (no errors in the console). Lower the confidence threshold to 0.3 temporarily.
- Detection is too slow. Use a lighter model like MobileNet SSD. Also, reduce input size to 320x320. If on a Raspberry Pi, consider using the Coral USB Accelerator for hardware acceleration.
- The bell doesn't ring. Test the smart plug manually with a phone app. Check the GPIO pin number and that the relay is wired correctly (common ground). For a smart plug, ensure the script can reach the device's IP address.
- Bell rings for cars or animals. Filter classes more strictly. You can also set a minimum detection confidence and a minimum bounding box area to ignore small objects.
- Script crashes after a few minutes. This is often a memory leak. Use
cv2.VideoCaptureand release it properly. Restart the script periodically with a cron job.
Frequently Asked Questions
Can I use a regular smartphone as the camera?
Yes, you can use an app like IP Webcam to stream video to your computer, then feed that stream into OpenCV. Replace the camera index with the IP address URL.
Do I need to train my own model?
No. Pre-trained models like YOLOv4 or MobileNet SSD already detect people and common objects. Training your own model is only needed for custom objects, like a specific package shape.
How do I avoid false triggers from shadows or moving trees?
Use a region of interest (ROI) that excludes those areas. Also, increase the confidence threshold and require detection for a few consecutive frames before triggering.
Can I use a smart doorbell instead of a chime?
Yes, but you'd need to integrate with its API. Many smart doorbells have their own motion detection, but this project is for a DIY solution with a standard chime.
Is this legal to install on my own property?
Generally yes, as long as you don't record audio without consent in some jurisdictions. Video recording on your own property is usually allowed, but check local privacy laws.
What if I don't have a smart plug?
You can use a simple relay controlled by a microcontroller (like an Arduino) connected to your computer via USB. The script can send a serial command to trigger the relay.