Electronics · PHP-AJAX

The DIY Surveillance System Using a Webcam – Part 1

Part-1:  Hardware

The Story: Its been a while since my last post due to some work projects which have been occupying a lot of time and effort. However I still managed to find time for a hobby project, a DIY surveillance system using a webcam so I thought I’d go and share it with the rest. It’s not really a hobby project the real motivation behind the project are some of the cleaners which mess up some of the wirings of some work related project I am involved in. So you’d leave everything okay and when you come back in the morning something would either be unplugged or just not working. I’d  to spend a lot of time probing on the scope of what’s wrong and most important where! with so many wires it’s a pain.  So I decided to go ahead with building a surveillance system for myself using a webcam and some other hardware which I will mention in a while (I don’t want to go in detail of why not buy one off the shelf so don’t ask). The project however did help me learn about some valuable electronics concepts and it wasn’t all for fun. Anyway without further delay id just get on with it.

Id cover this project in three parts:

1- The hardware

2- The comms (communication with the hardware)

3- The software (web based control including streaming)

The second one is also related to software but since its more about channeling communication down to the hardware I decided to keep it separate.  The flow of this entire process is summarized in the diagram below.

I would start off with the hardware part which is crucial as if that doesn’t work, there is no point to the rest of it really. The hardware that is needed for the projects include:

1- Webcam

2- Stepper Motor (4 wired  = 2 Coils for a clockwise and an 2 for anticlockwise rotation)- http://www.sparkfun.com/commerce/product_info.php?products_id=9238

3- Motor Driver Board (Controlling the motor why this is needed is discussed below)-http://www.sparkfun.com/commerce/product_info.php?products_id=9402

4- Arduino Board (any would do I’m using which has the ATmega168 on it. Description below)

Webcam is pretty obvious since its a surveillance system so cant do much without the video I guess. The webcam I am using is:

http://www.flickr.com/photos/45253805@N07/4156146981/

The stepper motor was a bit tricky to decide on compared with servos. But to summarize it down for those who don’t know much about it is that servos operate on a closed loop and perform a feedback mechanism to the microcontroller to adjust position errors in other words they aren’t that precise. So say I want the camera to be at a 45 degree angle id have to rely on the feedback from the motor to first decide where it is and then move on to adjust the position. Stepper motors are however are relatively cheap and provide precision control by operating in steps. A complete discussion on servos vs steppers is discussed here and is worth having a look at http://www.torchmate.com/motors/electronics_selection.htm. The motor for this project can be viewed at the link above and is a bipolar motor. The stepper motor: (http://www.flickr.com/photos/45253805@N07/4156919912/)

The stepper driver is the A3967SLB and can be viewed at the link above which is used to control motor movement and provide precision control based on the input pulses provided to it. It can be operated in full-, half-, quarter-, and eighth-step modes. For people who still don’t understand just consider it as way to translate the high/low pulses into something meaningful for the motor to move a step a very small if it’s just 1 pulse. The driver(http://www.flickr.com/photos/45253805@N07/4156920162/)

The last but not the least is the arduino board which has the Atmega168 microcontroller on it. This is needed as it talks to the stepper driver which in turn controls the movement on the motor.

Connecting it all up:

1- Camera to the stepper motor

Ok so now we know the basic components of what are going to use so lets get on with the connections and how all of this will work. The first thing was to cut the camera (you can see the image at the flickr link mentioned above) and make it fit on the motor. Now this is a very crucial yet very important procedure so make sure you don’t damage the camera. I cannot exactly go about telling how to cut it up but here are some of the snaps of how the camera is mounted on the motor:

1- http://www.flickr.com/photos/45253805@N07/4156920424/in/photostream/ – Side view

2- http://www.flickr.com/photos/45253805@N07/4156158481/in/photostream/ – Back view

Once this is done plug the camera within the USB port to make sure its working!!! Else you’ve done something wrong or damaged the camera J.

2-  The Stepper Driver – Motor Connection

The simplified diagram of the stepper is as follows, it only includes only the pins I am using

The 5v and the Gnd are to run the driver board. Instead of using a power supply for this what I did was use the pins on the arduino to get the 5v directly. This powers the driver.  The PWR is the power to the motor and can be from 12v to 30v. Im using 12v to power it from an external power supply. The C-A and C-B are the coils from the stepper motor in other words the 4 wires from the stepper motor would go in here. If you are using a motor different than the one mentioned above please read the data sheet to ensure that the correct wires are placed. In total there are 4 coils within the motor 2 to ensure a clockwise rotation and 2 to ensure an anticlockwise rotation. If these wires are attached in a wrong order the clockwise and the anticlockwise motion might cancel each other out and the motor might not move. The connections for the motor above are as follows:

Red and Green are for Coil A – C-A

Yellow and Blue are for Coil B – C-B

So now you would have the motor connected to the stepper driver board.

3- Stepper Driver- Arduino Connections

Ive connected the dir pin which controls the direction of the motor to Pin 4 on the arduino and the stepperPin which controls the amount of steps the motor should take is connected to Pin 3.The sleep pin is connected to Pin 7

My connections: http://www.flickr.com/photos/45253805@N07/4161441034/

Why connect the sleep pin?

Ok this is important because it allows the coil which holds the shaft in its position, to demagnetise. What I mean is if you don’t use the sleep pin the motor still functions but if you try to move it with your hand you wouldn’t be able to since the coils are magnetised and they hold the shaft firmly. Since the coils are magnetised this causes the motor to heat up and I didn’t want that to happen if the motor is not in operation else it would melt the glue that is holding the camera.

Test Software:

Now to ensure everything is alright and the motor is working upload the following test code which is a basic routine which comes with the driver to see if everything is working or not. I’ve added a bit to control the sleep pin you may wish to remove this to observe what happens without the sleep pin. This code is written with the arduino programming language which is based on c/c++

int dirPin  = 4;
int stepperPin= 3;
int sleepPin=7;
void setup () {
pinMode (dirPin, OUTPUT);
pinMode (stepperPin, OUTPUT);
pinMode(sleepPin,OUTPUT);
}

void step(boolean dir,int steps){
digitalWrite(sleepPin,HIGH);
digitalWrite(dirPin,dir);
delay(50);
for(int i=0;i<steps;i++){
digitalWrite(stepperPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepperPin, LOW);
delayMicroseconds(100);

}
digitalWrite(sleepPin,LOW);
}
void loop(){
step(true,15);
delay(500);
step(false,15);
delay(500);
}

The code is self explanatory, there is a simple step function which allows a direction to be input 0 for clockwise and 1 for anti-clockwise. The steps are the number of movements which the motor should take. The duration of the 1 step is determined by the delayMicroseconds in the functions which determines how long to hold the pulse high and in turning causing the motor to turn for that time period.  The main function or the loop simply calls this function once to make it turn clockwise and then anticlockwise. If this works as expected and the motor is moving the hardware step to the project is complete. We are now ready to create the front end software, the arduino would then be revisited once the front end software is complete.

Please note that the above code is not the final version of the code to go on the microcontroller and would be revisited.

26 thoughts on “The DIY Surveillance System Using a Webcam – Part 1

  1. hi.. this is pretty neat.

    do you happen to know if the stepper driver needs the 5v line hooked up,and would not doing so cause strange problems even though the regulator is onboard?
    am trying to get my laser etcher (using old scanner assembly) to work
    thanks, A

    1. As far as i know it does need the 5v to work i take the 5v from the arduino board pins so i think it does need 5v 🙂 ill confirm and let u know

      1. The EasyDriver (which I designed) does NOT need its 5V line hooked up in your system. That 5V pin is an output, to supply power to other parts of the system if need be. Your Arduino has its own power supply, so you don’t need the 5V output from the ED. The ED takes it’s power input (7-30V) and has an on-board voltage regulator to generate it’s own 5V supply. I added that 5V output pin in case people wanted to power other things in their system with the 5V.

      2. Ooops. Didn’t remember that I posted this same information below. Ignore the above. Sorry!

        *Brian

      3. Hi Brian I already commented and fixed the problem for the readers thanks for the update though its appreciated.

  2. The EasyDriver you got from SparkFun (which I designed) does NOT need 5V. In fact, that pin is to supply 5V to the outside world, should you need it. There is a 5V regulator on the EasyDriver that generates the logic level supply voltage for the driver chip. So leave that disconnected.

  3. Have you heard of any issues with these motors? I have hooked up the EasyDriver v4.2 with the same connections you made and am unable to get the motor to do more than vibrate when I start sending steps to it.

    1. Not as yet John but there is a mistake in my connections which is that the 5V is an output 5V rather than requring a 5V connection. At the start I did get a problem that the motor was just rotating in this case I suggest to check the coil connections -> motor to the driver board which might cause the movements (clock/anti) to cancel out… let me know how it goes

  4. HI. I’m trying to do a similar project… Could you help me out? I’m using a PIR sensor to sense a motion and the arduino will trigger a webcam and sent the pics online to a remote location.. this is what i have in mind….

    1. Hi, Okay for this im assuming you are going to create a desktop app…. and you would need some kind of video capture sdk…. look into directshow and the frame grabber. The arduino would poll the sensor if motion was detected. You can have a desktop app poll the serial for data. Once the motion is detected the arduino can send some data through the serial telling the app that motion has been detected …. now the camera can capture the snap using the directshow frame grabber (google it you will find lots of example). Once you have the snapshot upload it via FTP …. kind of simple…. so where do you need the help exactly? 🙂

  5. Thanks for the info! It got me set up and running in no time. I needed to change the digitalWrite(sleepPin,HIGH); to digitalWrite(sleepPin,LOW); in the beginning of the step function and the digitalWrite(sleepPin,LOW); to digitalWrite(sleepPin,HIGH); at the end of the function for the code to work.

    Thanks again!

  6. Very useful page – thank you very much for making the information so accessible and useful.

    I appreciate that you have made the effort made!

    Best regards
    Roy

Leave a comment