cam_photo.py

Raspberry Pi 3 Model B+ (2018) — Board Sheet · File : cam_photo.py · Language : Python · Use case : Prendre une photo avec le module caméra officiel et Picamera2

Picamera2 is the modern Raspberry Pi OS camera API: the script sets a still configuration, starts the sensor and saves a timestamped JPEG. Enable the camera (raspi-config or config.txt camera_auto_detect) before the first run on this model.

Hardware

A Raspberry Pi 3 Model B+ (quad-core BCM2837B0 at 1.4 GHz, 5 GHz Wi-Fi, Bluetooth 4.2, shielded metal case SoC, 40-pin GPIO). One Pi Camera module (v2 IMX219 or HQ), CSI ribbon cable.

Wiring

CSI ribbon into the camera socket (clip up on the 40-pin side), copper contacts facing the pins; see the official docs for the CSI port location.

How it works

Picamera2 talks to libcamera: still configuration, start, asynchronous capture straight to file, then a clean pipeline stop.

Running it

Copy the script to the board (scp), then run it with python3 (or bash). Raspberry Pi OS ships gpiozero; otherwise sudo apt install python3-gpiozero. The 40-pin pinout is documented on the official Raspberry Pi site.

Variants

create_video_configuration for clips; capture_request_and_convert to get a NumPy array. 5 GHz Wi-Fi available: test both bands; the shielded case dissipates well but watch cpuinfo.

cam_photo.py

# cam_photo.py — prend une photo avec la camera officielle (Picamera2).
# Deps: sudo apt install python3-picamera2 ; activer camera en raspi-config
# Sous Bookworm : camera_auto_detect=1 dans /boot/firmware/config.txt.
# Le clip du port CSI se releve, on inserre la nappe, contacts cuivres vers la carte.
from picamera2 import Picamera2
from datetime import datetime

picam2 = Picamera2()
cfg = picam2.create_still_configuration()
picam2.configure(cfg)
picam2.start()
picam2.switch_mode_and_capture_file(
    cfg, datetime.now().strftime("photo_%Y%m%d_%H%M%S.jpg")
)
picam2.stop()
print("Photo enregistrée")

← Back to the board