The latest version of the Raspberry Pi has finally arrived! This is the Raspberry Pi Zero V1.3. The difference between this model and the basic Raspberry Pi Zero is that the Raspberry Pi camera module can be connected to this one (as shown in the picture above). In this article, we’re going to explore how to use Python to control the Raspberry Pi Zero V1.3 and the camera module.
We’re also going to add some automation to it and make a fixed point Raspberry Pi camera!
As the name suggests, the Raspberry Pi Zero V1.3 is a more advanced version of the Raspberry Pi Zero. The only visible difference from the older version is the Camera Serial Interface (CSI) connector.
Figure 1
On the right side, you can see there is a connector mounted for use with the camera module. You insert the ribbon cable from the camera module here. On the Model B series, you had to insert it from above. However, you insert from the right side on the Zero V1.3.
Figure 2
Zero grows a camera connector – Raspberry Pi
To connect the camera to the Zero, we offer a custom six-inch adapter cable. This converts from the fine-pitch connector format to the coarser pitch used by the camera board.
Like with the HDMI and USB ports, this is a miniaturized connector for a camera module!
Compared to the standard sized connector on the Model B series, this one is somewhat smaller and uses a custom 6-inch cable as noted in the above Raspberry Pi blog post.
Figure 3
Camera Module – Raspberry Pi
The camera used in this article is v1.3. It’s slightly older than the current v2.0, which was released earlier this year.
So, when I tried to connect the camera cable to the Pi…
Figure 4
…the cable wouldn’t fit!
Like I said before, the size of the connector is different and I wasn’t aware of this until I actually tried it myself.
I thought maybe I had to buy a camera specifically for the Raspberry Pi Zero (i.e. v2.0) but it turned out I didn’t need to.
Figure 5
I discovered that I couldn’t detach the camera cable from the camera! So, I was provided with this cable (Figure 5).
Would the camera work just by switching out the cable?
Figure 6
When I looked closely at the back of the camera, I realized that it used the same connector as is mounted on a Raspberry Pi. So, when I removed the black stopper part, I was able to disconnect the cable.
After all that, I tried connecting up the new cable…
Figure 7
And it worked! Now we’re done setting things up so let’s take some pictures!
Download Raspbian for Raspberry Pi
For this article, I’m using the 5/27/2016 release of Raspbian.
Like always, I first updated Raspbian to the latest version. In doing so, the system froze several times… it seems like using the desktop and the camera at the same time was too much of a load on the system.
So, I decided to try “Raspbian Jessie Lite.”
For detailed installation instructions, check out this thread on the official Raspberry Pi forums. Below, we’re going to quickly run through the process from installing Raspbian Jessie Lite to setting up the NAS.
Select “B2 Console Autologin” on the “3. Boot Options” screen.
sudo raspi-config
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Add the following code to the /etc/wpa_supplicant/wpa_supplicant.conf file
1 2 3 4 |
network={ ssid="SSID" psk="password" } |
Installing Samba
sudo apt-get update
sudo apt-get install -y samba
sudo mkdir -m 777 /home/pi/nas/
sudo nano /etc/samba/smb.conf
Add the following code to the /etc/samba/smb.conf file
1 2 3 4 |
[nas] path = /home/pi/nas/ guest ok = Yes read only = No |
None of these items are actually required to use the camera. I’m using the command line user interface, so this is my preferred setup.
Since we’re going to do some programming, I set up the NAS so we can easily move files between the PC and the Pi.
If the samba install doesn’t work, you may need to expand the partition to resolve this using the raspi-config tool.
Next, let’s setup the camera.
There is a documentation on the official Raspberry Pi site regarding the camera module. We’ll refer to the documentation as we proceed.
Camera Module – Raspberry Pi Documentation
sudo raspi-config
Figure 8
Select “6. Enable Camera.”
Figure 9
You’ll be asked whether you want to enable the camera, and then select “Yes”.
Figure 10
Now, select “Yes” when it asks if you want to reboot. After reboot, we can use the camera!
According to the official Raspberry Pi site documentation, the two options available for using the camera are shell scripts and Python. Since we’ve been learning Python, we’ll use that as we work with the camera.
Python picamera – Raspberry Pi Documentation
First, install the “python-picamera” package.
sudo apt-get install python-picamera
You can find picamera documentation via the above link. There’re also plenty of sample codes for us to test!
/home/pi/nas/test.py
1 2 3 |
import picamera camera = picamera.PiCamera() camera.capture('nas/image.jpg') |
Using just 3 lines of code from the documentation, I was able to take a picture!
The LED on the module lights up red when taking a picture.
Due to the way the camera module was positioned when taking the picture…
…I got a nice shot of the pipes in the ceiling.
By default, the image files are saved in the directory where the program is run (in /home/pi/ if logged in as the “pi” user). I want to have them saved to the NAS so I passed the argument “nas/image.jpg” in the “camera.capture” function.
It seems like the default image resolution varies depending on what display the Pi is connected to. In my case, when a display was connected, the image resolution was 1980 px x1080 px. When it was not connected to any display, the resolution was 720×480 px. Since the image resolution can be set by parameters in picamera, I would recommend defining the resolution so it’s not affected by your system configuration.
In addition to taking pictures, picamera can also display a preview of what the camera is seeing in real time. In order to set the start and end, we need to use the “time” library.
Quick Start — Picamera 1.10 documentation
1 2 3 4 5 6 7 8 9 10 |
import time import picamera camera = picamera.PiCamera() try: camera.start_preview() time.sleep(10) camera.stop_preview() finally: camera.close() |
The preview can only be displayed on a display directly connected to the Raspberry Pi. You won’t be able to see it on a PC if connecting via SSH. It seems like a waste of the small form factor if you have to connect to a display…but maybe it’s not needed with the Raspberry Pi Zero anyway.In the above code, the preview is displayed for 10 seconds. Since this is Raspbian Jessie Lite’s command-line interface, the preview can be displayed very precisely.
1 2 3 4 5 |
import picamera camera = picamera.PiCamera() camera.start_recording('nas/video.h264') camera.wait_recording(10) camera.stop_recording() |
Lastly, let’s look at video.
You can set the extension on the file name to either .h264 or mjpeg. If you use another file format that’s not supported, it might freeze and stop working.
The “time.sleep()” function is used in the sample code on the official Raspberry Pi site. However, the explanation in the picamera documentation recommends using the “picamera.wait_recording()” function instead.
For this next step, we’re going to make use of our old friend cron to schedule a program that takes and saves pictures to run automatically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/home/pi/nas/camera.py import picamera import datetime import os now = datetime.datetime.now() dir_name = now.strftime('%Y%m%d') dir_path = '/home/pi/nas/'+dir_name file_name = now.strftime('%H%M%S') if not os.path.exists(dir_path): os.makedirs(dir_path) os.chmod(dir_path, 0777) picamera = picamera.PiCamera() picamera.capture(dir_path+'/'+file_name+'.jpg') |
This program takes a picture every 10 minutes and saves it using the timestamp (when the picture was taken) as the file name. It also creates a new directory each day and saves all the pictures taken that day to that folder. I set the save location as the NAS directory so that pictures can be accessed from a PC.
Next, register the program with cron so it can be run automatically.
sudo crontab -e
Add this line to the bottom of the file.
0-59/10 * * * * python /home/pi/nas/camera.py
If you see new files appearing every 10 minutes in the folder, that means it’s working! If the image files don’t have the correct time, check the timezone setting in the “raspi-config” tool.
With that step, we’ve made a fixed point Raspberry Pi camera! In thinking about this program running for a long period of time, it seems like it would be better for the Pi’s power source to be an electrical outlet rather than a battery. A fun application for something like this would be to document the growth of a plant over time or anything that grows/changes little by little over time.
The Raspberry Pi Zero doesn’t have GPIO pins mounted and requires significant effort to connect buttons and sensors. The more components you add, the bigger it gets and the more power it consumes. If you add a bunch of stuff to it, the value of the Zero drops in comparison to other Pis. For this reason, I recommend using it for applications like the fixed point Raspberry Pi camera we built in this article.