How to Set Up Raspberry Pi Camera Module
In this tutorial, you'll learn about How to Set Up Raspberry Pi Camera Module. We cover key concepts, practical examples, and best practices.
The Problem
You connected the Raspberry Pi Camera Module but libcamera-still -o test.jpg returns ERROR: *** no cameras available *** or mmal: Cannot read camera info, keeping previous defaults. The camera is not detected or produces a black image.
Quick Fix
Fix 1: Enable the Camera Interface
WRONG — assuming the camera works out of the box:
libcamera-still -o test.jpg
# ERROR: *** no cameras available ***
RIGHT — enable the camera in raspi-config:
sudo raspi-config
# Interface Options → Camera → Enable
# (reboot required)
Or edit config.txt directly:
sudo tee -a /boot/config.txt <<EOF
start_x=1
gpu_mem=128
camera_auto_detect=1
EOF
sudo reboot
Fix 2: Check the Camera Connection
WRONG — connecting the ribbon cable upside down:
# The silver contacts on the ribbon must face away from the Ethernet port
# (or toward the HDMI port, depending on the model)
RIGHT — properly seat the ribbon cable:
# 1. Lift the latch on the CSI connector
# 2. Insert the ribbon cable with contacts facing the correct direction
# 3. Press the latch down firmly
# 4. Check both ends (Pi side and camera side) are fully inserted
Check detection after fixing:
libcamera-hello --list-cameras
# Available cameras
# 0: imx219 [3280x2464] (/base/soc/i2c0mux/i2c@1e/imx219@10)
# Modes: 'SRGGB10_CSI2P' : 3280x2464 ...
Fix 3: Insufficient GPU Memory
WRONG — allocating too little memory for the GPU:
# (default gpu_mem=64 may not be enough for high-resolution cameras)
RIGHT — increase GPU memory:
sudo tee -a /boot/config.txt <<EOF
gpu_mem=128
EOF
sudo reboot
Fix 4: Camera Shows Black Image
WRONG — capturing in very low light without changing settings:
libcamera-still -o test.jpg
# (image is completely black)
RIGHT — adjust exposure and gain:
libcamera-still -o test.jpg --shutter 200000 --gain 4 --awb auto
# (200ms shutter, 4x analog gain)
Or for bright conditions:
libcamera-still -o test.jpg --exposure sport
Fix 5: Permission Denied for Camera
# ERROR: failed to import fd 14
# Permission denied
RIGHT — add user to the video group:
sudo usermod -a -G video $USER
# Log out and log back in
Fix 6: Camera Module Not Compatible with Pi Model
# Older Camera Module v1 is not compatible with Pi Zero natively
# (requires different ribbon cable: CSI-2 to mini-CSI)
Check compatibility:
# Camera Module v1/v2: works with all Pi models (different cable for Zero)
# Camera Module 3: requires Pi 4/5 or Zero 2 W
# HQ Camera: works with all Pi models
Fix 7: Record Video
libcamera-vid -t 10000 -o test.h264
# (records 10 seconds of H.264 video)
Convert to MP4:
ffmpeg -i test.h264 -c copy test.mp4
Use DodaTech's Camera Test Suite to benchmark camera modules, test focus and exposure, and validate image quality across different lighting conditions.
Prevention
- Enable camera in raspi-config before first use.
- Use a compatible camera module for your Pi model.
- Seat the ribbon cable firmly at both ends.
- Set
gpu_mem=128in config.txt. - Keep the camera lens clean with a microfiber cloth.
Common Mistakes with pi camera module
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
These mistakes appear frequently in real-world RASPBERRY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro