Raspberry Pi As Security Camera With Motion Detection

It was a fun weekend project I did with my kids. We started with a new Pi Zero and in a few hours (of many ‘paths’ to nowhere) we got into the point of having a useful security camera. The useful part is when the camera sends you alerts (email or Telegram messages) when it detects movements.

We open the package and connected the Pi Zero to a USB power, a keyboard, a mouse and monitor. We cut a bit a corner by buying an SD card with NOOBS on it but it wasn’t working (nothing was coming up on the screen when we boot the Pi). So we downloaded a new version from Raspian Jessie 4.4 from NOOBS and install it. Now when we boot the Pi we got a new screen. We open the terminal and typed:

sudo raspi-config

This will open the Raspberry Pi Configuration tool.

In the tool you should make sure to enable:

  • The Camera – after all we need the camera as our input source.
  • The SSH Server – So we could work like pros.
  • Boot to command line without manual login.

At the terminal prompt type enter:


(!) Make sure to have a strong password so no one could connect to your Pi.

Update your new OS with:

sudo apt-get update
sudo apt-get dist-upgrade

Now it’s a good time to check that your camera is working. Type:

raspistill -o theFirstImageFromPi.jpg

We will go with Motion as it got a low footprint. After all the Pi got very limited resources in terms of CPU, Memory.

The first step is to install motion. You can type:

sudo apt-get install motion

If something is not working with the apt-get you can try to download the last releases and install it with gdebi tool:

sudo apt-get install gdebi-core


sudo gdebi pi_jessie_motion_4.1.1-1_armhf.deb


You can open the configuration file in edit mode by entering:

mkdir ~/.motion
cp /etc/motion/motion.conf ~/.motion/motion.conf

sudo vi ~/.motion/motion.conf

Next go to this motion.conf gist and just copy it.


motion -c ~/.motion/motion.conf

You can connect to the web page that steam the videos at:

http://YourLocalPi-IP-address:9081 <-- Stream page

http://YourLocalPi-IP-address:9080 <-- Admin page


We will set two types of alerts: emails and telegram messages.


sudo apt-get install ssmtp
sudo apt-get install mailutils
sudo apt-get install mpack
sudo vi /etc/ssmtp/ssmtp.conf

# Below is the content for ssmtp.conf file. Make sure it's NOT your main gmail as we have the password here without encryption!


Now test that you can send emails:

mpack -s "The first email from my new Pi" /home/pi/logs/motion/someFile.txt

(!) Don’t worry as the motion.conf file is already configure to use mpack and send you the emails with the images once a motion is being detected! 


Now, if you like Telegram and wish to get these alerts in it as well – just use this IFTT integration between your gmail and telegram.

Last but not least, add the starting of motion to your rc.local file:

motion -c ~/.motion/motion.conf

You should also put a little script to clean images from Pi once the du -h is giving your less then ~25% of free space.


Be safe and happy.

Here is the motion.conf file:


One thought on “Raspberry Pi As Security Camera With Motion Detection

  1. Paul says:

    hey! this looks great, can you clarify how to link the email to Telegram – the link you posted doesn’t appear to work and I’d like to get this rather than the email…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s