For an index to all my stories click this text
I thought I deserved a present for all the hard work I have done for this weblog ;)
So I bought myself a Raspberry Pi Camera. And boy it is fun to play with !!!
Some background information.
At this moment V2 of the camera is being sold. The difference is mainly that it has a much larger resolution as V1. The first camera had a 5 Megapixel resolution and the V2 version has an 8 Megapixel resolution. That is an enormous increase and yet the price of the camera is very acceptible: around 30 euro. That is including the cable for the Raspberry Pi 3. Mind you the price for the camera is 6 times the price of a Raspberry Pi Zero !!!
That brings me to the Pi Zero.
The camera uses a special cable to connect directly to a camera port which is available on any Raspberry Pi. So it can also be connected to a Raspberry Pi Zero starting from version 1.3. The camera connector is on the right side of the board. However the Pi Zero has (due to its form factor) a smaller connector and therefore you will need to buy a special camera cable.
The camera itself is very small it is about 1 x1 cm. However it is
mounted on a pcb of about 2.5 x 2.5 cm. That's about a square inch. The camera is a mobile phone camera. Nothing wrong with that however there is no zoom option. The lens is fixed.
In the picture above you can see the standard cable and the camera with the Pi Zero cable with the smaller connections.
As stated the camera will work on any Raspberry Pi that has a camera connector and this means on all recent Raspberry's. I am going to use it on the humble Pi-Zero. So all steps that follow are done on a Pi Zero but there is no difference in executing them on a Raspberry Pi2 or Pi3.
Installing the camera
Attaching the camera to the Raspberry is a matter of plugging the cable in. You can't go wrong.
You do have to make sure that the camera is enabled in the Raspbian operating system. There are two ways to do this.
First you can choose "Raspberry Pi Configuration" from the preferences menu and activate the camera.
The second way is to open a terminal window by clicking on its icon (where the red arrow points) and give the following command:
sudo raspi-config
The configuration menu will open and first choose interfacing options.
Next choose Pi camera and activate it.
You now have to reboot the Raspberry Pi. You can do that in the terminal window by giving the following command:
sudo reboot now
Or you can choose Reboot from the "Shutdown menu".
First test
If the cable is attached and the camera is enabled we can test if all is working like it should.
Open the Python 3 Idle from the programming menu.
Type in the following commands and press enter after each command:
import picamera
camera = picamera.PiCamera()
camera.start_preview()
You will immediately see the camera picture displayed on your screen.
Move the camera around and you will see the picture move. And look how fast it is on that humble Pi Zero !!!
This will only work on the Pi itself connected to a monitor. The previeuw function will not work when you are connected to the Pi using VNC or SSH.
To stop just press CTRL-d
The picture you are seeing is full-screen. Therefore it is not easy to access the other windows (like Idle). Fortunaltely you can alter the position, width and hight of the preview screen by instructing the preview command:
camera.start_preview(fullscreen=False, window = (100,150,640,480))
The preview window will open at position 100, 150 and the dimensions will be set to 640 x 480 pixels. This way it will not cover your complete screen.
Taking a picture.
The previous steps just presented you a preview. What we want is to make a real picture. Well that is easy. We just have to alter one line. So open the Python3 Idle again and type in the next commands:
import picamera
camera = picamera.PiCamera()
camera.capture('testpic01.jpg')
You can use the name testpic01.jpg like I did or use any name you like. Just make sure it ends with .jpg otherwise the camera will not be able to save the picture.
Direct after the last command you can find the picture in the /home/pi directory.
Open the directory with the Filemanager and double click on testpic01.jpg
The picture will open with the Image Viewer program. Most likely the picture is something stupid like mine as the camera is just pointing at something random. Mine was pointing at the ceiling of my workroom.
As you can see the picture has a resolution of 1920 x 1080 pixels. That is the default resolution pictures are taken in. You can change that resolution in the maximum with the following command
camera.resolution = (3280, 2464)
Let's test that.
In Idle give the commands like the picture above shows:
camera.resolution = (3280, 2464)
camera.capture('testpic02.jpg')
And as you can see the second picture has the larger resolution of 3280 x 2464 which is 8081920 pixels which is the promised 8Mb. You can clearly see that both pictures are of excellent quality.
The settings
First let us look at the resolution settings.
The table shows the various resolutions and the aspect ratio. As you can see some resolutions make foto's and video's in old style (4:3) being almost square. And some resolutions make the pictures and video's in widescreen (16:9) ratio. The 1920 x 1080 resolution in 16:9 ratio is what in televisionland is called Full HD.
The top resolution of 3280 x 1464 will normally not be used for video purposes as the aspect ratio is 4:3 which will not be displayed well on a Full-HD television. It can however be diplayed on most computer screens without a problem. However be aware that this is memory hungry.....
There are more settings that we can all adress from Python:
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)
Sharpness, contrast, brightness, saturation can be given a value between 0 and 100. ISO can be set to 0 (automatic) or values from 100 to 800.
The camera.image_effect can be any of the following: negative, solarize, sketch, denoise, emboss, oilpaint, hatch, gpen, pastel, watercolor, film, blur, saturation, colorswap, washedout, posterise, colorpoint, colorbalance, cartoon, deinterlace1 and deinterlace2.
If you own a photocam most settings will be familiar to you. Sharpness, contrast and brightness are settings also found on standard camera's just like the ISO value. AWB means automatic white balance and with hflip and vflip we can turn the pictues. Most of these options will work in preview mode to. Play with these commands in Idle to get familiar with them.
Use them as you would use the command for setting the resolution.
Recording video.
Besides being a great photocam with many features the Pi Camera is also a great videocam. So let's try to make a video.
Start with opening a terminal window like described in the beginning of this story.
First make sure we are in the /home/pi directory by typing the following command:
cd ~
Next we make a Python program with the Nano editor with the name recvideo.py by giving the following command:
sudo nano recvideo.py
When the editor opens type in the following program:
import picamera
from time import sleep
camera = picamera.PiCamera()
camera.start_recording('video01.h264')
sleep(5)
camera.stop_recording()
When these lines have been typed in you can stop the editor by typing CTRL-x
Make sure you answer yes when asked if the file need to be saved and check if the right filename is being used.
Now start this program with:
python recvideo.py
After 5 seconds (determined by the sleep command) the program will quit and a file can be found in the /home/pi directory and it will be called video01.h264
You can play this file by using the omxplayer which is standard installed on the Raspberry Pi by typing the following command in the terminal window:
omxplayer video01.h264
The video will start to play after a short pause.
You can alter this program easily. Alter the sleep(5) command in a longer or shorter time period, and alter the name of the video each time for saving several video's.
More details and information.
The above described steps will lead the way for devellopping your own projects with the Pi Camera. However there is detailed information available on the website from the Raspberry Pi Foundation:
https://www.raspberrypi.org/documentation/usage/camera/python/README.md
And more in depth information can be found on:
http://picamera.readthedocs.io/en/release-1.13/install.html#alternate-distro-installation
Now let's build a real camera
In a follow up story I am going to build a camera that just uses a few buttons to control it and does not need a screen, mouse and keyboard just like my Internet Radio.
In the mean time use your imagination: add a PIR (https://lucstechblog.blogspot.nl/2017/01/pir-basics-movement-detection.html) so you can make photo's of animals feeding or someone entering your room/home, make photo's at intervals of minutes-hours-days so you can watch plants grow, or add a rain/temperature/whatever sensor to make photo's of certain events. The possibillities are endless.
Till then.
Have Fun
Luc Volders