APC Australia

Getting creative with the Pi camera

Christian Cawley is playing Doom and other games on his Raspberry Pi without emulation.

-

The Raspberry Pi Camera is great fun to play with, and in this tutorial we’re going to use the camera with a push button that will choose a random filter for the image. We’ll be using the Bash terminal as our programmin­g language, which is a great way to introduce the powerful scripting language.

Hardware setup

The Raspberry Pi Camera now comes in many different variations, and they all connect to the camera port of the Raspberry Pi, located just behind the Ethernet/USB ports. Open the camera port by gently lifting the plastic lock upwards, then insert the ribbon connector with the blue tab facing the USB/Ethernet ports. Close the lock on the connector and give it a very gentle pull to make sure it’s in place.

Power up your Raspberry Pi and then go to Preference­s > Raspberry Pi Configurat­ion. Under the Interfaces tab click the Enable button for the Camera. Then click OK and reboot the Pi.

With the Pi rebooted we’ll now test the camera, so open a Terminal and type the following command to take a quick picture: $ raspistill -o test.jpg

A preview window should appear, giving us five seconds to frame the shot before the image is saved to disk as test.jpg. We can take a look at the image using the file manager. If there are any problems, go back and check that your camera’s properly connected and that the camera interface is enabled.

On the breadboard, place a button so that it sits across the central slot. There should be two legs on either side of the board. On one side of the board only, insert two jumper wires so that they’re in line with the legs. Place the other end of the wires on the GPIO. One should go to 3.3V which is pin 1, located at the top left of the GPIO connector. It’s the pin nearest the SD card slot. The other wire connects to GPIO17, located five pins below pin 1, on the same column. Please see the diagram (facing page) for visual confirmati­on.

Go to the main menu and select Programmin­g > Geany. Create a new file called random-art.sh and remember to save often.

We’ll be using Bash to write the code for the project. Bash is a scripting language and it can be used as a quick and easy tool to create a project. We start by telling the code where to find the Bash interprete­r:

#!/bin/bash

The next two lines use echo to write a value to a file. In this case it’ll enable GPIO17 for use, and then set the direction to in which will turn GPIO17 into an input pin with the default value of 0 (off). When the push button is pressed, the value of GPIO17 changes as we connect that pin to the 3.3V. So the default 0 (off) changes to 1 (on):

echo “17” > /sys/class/gpio/ export

echo “in” > /sys/class/gpio/ gpio17/direction

Moving on and we need to create an array that will store all 20 of the filter options. Arrays are data structures that store data using an index that starts from zero to determine the position of the item. Each item in the array has a position and we can retrieve items from the array by using the name of the array and the index number. If you’re a Python coder, arrays are known as lists. The first six items are:

array[0]=”none” array[1]=”negative” array[2]=”solarise” array[3]=”sketch” array[4]=”denoise” array[5]=”emboss”

More filters add artistic flair.

Oils, watercolou­rs, film and graphic pen create striking images:

array[6]=”oilpant” array[7]=”hatch” array[8]=”gpen” array[9]=”pastel” array[10]=”watercolou­r” array[11]=”film” array[12]=”blur”

The final filters for the array create images with bright colours, low colour posters and washedout colours. array[13]=”saturation” array[14]=”colourswap” array[15]=”washedout” array[16]=”posterise” array[17]=”colourpoin­t” array[18]=”colourbala­nce”

array[19]=”cartoon”

Next, create a variable called “size” that stores the output of a command. This command will return the number of items in the array. Using the { } brackets around the command means that we’re only interested in the output of the command.

size=${#array[@]}

To constantly run the code we use a while true loop. Anything inside the loop will be run over and over until we exit from the code.

while true; do

Inside the loop the first step is to generate a random number between zero and the number of items in the array, 20 in this case. We do this by creating a variable, index and storing the output from a random number generator that uses the value of size as a limit. Note that we’ve indented the code inside of the loop by one space, to help identify that the code is in the loop.

index=$(($RANDOM % $size))

To print the name of the selected filter we echo the item stored in the array at the randomly selected index:

echo ${array[$index]}

The button connected to GPIO17 has a default state of 0 (off), but when pressed it’s connected to the 3.3V pin, causing the state to change to 1 (on). To see this change we need

QUICK TIP The Raspberry Pi Camera is quite delicate. Always ensure that the Pi is turned off when inserting or removing the flat flex cable. The blue tab should face the USB/ethernet ports and be held securely in place.

to create a variable, data that will store the output of checking the value of the GPIO pin.

data=”$(cat /sys/class/gpio/ gpio17/value)”

A conditiona­l test is used to check the contents of the data variable. If that value is “1” then two lines of code are activated. Otherwise the code keeps looping:

if [ ${data} = “1” ]; then

The two lines of code create a new variable, TIME which stores the current date and time in year, month, day, hour, minutes and seconds format. This variable is then used with the next line to create the filename using the -o switch. The filter used for the image is set using the randomly chosen index value from the array.

TIME=$(date +”%Y-%m%d_%H%M%S”)

raspistill -ifx ${array[$index]} -o $TIME.jpg

The final two lines of the code will end the if conditiona­l test, and then end the while true loop:

fi done

Save the code and open a terminal. In the terminal use the following command to make the file executable:

$ chmod +x random-art.sh

To run the command type the following

./random-art.sh

and press the button when you’re ready for an arty photo. Say cheese!

 ??  ?? Use the filters to randomly create a photo that belongs in an art gallery, displaying an exhibition on pop art.
Use the filters to randomly create a photo that belongs in an art gallery, displaying an exhibition on pop art.
 ??  ?? Just a button connected to two GPIO pins via a breadboard is an ideal way to introduce electronic­s via a quick hit of fun.
Just a button connected to two GPIO pins via a breadboard is an ideal way to introduce electronic­s via a quick hit of fun.
 ??  ?? No this isn’t a misprint, this is the low-res black and white “gpen” switch in action. Now anyone can be in a 1980s pop video!
No this isn’t a misprint, this is the low-res black and white “gpen” switch in action. Now anyone can be in a 1980s pop video!

Newspapers in English

Newspapers from Australia