Camera Setup
A Raspberry Pi Camera works with any Raspberry Board. There are a lot of very interesting things that can be done with a camera. The Official Raspberry Pi Camera Guide gives lots of examples of how to use the camera.
I plan to use the camera in a Smart Doorbell and security cameras
In addition to the base camera, there are a lot of add-ons.
Parts:
Latest Version of Raspberry Pi Camera V2.1, Amazon, $26.99
Very Annoying, the above says it work with RPi Zero, but only with a separate cable that is not included. There is a v2.1 camera from another vendor for about the same price that comes with both cables
Raspberry Pi running latest version Raspberry Pi OS or MotionEyeOS
UHS Class 10 microSD Card 32GB or larger
Step 1. Connect Camera to Raspberry Pi
The Raspberry Pi Zero needs a 22 to 15 pin ribbon cable to connect to a Raspberry Pi Camera.
With the Raspberry Pi powered off, connect the camera
The Raspberry Pi Camera Module port is shown in the images above
Be careful with the camera connector gray clip it slides out about the width of a fingernail. So, very gently pull on the edges of the port’s gray plastic clip. Do not force it, or it will break. I broke the first one and tried jury rigging the connector with no success. Luckily, I had a spare Raspberry Pi, which a friend gave me.
Insert the camera's ribbon cable into the port. The colored side (blue or black) needs to face the right way. See the images above.
Push the gray plastic clip back into place
Step 3. Setup Camera on Raspberry Pi
Run Raspberry Pi setup and enable the camera interface:
$ sudo raspi-config
Interfacing Options
P1 Camera Enable
Back, Finish and Reboot
When the RPi reboots, log back in and check if the camera is supported (1) and detected (1):
$ sudo vcgencmd get_camera
supported=1 detected=1
If the camera is not detected (0), then check the ribbon cables are properly inserted
Step 4. Install Software on Raspberry Pi
Most of my projects use python.
Install python libraries for the camera on the Raspberry Pi
$ sudo apt-get install python-picamera python3-picamera -y
Step 5. Take a Photo
Take a photo (see raspicam for other commands)
$ raspistill -o ~/photo.jpg
$ ls -l
Open another terminal window and run the following commands, which copies the photo from Raspberry Pi to MacBook
$ cd Desktop
$ scp pi@♣hostname♣.local:/home/pi/photo.jpg .
Double click on photo.jpg on your desktop to view the photo. The photo can use any name.
Play around with the camera settings. jpg files use hardware acceleration. My recommended settings for a doorbell photo on Raspberry Pi Zero (minimize time to take and upload photo while retaining reasonable clarity):
raspistill -md 4 -q 25 -a 12 -e jpg -o photo.jpg
will result in a date and time stamped 1.2MB jpeg file, which takes about 5-7 seconds to write to the microSD Card
Step 6. Performance Considerations
An RPi Zero runs at 1GHz and has 0.5GB of RAM. So, taking videos and photos might be too slow.
GPU RAM can only be either 128 (default) or 256. I didn't notice a significant decrease in time to write a photo to disk with GPU set to 256.
The other obvious check is the speed of the microSD Card. I always use Class 10. Within Class 10, there are different speeds. But, I haven't made these measurements.
Step 7. Skip this step
I found some instructions for using mplayer and netcat, but I could not get the streaming to work properly. I think this step can be skipped.
In trying to get mplayer and netcat running on my MacBook, I did the following. I left these installed.
On MacBook, open a terminal window and install the following:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
$ brew install netcat
$ brew cask install xquartz
$ brew install mplayer
Step 8. Take a Video
Take a 10 second video
$ raspivid -n -ih -t 10000 -w 640 -h 360 -b 1000000 -fps 30 -o video
In a MacBook terminal window run:
$ scp pi@rpiDoorbell.local:/home/pi/video.h264 .
Which copies it to your desktop. Open and play with vlc
My recommended video settings for Raspberry Pi Zero doorbell:
-ih required for playback to a MacBook
-t 0 stream until stopped
-n do not display a preview
-fps 30 is fast enough to not be jittery
-b 1000000 = 1Mbps
-md ??
Step 9. Stream Raspberry Pi Video to MacBook
This is why I love these projects and hate them at the same time. This took me days to figure out. I tried various players on the MacBook and could not get them to stream
Open a terminal window and login to the Raspberry Pi and run the command:
$ raspivid -n -ih -t 0 -w 640 -h 360 -b 1000000 -fps 30 -o - | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8080/}' :demux=h264
If cvlc does not work, then install vlc
$ sudo apt install vlc -y
On a MacBook start VLC:
Click on Open Media
Go to Network tab and enter the following in the url box
rtsp://♣hostname♣.local:8080/
or
rtsp://♣raspberry-pi-ip-address♣:8080/
A window should pop up showing video from the Raspberry Pi's camera
On the Raspberry Pi, type CTRL-c to stop raspivid and the pop up window will close on the MacBook
References:
Camera Setup - show video or pictures on MacBook via terminal window
Official Raspberry Pi Camera Guide , Mag Pi - this is outstanding!