Unit 03 Section 02 Lesson 10

Motion Activated Camera

Assigned activities

In this activity you will use a Passive Infrared sensor to detect motion which causes a camera to record a video clip to the computers hard drive in .avi format.

Activity 1
Part 1

Required Resources

  • Phidget IFKit 888
  • Passive infrared sensor (PIR - Parallax) with cable
  • Web Cam (USB)

Circuit Configuration

Before you connect the PIR etc. make sure the cable on the PIR is correct. See the photos below and confirm the cable is connected correctly with the black wire on the left as seen in the last image.

Parallax PIR sensor

Under side of PIR sensor

Use a Black Red White sensor cable

Connect the web cam to the computer. Connect the IFkit 888 to the computer. Plug the PIR into Analog Port 0 of the IFKit 888.

Create the Graphical User Interface

Note: Save your project at frequent intervals!

Run Visual Basic 6.  Select a Standard Exe.

  • Draw a Frame on the form.
  • Draw the following control on the frame.
    • A Timer
  • Open the Project menu and select Components.
  • Scroll down to and select ezVidCap. Note: you must click the box so that the check mark appears. Click OK

  • Look at the bottom of the tool bar. A new control should be added (circled in red). Select and draw on the frame.

  • Draw the EZVidCap control on the frame. A suggested layout is shown below.

Change the properties for the following:

  • Frame1
    • Caption - Critter Cam
    • Font - Arial, 14pt, bold
    • Backcolor - something appropriate
  • Timer1
    • Interval - 10000
    • Enabled - False

Code the Interface

  • You will be using the Phidget IFKit 888 therefore the Phidget library must be referenced. Open the Project menu and select References.

    Scroll down and check Phidget Library 2.1. Click OK.
  • Open the code window. Start by selecting General...Declarations in the small windows. Enter the following code:

Dim WithEvents pir As PhidgetInterfaceKit, flag As Boolean, vidnumber As Integer 

  • Code the Form_Load procedure:

Set pir = New PhidgetInterfaceKit
Call pir.Open

ezVidCap1.MakeUserConfirmCapture = False
ezVidCap1.TimeLimitEnabled = True
ezVidCap1.TimeLimit = 3

vidnumber = 0
flag = True

  • Go to the small windows at the top of the code window. In the left pane, select pir. In the right pane,  select OnSensorChange

  • Code the OnSensorChange procedure with the following:

Select Case Index

Case 0 'plug PIR into port 0

If (SensorValue > 100) And (flag = True) Then

   ezVidCap1.CaptureFile = "c:\vbimages\crittercam" + Str$(vidnumber) + ".avi"

   flag = False
   Timer1.Enabled = True
   vidnumber = vidnumber + 1

End If

End Select

  • Code the Timer1 procedure with:

flag = True
Timer1.Enabled = False

Test the System

Save your project. Before you begin, make sure the PIR is not facing a "warm body". You can mount the PIR at the end of a paper towel or toilet paper tube to limit its sensitivity (use some masking or "scotch" tape). Run the program. Have someone move in front of the PIR. The camera should activate.

Check the files on the hard drive in the folder you set in code (default is vbimages)


Add a text box that turns red when the system is capturing a video and turns green when it is ready to take another!

Notes on the Code

Ezvidcap control:

ezVidCap1.MakeUserConfirmCapture = False
ezVidCap1.TimeLimitEnabled = True
ezVidCap1.TimeLimit = 3

These code lines turn off the ezvidcap dialog where it would normally ask if you want to do a video capture, sets a limit on the video capture time, and sets that time to 3 seconds.

The flag variable: This variable can be given any name. The name flag reminds us it will be used as a signal. Like a rural mailbox where the flag means mail or no mail. In this case the flag variable is set as a Boolean type. That is, it has 2 values, true or false.

If (SensorValue > 100) And (flag = True) Then

The vidcap control will only capture video if both conditions are true; the analog value of the PIR must be over 100 and the flag variable must be true. The first time 'round the flag is true as it was set to this value in the form load procedure. The PIR has a value over 100 when it is triggered by motion.

As soon as these conditions are met, the vidcap control starts sending the video file to the hard drive (or usb drive if you set it up - see below) but it also sets the value of the flag variable to false which prevents the control from trying to send more video files.

ezVidCap1.CaptureFile = "c:\vbimages\crittercam" + Str$(vidnumber) + ".avi"

The first line above sets the name of the video file and where it will be saved. You can change the folder or even the drive by modifying this line. The vidnumber starts at 0 and increments by 1 each time. Therefore with the example above, the first video file would be called crittercam0.avi and would be stored in the vbimages folder. The next file would be crittercam1.avi and so on.

flag = False
Timer1.Enabled = True
vidnumber = vidnumber + 1

Again, the flag variable is set to false to prevent the camera from trying to capture when it is already doing so. Timer1 is enabled. This timer's interval is set to 10000 (10 seconds - check to make sure if you are having issues). After 10 seconds the code in Timer1 is executed.  Thisa sets the flag vaiable back to true which sets the system ready to take another video when the sensor is triggered.



2004 CDLI