element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Sci-Pi Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Sci-Pi Design Challenge
  • More
  • Cancel
Sci-Pi Design Challenge
Blog Any Measurement to Pi - Blog #3: Working with Raspberry Pi Camera
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Sci-Pi Design Challenge to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: taifur
  • Date Created: 6 Jun 2023 4:10 AM Date Created
  • Views 1246 views
  • Likes 10 likes
  • Comments 0 comments
  • image processing
  • Raspberry Pi Camera
  • sci-pi
  • raspberry pi
  • raspberry pi 4 b
  • sci-pi design challenge
Related
Recommended

Any Measurement to Pi - Blog #3: Working with Raspberry Pi Camera

taifur
taifur
6 Jun 2023
Any Measurement to Pi - Blog #3: Working with Raspberry Pi Camera

In this blog post I will show how to connect a camera module to Raspberry Pi, configure it and take image using a Python program. Before getting started let see what type of camera module available for Raspberry Pi and which should work better for our project. The Raspberry Pi Foundation officially released its first add-on board: the Raspberry Pi camera module but it is of course possible to access a camera feed on the Raspberry Pi using a suitable webcam. In this project instead of using a webcam I will use a camera module because it is compact an will take less space compare to webcam. 

Available Camera Modules

There are now several official Raspberry Pi camera modules. The original 5-megapixel model was released in 2013, it was followed by an 8-megapixel Camera Module 2 which was released in 2016. The latest camera model is the 12-megapixel Camera Module 3 which was released in 2023. The original 5MP device is no longer available from Raspberry Pi.

All of these cameras come in visible light and infrared versions, while the Camera Module 3 also comes as a standard or wide FoV model for a total of four different variants.

image

Camera Module 3 (left) and Camera Module 3 Wide (right)

image

Camera Module 3 NoIR (left) and Camera Module 3 NoIR Wide (right)

Without these official version cheaper version is also available. All the pi Camera module is a camera that can be used to take pictures and high definition video.

Installing a Raspberry Pi Camera

Warning: Cameras are sensitive to static. Earth yourself prior to handling the PCB. A sink tap or similar should suffice if you don’t have an earthing strap.

Raspberry Pi Board has CSI (Camera Serial Interface) interface to which we can attach the PiCamera module directly. Regardless of the chosen camera, the setup process always works similarly, as long as the camera is supported by the Raspberry Pi. 

To attach the camera module to the Raspberry Pi, first make sure your Raspberry Pi is completely disconnected from any power source. Before removing the camera module from its grey anti-static bag, make sure you touch a radiator, a tap, or any other grounded metal object to get rid of any static that you might have built up.

The camera board CSI connector is located between the Ethernet and HDMI ports on the Model B Raspberry Pi. The Ethernet port does not exist on a Model A board, but the camera connector is in the same place.

image

Open the connector by pulling the tabs on either side of the connector upwards and towards the Ethernet port. You should then be able to insert the ribbon cable on the camera module (with silver connectors facing towards the HDMI port) into the connector. Hold the cable firmly in place, and close the CSI connector by pushing the top part towards the HDMI port and down. It should click into place nicely and hold the camera ribbon cable securely.

image

Raspberry Pi will look like following after installing the camera.

image

Raspberry Pi has created a video to illustrate the process of connecting the camera. Although the video shows the original camera on the original Raspberry Pi 1, the principle is the same for all camera boards:

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Setting Up the Camera

I hope you already installed the camera. The next step is to configure the Raspberry Pi to enable the camera and test the camera. As I am using the Raspberry Pi without any monitor I will configure the camera from the terminal. By default camera is disabled in Raspberry Pi. We need to enable it using the following sudo raspi-config from the terminal. For accessing raspberry pi remotely I am using PuTTY like before.

image

Then I run the following command from the terminal after login in to the terminal.

$ sudo raspi-config

A new configuration window will open. I choose Interface Options by pressing down arrow from the keyboard and go inside the option by pressing Enter.

image

Then I selected Legacy Camera option and hit enter.

image

I selected Yes and hit enter again.

image

Finally, I completed the setup by entering the Finish option.

image

By finishing the configuration it is better to reboot the Pi. Once you reboot, the camera module will be enabled and ready to start snapping pictures!

The basic commands are raspivid and raspistill, which (as the names suggest) are for capturing video and still images, respectively. Each of these commands can accept a number of parameters after them. Several parameters are available, but the most basic are:

  • -o or -output, for setting the output filename
  • -t or -timeout, for setting the time in milliseconds for which a preview will display (the default is 5 seconds, and when using raspistill, it will capture the last frame of the preview and save it to the specified filename. When using raspivid, the -t parameter defines the capture time.)

Therefore, to display a 5 second preview and then save a jpeg picture called test.jpeg, the command would be:

$ raspistill -o test.jpeg

To take a 30-second video and save it in h264 format, use the command:

$ raspivid -o test.h264 -t 30000

By adding different options, you can set the size and look of the image the raspistill command takes.

  • For example, add -h and -w to change the height and width of the image:
$ raspistill -o Desktop/image-small.jpg -w 640 -h 480

Any of the above command will take a picture and store it in /home/pi directory. It is the default file location folder. I got the test.jpeg as shown in the image below.

image

Using Python to take Picture

We can control Raspberry Pi Camera using Python. That will give us more flexibility and we can create amazing project using camera and python program. We need to use picamera to access the camera using python. This library is preinstalled with the OS. If the picamera library is not installed for any reason you can install it by the following command:

sudo apt install python3-picamera

Now, for accessing Pi Camera we need to create a Python script. We can do it in different way. Either we can use SSH terminal, VNC or we can make the file to our host PC and then transfer the file through WinSCP file transfer client.

As we are using SSH lets create a Python file from terminal. I am using nano text editor here. You can use your favorite text editor instead of nano. To create a Python file and open it with nano use the command bellow:

sudo nano camera_script.py

Now paste the following code to the file

from picamera import PiCamera

camera = PiCamera()
#camera.start_preview()
camera.capture("/home/pi/Pictures/img.jpg") print("Done.")
#camera.stop_preview()

Now press CTRL+X from the keyboard to close the script. It will ask to save the file. Just press y from the keyboard.

image

Your file is now ready to run. Before run the code let understand the code line by line. 

First, we import the PiCamera class from the picamera module. We are going to use that class to get access to the physical camera.

from picamera import PiCamera

Then we create an object from the PiCamera class. When we create this object, the camera will start initializing itself.

camera = PiCamera()

You can use the capture() function from the camera object to take a picture. You will have to give at least one argument: the path and name of the file to store the picture. Here we store the picture in the Pictures/ directory of the pi user, which is the default user on Raspberry Pi OS. Don’t forget the extension (most common: “.jpg”, you can also use “.png”), this will be helpful if you want to share and open the picture in Windows for example.

camera.capture("/home/pi/Pictures/img.jpg")

Note: the camera preview only works when a monitor is connected to your Raspberry Pi. If you are using remote access (such as SSH or VNC), you won’t’ see the camera preview. As, I am using SSH, I keep commented the preview command.

Now, run the code from the terminal using the following command:

sudo python3 camera_script.py

If everything goes fine you will get done message printed in the terminal.

image

For getting details about image setting please refer to the official getting started with camera page here.

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube