Motion Detection Camera

Reading Time: 11 minutes
Pi Zero Motion Detection Camera

Ok, now for something a bit different, and this will be a bit more in depth as a tutorial rather than just my ramblings.

The back story is that we knew we had hedgehogs in the neighbouring gardens. Or as my non-English speaking neighbour said: “It like a mouse with needles”. So with permission from next door, we cut a small hole (about 15cm) in the fence to help the little critters extend their territory. I figured it might be nice to get some video of them on their travels so put this camera together.

I reckon, once you get started, even if you make sure you take your time with the trickier bits, this shouldn’t take more than an hour or two if you’ve never done anything like this before. I’ve built a few of these and I’d say I can probably do it in about half an hour now.

Below is a little shopping list to get you started. These are all Amazon UK links. These are the basics but a good starting point. All links will open in a new window.

  • Raspberry Pi Zero W Starter kit
  • Power supply – this is not the official one but it should be good enough. Important note: Your camera is going to be outside so you need to get power to it. It’s vital that this is done safely. If you’re going to power your camera using mains, get something like this waterproof extension cable and box. It’ll come in handy in the garden for other stuff as well.
  • Alternative power supply – If you want, you can get one of these power banks instead of or as well as the mains one. This will power your little camera for plenty of time to last over night. You can charge it during the day and it’s also a nice little power bank for other things as well. It has the advantage of making your camera much more portable
  • Night vision camera – This is a great little camera with built in IR lighting so you get night vision. It’s not going to be massively powerful. It won’t light your whole garden but it will give you enough if you have your camera located close to where the hedgehogs are going to be wandering past. Note: This camera is what’s known as a NOIR camera. This means it has no infrared filter on the lens. So in daytime your images will come out with a slightly pinkish hue
  • Optional lenses – If you want, you can add on one of these wide angle lenses. It’s not critical but it can help where you have your camera in a place where it’s going to have a narrow field of vision
  • Optional replacement micro SD card – The kit linked above includes a micro SD card. It should be fine but I tend to stick with the SanDisk brand for reliability. These cards have a finite life when they are being written to a lot, such as recording video. I had one of these running for about 2 years as a camera and never had a single problem so it might be worth thinking about
  • USB SD card reader – You’ll need this if you don’t have a slot in your computer to read and write micro SD cards.
  • A clear plastic weather proof box – I’m using a food storage container.
  • A USB keyboard
  • An HDMI lead to plug your Pi into a monitor or TV.

In your kit, you should get something like the images below:

When it’s all assembled and in the case you get in the starter kit it’ll look like the last pic which is also at the top of this page. Ignore my tatty case. It’s one I was using for something else but it’s good enough for this exercise when the whole thing is going to be in another box anyway.

The cable that joins the camera to the Raspberry Pi needs to go in with the black bit pointing up or the camera won't work.

And be careful connecting it as it's quite fragile.

The video here shows how to insert the cable into the connector.

You just need to be really gentle with it.  The connector is designed so that you don't have to use any force.  The little black clip then clamps the cable in place.

Setting it all up

I’m hoping that this will be easier than putting an IKEA bookcase together! I’m going to keep to simple steps and will try to avoid too much explanation about why you’re doing things.

Preparing your Pi

This looks like a long process. In reality, it can take as little as 20 to 30 minutes. I’d read it through first, then go back when you have everything ready to go. Links will open in new tabs/windows.

When you open Balena Etcher you’ll see a screen like this:

Balena Etcher Screen
  • Click in the button that says “Flash from file” and select the Raspberry Pi OS image you downloaded
  • Insert your micro SD card into the SD card adapter you got with your USB SD card reader and plug it into a spare USB port on your computer
  • Click the “Select target” button on Balena Etcher and choose your SD card. You’ll probably find that it will only see one card. Just make sure that it is your SD card and not a hard drive plugged into your system!
  • Click the “Flash!” button.
  • After a minute or two, you’ll get a message to say you’ve been successful and you can pull the USB SD card reader out.
  • Remove the micro SD card and insert it into the slot on your Raspberry Pi Zero. Look at the image above and you’ll see mine peeking out on the right. You need the label facing up
  • Plug your USB converter into the micro USB port on the Raspberry Pi Zero – it’s the one on the right as you look from above with the 2 micro USB ports at the top.
  • Plug your HDMI adapter into the mini HDMI socket on the Raspberry Pi Zero – the larger socket near the micro SD card slot
  • Plug your keyboard into the adapter
  • Plug an HDMI cable into the HDMI adapter and plug the other end into a TV or monitor
  • Plug your power supply into the left hand micro USB port.

You should hopefully see a nice rainbow pattern on the screen, closely followed by lots of stuff scrolling by. After a few minutes, it will give you a login prompt. You’re going to use “pi” as the username and “raspberry” as the password.

You should see something like this:

The very first thing you’re going to type is:

sudo passwd

This will ask you to type in the existing password (raspberry) and then a new one.

Now type in:

sudo raspi-config

You’ll see a screen like this:

sudo raspi-config

Again, I’ll keeps this as brief as possible. Note, you use the arrows to move around but sometimes you might have to use the “Tab” key to move from the menu to the bottom options. Once an option is highlighted, use the return key to actually select it. Some of these steps will take a little time and it might look like it’s crashed. Especially when you’re setting up some of the localisation. If something is sat there for more than a minute or 2 then something is probably not right. For this tutorial, we have to assume the happy path.

  • Hit return to go into “System Options
  • Select “Hostname” and hit return once your in that menu. Call it anything you want but keep it simple. no spaces or special characters.
  • Once you select “Ok”, it might send you back to the main menu so just hit return to go into “System Options” again. The one you’re looking for now is “Wireless LAN”. This will ask you to enter the SSID of your WiFi. You should be able to find this on your phone or any other device connected to your WiFi. Enter this exactly as it shows on your phone. It’s case sensitive. Hit return and enter your WiFi password.

    Note: You will need to use your 2.4Ghz network. Not your 5Ghz. Your network at home might have the same name for both in which case, you’re fine. But if you get to choose then go for the 2.4Ghz one.
  • Now find “Interface Options” on the main menu and select that.
  • Select “Camera” and hit return. It’ll ask if you want to enable the camera and “Yes” will be selected so you can just hit return again.
  • It will then take you back to the main menu so go to “interface Options” again.
  • Select “SSH” and hit return. It’ll ask if you want to enable SSH and “Yes” will be selected so you can just hit return again.
  • Lastly, go into “Localization Options
  • Go into “Locale” and find “en-GB.UTF-8 UTF-8”. If it has an “*” then you’re fine. If not, press the space bar to select it.
  • Hit tab to select “OK” and then hit return.
  • Back into “Localization Options” and select “Timezone”
  • Select Europe and then London by highlighting them with the cursor and hitting return
  • “Localization Options” again
  • “WLAN country” and find “GB Britain (UK)”. Make sure it’s highlighted and hit return
  • Use the arrows and/or tab key to highlight “Finish” and hit return.
  • It will probably ask you if you want to reboot. Select “Yes” and then wait until you get the login prompt again.

Honestly, that looks onerous but it’ll take 5 minutes. We’re on the home straight now… I promise!

So, log back in using “pi” as the username and the password you set back at the start.

Type in:

ifconfig

…and hit return.

Look for a bit that starts with “WLAN0”.

Under that you will hopefully see something that starts with “inet”. Next to that is the IP address of your Raspberry Pi on your network. This assumes that you set everything up right when you were in the system settings above. If you don’t have something like 192.168.0.99 then something is wrong. It might be different to that number but it should be a similar format of 4 numbers with a “.” between them.

Write this down.

As a side note, if you know how, you should be able to log into your internet router and reserve this address so that your Raspberry Pi always gets the same one. It will make like a bit easier if you can do this.

At this point, you have your Pi set up and you will hopefully have a connection to the internet. If you have, move on. If not, you’ll need to see if you can find out which bit hasn’t worked and correct it.

Now you’re going to type in a few commands – or copy and paste them from here. After the second one on this list, go and put the kettle on as it can take a while. These two commands are downloading all the updates for your computer.

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

Your Pi is now updated with the latest software.

sudo apt-get install git
git clone https://github.com/silvanmelchior/RPi_Cam_Web_Interface.git

You’ve now installed something called “git” that allows you to load software from other people’s repositories and you’ve downloaded the camera software.

cd RPi_Cam_Web_Interface

That’s put you in the right directory

./install.sh

That should bring up a screen like this:

Camera Installation Page

The best advice here is just to copy what’s on this screen. Once you’ve done that, use the tab key to highlight “OK” and hit return.

It’ll now spend some time doing all the good stuff that it needs to do. At the end it’ll ask you if you want to start the software. Select “Yes” and hit return again.

it should now drop you out of this to the command prompt. It might not be obvious but you’ll see the command prompt appear at the bottom of the screen.

So close now!

Open your browser and type in http://YOUR_IP_ADDRESS/cam

Obviously you’ll need to put the IP address in that you wrote down a few minutes ago. So, in my case, it’s http://192.168.1.66/cam. Unless there’s a huge coincidence, yours will be different.

You should now see something like this. Hopefully you’ll see the room you’re in, not my office, or something has gone terribly wrong 🙂

There are a couple of settings on here that might make life easier.

Underneath this bit are links for setting for the system and the camera. Go into the ones for the camera. At the top, you’ll see setting for the video resolution. Set this to 1296 x 972. And set the fps to 25 for both settings. Set the image resolution to 2592 x 1944.

This will give you a nice big preview and nice big images if you capture any stills.

Look at your camera and work out which way up it’s going to be when it’s in your garden. Scroll down the page and look for a setting for “Rotation”. You can use this to make sure your videos are the right way up.

Look for “Image quality” and set it to 50.

Look for “Preview quality”. Set that to 10 and the width to 1024. The width is important. It needs to be a multiple of 256 or I’ve seen it crash. 1024 should be a nice starting point.

Look for “Motion detect mode” and set that to “Internal”

You’re done!

You might need to tweak some of the settings once you get going. This is a cracking system to play with and can be very sensitive. You want it to detect a hedgehog but, probably, to ignore an ant. If you want very detailed instructions on the software, have a look at the wiki page.

Now, go and place your camera. Come back to your computer and click on the “motion detection start” button. Go back and do a little victory dance in front of your camera. Come back in and wait a minute or two. Click on the “Download videos and images” button and there should be one waiting of you doing your dance.

At the moment, mine isn’t in place as I’ve got it next to me to type this and take pics. The hedgehogs have only been coming in for a few days and mine hasn’t been used in the field yet. As soon as I have some video, I’ll add it in here.

When you get to grips with it, there is all sorts you can do. I’ve just integrated mine with my home automation system so it sends me an alert if motion is detected.

Just one last note. One of the things you did was enable SSH. This allows you to connect to your Pi from another computer rather than having to plug it into a monitor and use a USB keyboard. If you’re on a Mac, just open a terminal window and then type in:

ssh pi@192.168.1.99

Replace the “192.168.1.99 with the IP address you used previously. This will then ask you for the password, which is the one you changed the default one to.

It might ask you to confirm that you want to add it to your keychain. Type “yes” and hit return. You should now be connected to your Pi in the same way as you did earlier. It’ll be much easier if you need to do any maintenance.

If you have Windows, I’d advise getting a programme called Putty. This allows you to set up connections. Unfortunately, I don’t use a Windows PC so I don’t know a great deal about it.

If you really get stuck, post a comment. Spam WILL be deleted before it gets published so don’t even try.

Yay!

First night for mine. Works perfectly.

Bless you!