Bird
0
0
Raspberry Piprogramming~10 mins

Raspberry Pi Camera setup

Choose your learning style9 modes available
Introduction

The Raspberry Pi Camera lets you take pictures and videos with your Raspberry Pi. Setting it up correctly helps you start capturing images easily.

You want to take photos or record videos with your Raspberry Pi.
You are building a security camera or baby monitor project.
You want to do time-lapse photography or motion detection.
You need to capture images for a robotics or AI project.
You want to stream live video from your Raspberry Pi.
Syntax
Raspberry Pi
1. Connect the camera module to the Raspberry Pi's camera port.
2. Enable the camera interface in Raspberry Pi settings.
3. Use commands or Python code to capture images or videos.

Make sure the Raspberry Pi is powered off when connecting the camera.

Use the Raspberry Pi Configuration tool or raspi-config to enable the camera.

Examples
This enables the camera interface using the Raspberry Pi configuration tool.
Raspberry Pi
sudo raspi-config
# Navigate to Interface Options > Camera > Enable
sudo reboot
This command takes a photo and saves it as image.jpg.
Raspberry Pi
raspistill -o image.jpg
This records a 10-second video and saves it as video.h264.
Raspberry Pi
raspivid -o video.h264 -t 10000
This Python code takes a photo after showing a preview for 5 seconds.
Raspberry Pi
from picamera import PiCamera
from time import sleep

camera = PiCamera()
camera.start_preview()
sleep(5)
camera.capture('/home/pi/image.jpg')
camera.stop_preview()
Sample Program

This program uses the Raspberry Pi Camera to take a photo after a 3-second preview and saves it to the home directory.

Raspberry Pi
from picamera import PiCamera
from time import sleep

camera = PiCamera()
camera.start_preview()
sleep(3)  # Wait 3 seconds to adjust
camera.capture('/home/pi/test_photo.jpg')
camera.stop_preview()
print('Photo taken and saved as test_photo.jpg')
OutputSuccess
Important Notes

Always power off your Raspberry Pi before connecting or disconnecting the camera to avoid damage.

The camera preview may not work if you are connected via SSH without a desktop environment.

Use full file paths when saving images or videos to avoid confusion.

Summary

Connect the camera carefully to the Raspberry Pi's camera port.

Enable the camera interface using raspi-config or Raspberry Pi Configuration.

Use raspistill, raspivid, or Python code to capture photos and videos.