4.1.5 Intelligent Visual Doorbell¶
Introduction¶
In this project, let’s make a DIY intelligent visual doorbell.
Required Components¶
In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Schematic Diagram¶
T-Board Name |
physical |
wiringPi |
BCM |
GPIO27 |
Pin 13 |
2 |
27 |

Experimental Procedures¶
Step 1: Build the circuit.

Before this project, you need to make sure you complete 3.1.3 Audio Module & 3.1.2 Video Module.
Step 2: Get into the folder of the code.
cd ~/raphael-kit/python/
Step 3: Run.
python3 4.1.5_DoorBell.py
After the code runs, when the button is pressed, a bell will sound, and the camera will record a 5s video, which is stored as the visitor.h264
file in the ~
directory. If you have a screen, you can also view visitors by previewing the video in real time.
Code
Note
You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like raphael-kit/python
. After modifying the code, you can run it directly to see the effect.
#!/usr/bin/env python3
from picamera import PiCamera
from pygame import mixer
import RPi.GPIO as GPIO
import time
import os
user = os.getlogin()
user_home = os.path.expanduser(f'~{user}')
camera = PiCamera()
BtnPin = 18
status = False
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(BtnPin, GPIO.IN, GPIO.PUD_UP)
mixer.init()
def takePhotos(pin):
global status
status = True
def main():
global status
GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=takePhotos)
while True:
if status:
mixer.music.load(f'{user_home}/raphael-kit/music/doorbell.wav')
mixer.music.set_volume(0.7)
mixer.music.play()
camera.start_preview(alpha=200)
camera.start_recording(f'{user_home}/visitor.h264')
print ('Have a visitor')
time.sleep(5)
mixer.music.stop()
camera.stop_preview()
camera.stop_recording()
status = False
def destroy():
GPIO.cleanup()
mixer.music.stop()
camera.stop_preview()
camera.stop_recording()
if __name__ == '__main__':
setup()
try:
main()
except KeyboardInterrupt:
destroy()
Code Explanation
status = False
This is a flag used to record whether the doorbell is used.
GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=takePhotos)
Set the event of BtnPin
, when the button is pressed (the level signal changes from high to low) , call the function takePhotos()
.
if status:
mixer.music.load(f'{user_home}/raphael-kit/music/doorbell.wav')
mixer.music.set_volume(0.7)
mixer.music.play()
camera.start_preview(alpha=200)
camera.start_recording(f'{user_home}/visitor.h264')
print ('Have a visitor')
time.sleep(5)
mixer.music.stop()
camera.stop_preview()
camera.stop_recording()
status = False
Five seconds are used here to play music and record videos, thus functioning as a doorbell.