You are on page 1of 4

Raspberry Pi Camera

Inthislab,youwilllearnhowtouseacamerawiththeRaspberryPicomputer.
Note:Thecameracanbedamagedbystaticelectricity.Beforeremovingthecamerafromitsgreyantistatic
bag,pleasemakesureyouhavedischargedyourselfbytouchinganearthedobject(e.g.aradiatororwater
tap).

TheRaspberryPicameraisa5MPstillcamerathatiscapableofrecording1080p30or720p60video.The
camera has a fixedfocus lens, suitable for most purposes, but focusing on very close objects is not
possible.
Connection: The Raspberry Pi camera is connected to the camera connector, located adjacent to the
Ethernetport.PullthetabsonthetopoftheconnectorupwardsandthentowardstheEthernetport.
Insertthecameraribbon,withtheexposedmetalcontactsfacingawayfromtheEthernetport.Pushthe
toppartoftheconnectortowardstheHDMIconnectoranddown,whiletheflexcableisheldinplace.If
exists,removeasmallpieceoftranslucentblueplasticfilmcoveringthelens.
Setup:Beforeimagescanbetaken,youmustenablethecameraontheRaspberryPi.Openaterminal
andtypethecommand

sudoraspiconfig

SelectEnableCameraandpressFinish.Youmustrebootinorderforthechangestotakeeffect.
StillImages:Inordertotakeapicture,usethecommandraspistill:
raspistilloimage.jpg
Thecamerawilltakeaphotoanditwillbesavedinyourworkingdirectory.
ooroutputspecifiestheoutputfilenameandtortimeoutspecifiestheamountoftimethatthe
previewwillbedisplayedinmilliseconds.Notethatthissetto5sbydefaultandthatraspistillwill
capturethefinalframeofthepreviewperiod.

dordemorunsthedemomodethatwillcyclethroughthevariousimageeffectsthatareavailable.
Ifthecameraistakingupsidedownphotosanditisinconvenienttorotatethephysicalcamera,usethe
command
raspistillvfhfocam.jpg
tofliptheimageswhentheyaretaken.
Totakethephotowiththefilenamedwiththedateandtime,use

raspistillodate+%Y%m%d_%H%N.jpg

Video:Torecordvideo,typethecommand:
raspividovideo.h264t10000
Thecamerawilltakea10000millisecond(10seconds)videoanditwillbesavedtovideo.h264inthe
workingdirectory.
Toseealistofpossibleoptionsforrunningraspividorraspistill,youcanrun:
raspivid|less
raspistill|less
Usethearrowkeystoscrollandtypeqtoexit.
Python:
Installation:
sudoaptgetupdate
sudoaptgetinstallpythonpicamera
Usage:
First,atthePythonpromptoratthetopofaPythonscript,enter:
importpicamera
Thiswillmakethelibraryavailabletothescript.NowcreateaninstanceofthePiCameraclass:
camera=picamera.PiCamera()
Andtakeapicture:
camera.capture('image.jpg')
Record5secondsofvideo:
camera.start_recording('video.h264')
sleep(5)
camera.stop_recording()
Writeaprogramtotakeastillpicturewhenyoupressthepushbutton.
Arrangeapreview(seeAppendixbelow)anduseGPIO.add_event_detect()tomakeathreadedcallback.

Appendix:PythonCommands
Horizontalandverticalflip:
Likewiththeraspistillcommand,youcanapplyahorizontalandverticalflipifyourcameraispositioned
upsidedown.Thisisdonebychangingthehflipandvflippropertiesdirectly:
camera.hflip=True
camera.vflip=True
Preview:
Youcandisplayapreviewshowingthecamerafeedonscreen.Warning:thiswilloverlayyourPython
session by default; if you have trouble stopping the preview, simply pressing Ctrl+D to terminate the
Pythonsessionisusuallyenoughtorestorethedisplay:
camera.start_preview()
Youcanusethestoppreviewmethodtoremovethepreviewoverlayandrestorethedisplay:
camera.stop_preview()
CameraSettings:
Youcanchangeothercameraconfigurationbyeditingpropertyvalues,forexample:
camera.brightness=70
Thiswillchangethebrightnesssettingfromitsdefault50to70(valuesbetween0and100).
Othersettingsareavailable.Hereisalistwiththeirdefaultvalues:
camera.sharpness=0
camera.contrast=0
camera.brightness=50
camera.saturation=0
camera.ISO=0
camera.video_stabilization=False
camera.exposure_compensation=0
camera.exposure_mode='auto'
camera.meter_mode='average'
camera.awb_mode='auto'
camera.image_effect='none'
camera.color_effects=None
camera.rotation=0
camera.hflip=False
camera.vflip=False
camera.crop=(0.0,0.0,1.0,1.0)
Sleep:
Youcanaddpausesbetweencommandsusingsleepfromthetimemodule:
importpicamera

fromtimeimportsleep
camera=picamera.PiCamera()
camera.capture('image1.jpg')
sleep(5)
camera.capture('image2.jpg')
Youcanalsousesleepinapreviewtoadjustsettingsovertime:
camera.start_preview()
foriinrange(100):

camera.brightness=i
sleep(0.2)
Documentation:
Fulldocumentationforpythonpicameraisavailableat:
http://picamera.readthedocs.org/en/release1.9/

You might also like