Posted by & filed under Content - Highlights and Reviews, Programming & Development.

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

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:

    • Raspberry Pi
    • Camera module
    • A momentary switch
    • 3x female to male jumper cables
    • 2x male to male jumper cables
    • 220ohm resistor
    • Breadboard

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.


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:

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:

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

        • import os allows us to use Linux commands from inside the Python code, which means we can later use the raspistill command to take a picture.
        • import RPi.GPIO as GPIO gives us access to the GPIO library, and shortens its name to GPIO for ease of use.
        • GPIO.setmode(GPIO.BCM) sets the GPIO to use the Broadcom layout, and you can find a great source for quick reference at
        • GPIO.setup(23 , GPIO.IN) tells the program that pin 23 will be used for input, and we will attach our button to this pin.

So our While loop is as follows:

        • If GPIO.input(23)==1: while the button is pressed, os.system('raspistill -o image.jpg') take a picture using raspistill and call it image.jpg.
        • os.system(‘gpicview image.jpg &’) loads the picture we have just taken and places the process into the background of the system.
        • time.sleep(20) shows the picture for 20 seconds.
        • os.system(‘killall gpicview’) closes the picture viewer, ready for us to take another picture.

And here is a picture of the 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:


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

Tags: camera, General Purpose Input Output, GPIO, LED, Raspberry Pi, Raspbian, raspistill,

Comments are closed.