tag:blogger.com,1999:blog-39205895289419518572024-03-14T01:56:07.304-07:00Musings of AdrianThis blog will detail the trials, tribulations & sometimes even successes of projects that I find of interest.<br>I hope they may be of use to someone other than myself.A.J.Bevanhttp://www.blogger.com/profile/13464543123006295316noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-3920589528941951857.post-83800979374889573292013-04-08T09:04:00.001-07:002013-04-12T03:53:03.797-07:00Raspberry Pi Controlled Canon DSLR<div class="separator" style="clear: both; text-align: left;">
<span style="background-color: clear; font-size: 17px;">As I work from home, overlooking the garden, I decided to build myself a bird table to have something interesting to look at whilst working out whatever network problem has fallen into my lap that day.</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="background-color: clear;"><br /></span></div>
<span style="background-color: clear; font-size: 17px;">I am by no means a carpenter but I think it turned out OK, and the birds seem to like it.</span><br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="http://i.imgur.com/xcs8McN.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="http://i.imgur.com/xcs8McN.jpg" height="320" width="180" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Click for Larger Version</td></tr>
</tbody></table>
<span style="background-color: clear;"><br /></span>
<span style="background-color: clear; font-size: 17px;">Next it was time to take some pictures of the visitors I had, this turned out to be very difficult with only a 250mm zoom lens. As soon as you moved the little blighters flew off! plus I had work to do, couldn't do both at the same time.</span><br />
<span style="background-color: clear;"><br /></span>
<span style="background-color: clear; font-size: 17px;">Having made a DIY shutter release for my Canon1000D camera the obvious next step was to build a remote release and the device most suited to this task was of course my RaspberryPi.....</span><br />
<span style="background-color: clear; font-size: 17px;"><br /></span>
<span style="background-color: clear; font-size: 17px;">This post will not cover setting up and updating your RPi, there are plenty of those already, suffice to say, keep your system updated.</span><br />
<span style="background-color: clear; font-size: 17px;">There is also a myriad of websites detailing the physical differences between camera shutter release connections, please check your own camera/connectors. Although each one will have at least 3 pins to connect to, these are usually pre-focus, shutter & ground. You'll need to know which is which.</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">So, let's get to it.....</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">Step1 : The Circuit</span><br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="http://i.imgur.com/cBd2bsX.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="http://i.imgur.com/cBd2bsX.jpg" height="180" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Click for Larger Version</td></tr>
</tbody></table>
<br />
<span style="background-color: clear; font-size: 17px;">The circuit comprises of two transistors which replace the physical buttons used on a standard handheld shutter release, a transistor is a switch after all - just one controlled by electricity instead.</span><br />
<span style="background-color: clear; font-size: 17px;">The transistors I used were "2N3440", I decided quite arbitrarily on these (Lots of Googling shows these are quite 'beefy' so not much chance of any power from the camera getting to my RPi) - I'm not an electronics person either, although I do have a soldering iron !</span><br />
<span style="background-color: clear; font-size: 17px;"><br /></span>
<span style="background-color: clear; font-size: 17px;"><br /></span>
<span style="background-color: clear; font-size: 17px;">The basic logic is that each transistor has 3 'legs' Base, Collector & Emitter, they usually have a flat edge or a bumb to show it's orientation - again, google it.</span><br />
<span style="background-color: clear; font-size: 17px;">We apply current to the 'Base' from the RPi GPIO pin (via software which we'll get to later), which throws the transistor 'switch', the 'Emitter' is ground and the 'Collector' is connected to the respective pin in the 2.5mm jack.</span><br />
<span style="background-color: clear; font-size: 17px;">You can see all of the 'grounds' are connected together.</span><br />
<span style="background-color: clear; font-size: 17px;">The pictures below show the finished board, and with it connected to the RPi. The 3 grey connector wires, with respect to the diagram above, from top to bottom, GPIO Pin#6(Green), #16(Red) & #18(Yellow)</span><br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="http://i.imgur.com/fKfLVVs.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="http://i.imgur.com/fKfLVVs.jpg" height="280" width="280" /></a></td>
<td><a href="http://i.imgur.com/hVxICa1.jpg" imageanchor="1" style="margin-left: 1em margin-right: 1em;"><img a="" border="0" src="http://i.imgur.com/hVxICa1.jpg" height="280" width="280" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Click for Larger Version</td><td class="tr-caption" style="text-align: center;">Click for Larger Version</td></tr>
</tbody></table>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<span style="background-color: clear; font-size: 17px;">That covers the hardware side of things, next is the software..</span>
<br />
<span style="background-color: clear; font-size: 17px;"></span><br />
<a name='more'></a><span style="background-color: clear; font-size: 17px;">The software side of this project is surprisingly simple.</span><br />
<span style="background-color: clear; font-size: 17px;">I used a Youtube video to understand how the GPIO is used and what software you need to install in order to write your own routines.</span><br />
<span style="background-color: clear; font-size: 17px;">I'm not going to re-invent the wheel here so I suggest you watch it too : <a href="http://www.youtube.com/watch?v=q_NvDTZIaS4" target="_blank">GPIO For Beginners</a></span><br />
<br />
<span style="background-color: clear; font-size: 17px;">OK, so now you have the software installed and understand a bit more about GPIO.</span><br />
<span style="background-color: clear; font-size: 17px;">To fire the camera shutter all we need to do is set the correct pin to 'high' for a specified amount of time and then bring it back down to 'low'</span><br />
<span style="background-color: clear; font-size: 17px;">Below is the short script that I used, actually fires the camera for 4 seconds - you can of course change this to whatever you want. I have setup pin 23 as well (shutter half press), although I'm not using it in this script at the moment.</span><br />
<span style="background-color: clear; font-size: 17px;">The code (shutter.py):</span><br />
<br />
<span style="font-family: Courier New, Courier, monospace;">import RPi.GPIO as GPIO</span><br />
<span style="font-family: Courier New, Courier, monospace;">import time</span><br />
<span style="font-family: Courier New, Courier, monospace;">GPIO.setmode(GPIO.BCM)</span><br />
<span style="font-family: Courier New, Courier, monospace;">GPIO.setup(23, GPIO.OUT)</span><br />
<span style="font-family: Courier New, Courier, monospace;">GPIO.setup(24, GPIO.OUT)</span><br />
<span style="font-family: Courier New, Courier, monospace;">GPIO.output(24, True)</span><br />
<span style="font-family: Courier New, Courier, monospace;">time.sleep(04) </span><br />
<span style="font-family: Courier New, Courier, monospace;">GPIO.output(24, False)</span><br />
<span style="font-family: Courier New, Courier, monospace;">print "Done..."</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">Make it executable :</span><br />
<br />
<span style="font-family: Courier New, Courier, monospace;">sudo chmod +X shutter.py</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">To actually run the code :</span><br />
<br />
<span style="font-family: Courier New, Courier, monospace;">sudo python shutter.py</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">So you can see that by setting the pin number to 'True' we are in fact delivering current from the RPi GPIO pin to the 'Base' of the transistor which then fires the camera, then switch it off by setting the pin to 'False'.</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">This short video shows the whole setup working, this was my first YouTube video and the quality isn't great but it gives you the idea : <a href="http://www.youtube.com/watch?v=n72l6Vgf5Ek" target="_blank">RPi GPIO Controlled Canon DSLR</a></span><br />
<br />
<span style="background-color: clear; font-size: 17px;">Next step is make the whole setup battery powered!!</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">Again, I'm not going to re-invent the wheel here, this site is where I got all the information needed : <br /> <a href="http://www.raspberrypi-spy.co.uk/2013/02/running-a-raspberry-pi-from-6-aa-batteries/" target="_blank">RPi Running From Batteries</a></span><br />
<span style="background-color: clear; font-size: 17px;">The only changes I made were getting a 10xAA Battery box and connecting a salvaged micro USB instead of using the GPIO header to connect the power. So far I have had my setup running for about 5 hours on 10xAAs. In the end you then get :</span><br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="http://i.imgur.com/slRm6ZU.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="http://i.imgur.com/slRm6ZU.jpg" height="280" width="380" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Click for Larger Version</td></tr>
</tbody></table>
<span style="background-color: clear; font-size: 17px;">The webcam is used in the next part, detecting motion then activating the camera shutter!</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">We now have an isolated platform (associated with my wireless network) which can be placed in the field. </span><br />
<span style="background-color: clear; font-size: 17px;">Set up the tripod, camera etc approx 8-10 feet from the bird table, connect the 2.5mm jack to the remote shutter release port of the camera, SSH to RPi from the comfort of the conservatory and wait.....sometimes for a while.....until something lands on the table. When it is roughly in the area you have setup the camera for then run the script that fires the shutter, and repeat until your satisfied you have enough material to work through. When your finished you can get some good pictures.</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">Selection of pictures taken with this setup can be found here : <a href="http://www.flickr.com//photos/ajbevan/sets/72157633180591610/show/" target="_blank">Recent Blackbird Pictures</a></span><br />
<br />
<span style="background-color: clear; font-size: 17px;">Next we want to fire the camera only when motion has been detected, so we don't have to sit there waiting, ie: automate the process.</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">Software we need for motion detection is called 'Motion', so first of all we need to install it, which is as simple as : </span><br />
<br />
<span style="font-family: Courier New, Courier, monospace;">sudo apt-get install motion</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">Motion has its' own configuration file, 'motion.conf', we need to edit that to make a few changes, so....</span><br />
<br />
<span style="font-family: Courier New, Courier, monospace;">sudo nano /etc/motion/motion.conf</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">Motion.conf is a long file, lines starting with a "#" are comment lines and are ignored. Listed next are the parameters I changed : </span><br />
<br />
<span style="font-family: Courier New, Courier, monospace;"># Image width (pixels). Valid range: Camera dependent, default: 352</span><br />
<span style="font-family: Courier New, Courier, monospace;">width 320</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"># Image height (pixels). Valid range: Camera dependent, default: 288</span><br />
<span style="font-family: Courier New, Courier, monospace;">height 240</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"># Maximum number of frames to be captured per second.</span><br />
<span style="font-family: Courier New, Courier, monospace;"># Valid range: 2-100. Default: 100 (almost no limit).</span><br />
<span style="font-family: Courier New, Courier, monospace;">framerate 50</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"># Image File Output</span><br />
<span style="font-family: Courier New, Courier, monospace;">output_normal off</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"># Live Webcam Server</span><br />
<span style="font-family: Courier New, Courier, monospace;">webcam_port 8081</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"># Restrict webcam connections to localhost only (default: on)</span><br />
<span style="font-family: Courier New, Courier, monospace;">webcam_localhost off</span><br />
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"># Command to be executed when an event starts. (default: none)</span><br />
<span style="font-family: Courier New, Courier, monospace;"># An event starts at first motion detected after a period of no motion defined</span><br />
<span style="font-family: Courier New, Courier, monospace;">on_event_start sudo python shutter.py</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">So when we start 'motion' you can use your favorite browser to go to your RPi's IP address and port 8081 eg: http://192.168.1.50:8081 to see what the webcam can see.
Once motion is detected then the script shutter.py is called and fires the camera - that's it !!</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">The short video below shows the setup working, this was before the battery pack was completed:<br /> <a href="http://www.youtube.com/watch?v=10OXagkNRWI" target="_blank">RPi Controlled Canon DSLR Motion Detect</a></span><br />
<br />
<span style="background-color: clear; font-size: 17px;">This does work 'in the field' but is a bit restrictive by the length of USB cable from the webcam to the RPi. To overcome this we need the webcam independent of our RPi running motion.</span><br />
<span style="background-color: clear; font-size: 17px;">We'll use another program called 'mpeg-streamer', details and instructions of which are found here :<br /> <a href="http://www.slblabs.com/2012/09/26/rpi-webcam-stream/" target="_blank">RPi-Webcam_Streaming</a></span><br />
<br />
<span style="background-color: clear; font-size: 17px;">There is information regarding other programs in there but we are interested only in mpeg-streamer for this.</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">All we do then is edit motion.conf again and add the line </span><br />
<br />
<span style="font-family: Courier New, Courier, monospace;">netcam_url http://192.168.1.64:8080/?action=stream</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">In the NetCam section of the file, amending the IP address according to whatever your second RPi's address is. <br />I have had this working on the bench but no video available.</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">So that's it, you should now be able to get 'up close & personal' with whatever you want to get pictures of, without having massive lens or sitting around getting bored, hope it's useful.</span><br />
<br />
<span style="background-color: clear; font-size: 17px;">Thanks go to people who I have linked to, it's thanks to their efforts that this work has been possible.</span><br />
<span style="background-color: clear; font-size: 17px;">I'd also like to add my personal gratitude to Adam Kovacs for supporting me in this piece of work - Thanks Adam - Top Man !! </span>A.J.Bevanhttp://www.blogger.com/profile/13464543123006295316noreply@blogger.com29United Kingdom55.378051 -3.4359729999999912.2172075 -86.05316049999999 90 79.18121450000001