Make your own stop motion animation rig with a push button, using Python Picamera and GPIO.
You can use LEGO to animate a tower being built, figures acting out a scene, or anything else you can think of!
Before booting your Pi, you'll need to connect the camera.
- Locate the camera port next to the Ethernet port.
- Lift the tab on the top.
- Place the strip in the connector, with the blue side facing the Ethernet port.
- While holding the strip in place, push down the tab.
- Turn the power on to boot the Pi.
- Log in with username
pi
and passwordraspberry
. - Adjust the camera to point at yourself or an object.
- At the command prompt, enter
raspistill -o image1.jpg
. - You should see a preview appear on the screen for a few seconds, and then change briefly while the image is captured. It doesn't matter if the picture is upside-down; we'll come to that later.
- Run the command
ls
to see the files in your home directory; you should seeimage1.jpg
listed. - Enter
startx
to start the graphical desktop environment. - Once the desktop icons appear, the graphical interface has loaded. Click the file manager icon in the taskbar and you should see some folders and files.
- Double-click
image1.jpg
to preview it.
-
Double-click on
LXTerminal
to open a terminal window, and entersudo idle3 &
to start the Python environment. -
Select
File > New Window
from the menu to open a Python file editor. -
Carefully enter the following code (case is important!):
import picamera from time import sleep with picamera.PiCamera() as camera: camera.start_preview() sleep(5) camera.capture('/home/pi/image2.jpg') camera.stop_preview()
-
Select
File > Save
from the menu (or pressCtrl + S
) and save asanimation.py
. -
Press
F5
to run the script. -
Without closing the Python window, return to the file manager window and you'll see the new file
image2.jpg
. Double-click to view the picture. -
If the picture is upside-down you can either reposition your camera using a mount, or leave it as it is and tell Python to flip the image. To do this, add the following lines:
camera.vflip = True camera.hflip = True
inside the
with
loop, so it becomes:import picamera from time import sleep with picamera.PiCamera() as camera: camera.vflip = True camera.hflip = True camera.start_preview() sleep(5) camera.capture('/home/pi/image2.jpg') camera.stop_preview()
-
Run the file again and it will overwrite
image2.jpg
with a new image in the correct orientation. Remember to keep these lines in your code while you alter it in the next few steps.
-
Connect the Pi to the button as shown in the diagram below:
-
Import the
RPi.GPIO
module at the top of the code, set up GPIO pin 17, and change thesleep
line to useGPIO.wait_for_edge
like so:import picamera from time import sleep import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN, GPIO.PUD_UP) with picamera.PiCamera() as camera: camera.start_preview() GPIO.wait_for_edge(17, GPIO.FALLING) camera.capture('/home/pi/image3.jpg') camera.stop_preview()
-
Save and run your script.
-
Once the preview has started, press the button connected to your Pi to capture an image.
-
Return to the file manager window and you should see your
image3.jpg
. Again, double-click to view.
-
Modify your program to include a delay after the button wait, as follows:
import picamera from time import sleep import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN, GPIO.PUD_UP) with picamera.PiCamera() as camera: camera.start_preview() GPIO.wait_for_edge(17, GPIO.FALLING) sleep(5) camera.capture('/home/pi/image4.jpg') camera.stop_preview()
-
Save and run your script.
-
Press the button and try to take a selfie. Be sure to keep the camera still! Ideally, it should be mounted in position.
-
Again, feel free to check the image in the file manager. Run the program again to take another selfie!
-
IMPORTANT: Create a new folder to store your stills. In the terminal window, enter
mkdir animation
. -
Modify your code to add a loop to keep taking pictures every time the button is pressed:
import picamera import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN, GPIO.PUD_UP) with picamera.PiCamera() as camera: camera.start_preview() frame = 1 while True: GPIO.wait_for_edge(17, GPIO.FALLING) camera.capture('/home/pi/animation/frame%03d.jpg' % frame) frame += 1 camera.stop_preview()
-
Now set up your animation subject (e.g. LEGO), ready to start the stop motion animation.
-
IMPORTANT This time, do not run the program from IDLE as it will be impossible to break out of the loop. Instead, return to the terminal window and enter
sudo python3 animation.py
. -
Press the button to capture the first frame, then rearrange the animation subject and press the button again to capture each subsequent frame.
-
Once all the frames have been captured, press
Ctrl + C
which will terminate the program. -
Open the
animation
folder in the file manager to see your stills collection.
-
Now return to the terminal window.
-
Run the video rendering command:
avconv -r 10 -i animation/frame%03d.jpg -vcodec libx264 animation.mp4
-
With 10 frames this will take about 5 minutes. Once complete, you can play the video with the following command:
omxplayer animation.mp4
-
Optionally, you can adjust the frame rate by editing the rendering command. Try changing
-r 10
(10 frames per second) to another number.