|

Raspberry Pi: Creating a Camera

A guest post by Les Pounder, a Freelance IT Consultant and Trainer from the UK who is a regular contributor to many Linux magazines and podcasts. You can find out more about him at about.me/lespounder.

For just over a year, the Raspberry Pi has dominated the attention of makers and the media as well. For many, the device has laid unused on a dusty shelf. But the Raspberry Pi is capable of many great things, all you need is a little imagination and a few accessories.

The biggest area for invention and tinkering on the Raspberry Pi is the General Purpose Input Output (GPIO) pins, a series of 26 pins located on the outer edge of the Pi. The GPIO allows you to attach standard electronic components, such as LED, push buttons and cables to the Raspberry Pi.

With the recent launch of the camera module, we now have a great new opportunity to show how easy it is to create a simple method of input, which produces a quick and exciting form of output – a photo or video. So, let’s make a simple button controlled camera with our Pi.

This guide, while simple, introduces the wonderful world of the GPIO to everyone, and would be an ideal project for children to try.

What you will need are the following:

The basic objective for our project is that when a button is pressed, it will run the raspistill command and take a picture.

So let’s wire this up. For safety reasons, please ensure that the Raspberry Pi is turned off before connecting any wires to the GPIO.

pi.first

We need to connect the 3v3 pin to the breadboards + rail (this is the red rail in the diagram), since this will give us the power we need. Then, we need to connect the GND from the Pi, to the - rail of the breadboard (this is the blue rail in the diagram).

Connect a momentary switch as shown in the diagram, and connect the + rail in line with the momentary switch as shown.

Now we connect the resistor, so that it bridges the button to the GND (- rail). Lastly, we connect pin 23 of the Pi, so that it is inline with the resistor on the breadboard.

Once we have checked that our wiring is correct, and as per the diagram, it’s time to power up our Pi.

You will need to have your camera module connected, and setup ready before proceeding any further. The camera plugs into the connector between the HDMI output and ethernet socket, with the silver stripes of the cable facing the HDMI socket.

We will need to install a new library, to allow us to talk directly to the GPIO. Ensure that your Pi is connected to the Internet.

To do this, we need to enter a few commands in the terminal, in Raspbian. The terminal is called LXTerminal, and an icon can be found on the desktop. Double left-click on the icon, and in a few seconds a terminal window should appear.

In the terminal window enter the following:

sudo apt-get update
sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio

What we have just done, is update a list of software that we can download, then installed some Python tools. Now we can talk directly to the GPIO in our program.

Let’s start coding!

Our code will be Python-based, and looks like this:

import time
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(23 , GPIO.IN)

while True:
    if GPIO.input(23)==1:
        os.system('raspistill -o image.jpg')
        os.system(‘gpicview image.jpg &’)
        sleep(20)
        os.system(‘killall gpicview’)
    else:
        print "Ready to take picture"
GPIO.cleanup()

Let’s step through the code and see how it works.

So our While loop is as follows:

And here is a picture of the camera:

camera

You could replace the raspistill command with raspivid which will record HD video from the camera. To use the raspivid command you need to specify an output filename, and a duration, in miliseconds. So, to record a 10-second video called test.h264 you would type:

raspivid -o test.h264 -t 10000

Conclusion

And that’s it, a simple little camera program that is ready to be hacked into a more feature-rich application. Have you thought about asking for filenames, or adding a time stamp to each picture? In the true spirit of open source, please take this code and make it your own.

Safari Books Online has the content you need

These books in Safari Books Online will help you create your Raspberry Pi projects:

Raspberry Pi Cookbook helps you solve specific issues for using Raspberry Pi, the $35 system on a chip that’s taking the computer and electronics world by storm. This cookbook covers a wide range of topics from Linux and Python to sensors and displays.
Getting Started with Raspberry Pi will show you just how valuable this flexible little platform can be. This book takes you step-by-step through many fun and educational possibilities.
In Raspberry Pi Networking Cookbook is an essential reference full of practical solutions for use both at home and in the office. Beginning with step-by-step instructions for installation and configuration, this book can either be read from cover to cover or treated as an essential reference companion to your Raspberry Pi.
Raspberry Pi For Dummies will help you discover why the supply for the Pi cannot keep up with the demand! Veteran tech authors Sean McManus and Mike Cook show you how to download and install the operating system, use the installed applications, and much more.

About this author

lespounder Les Pounder is a Freelance IT Consultant and Trainer from the UK. He has worked with organizations to provide bespoke training in hardware hacking and computing, and is a regular contributor to many Linux magazines and podcasts. You can find out more about him at about.me/lespounder.

About Safari Books Online

Safari Books Online is an online learning library that provides access to thousands of technical, engineering, business, and digital media books and training videos. Get the latest information on topics like Windows 8, Android Development, iOS Development, Cloud Computing, HTML5, and so much more – sometimes even before the book is published or on bookshelves. Learn something new today with a free subscription to Safari Books Online.
|

Comments are closed.