How to Interface the Picamera module with Raspberry Pi using python
Hey guys! Here is another series on Raspberry pi. In the article, we will learn how to install the camera module, take a picture, and send the picture as a mail. Less I forget, you can find other interesting articles on Raspberry Pi here.
Before we head on to the practical part, let’s brush on this powerful component of the Raspberry Pi — Pi Camera module.
The Pi Camera module takes pictures and high-definition videos. It has two versions which are the standard and NoIR versions.
- The standard version takes pictures in normal light.
- The NoIR version doesn’t have an infrared filter. It’s used together with an infrared light source to take pictures in the dark.
The Raspberry Pi Board has CSI (Camera Serial Interface) interface used to attach the PiCamera module.
This Pi Camera module can attach to the Raspberry Pi’s CSI port using a 15-pin ribbon cable.
For further reading on the Pi Camera Module, do check the docs.
Let’s head over to set up the components.
Hardware needed:
- Pi Camera module
- Raspberry Pi 3 Model B+
- a 5V 2A micro USB power adapter.
Software needed:
- PuTTy
- Raspbian OS
Note: Make sure the Raspberry Pi is turned off.
Step 1: Installing the Camera module
- On the Raspberry Pi, locate the Camera module port between the audio port and the HDMI port. If you’re using Raspberry Pi B, it is between the Ethernet port and the HDMI port.
- Insert the Camera module ribbon cable by gently pulling up the edges of the camera module port’s plastic chip. When inserting the cable, make sure the blue side is faced to the Ethernet port, and the silver side is facing the HDMI port.
- Then, push down the plastic chip into place.
- Power on the Raspberry Pi.
Step 2: Connecting the Camera module.
- SSH to the Raspberry PI using its IP address and port 22
- Enter the Username and password
- Upgrade and Update the Pi. It’s a good practice to always update and upgrade the pi each time we use it. Enter
sudo apt-get update
andsudo apt-get upgrade.
- On the terminal, run
sudo raspi-config.
- Click on the Interface option.
- Click on camera, then enable the camera interface.
- Reboot the Raspberry Pi
Let’s take a still picture to be sure it working properly.
On the command-line (SSH), run raspistill -o Desktop/pic.jpg
The command raspistill
is for taking still pictures. Since we are running the code from the terminal, we can’t view the image captured. But we can use some software like VNC to view Raspberry’s desktop or send the file via command-line to your PC.
Here, we’ll use VNC to view the picture taken.
Output:
First, install the Picamera library on the Raspbian OS. Else, we’ll get an Import error: ‘no module named PiCamera’.
sudo apt-get install python-picamera python3-picamera
Now, let’s write a python code that captures an image and sends a mail attaching the picture.
Step 3: Python code
- Create a python file
send_image.py
- Run
sudo nano send_image.py
- Enter the following codes
Code Explanation:
First, we import the Python picamera
library, which allows us to control the Camera Module. The sleep
library is used to add delays before the camera captures the image. Then, import smtp
(Simple Mail Transfer Protocol) is an SMTP client session object used to send e-mail between mail servers.
Next, the camera preview opens for five seconds, and then a still picture is captured.
Up next, we attach the file with the necessary details.
Finally, we send the mail usingsession.sendmail(sender_address,receiver_address, text)
- Now, run
python3 send_image.py
let’s check out the result-
Note: you must never save the file as picamera.py
.
Check the codes here.
Still, want more projects on Raspberry pi? Watch out for my next article. Or better still, follow 😊so you can get a notification for my articles.
❤ ❤ Thanks for reading this article. ❤❤