Push Button Stop Motion
Make your own stop motion animation video with a push button controller, using Python and GPIO Zero.
You can use LEGO to animate a tower being built, figures acting out a scene, or anything else you can think of!
Connect the camera
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.
Test the camera
Open a terminal window from the application menu. Enter the following command:
You should see a preview appear on the screen. It doesn't matter if the picture is upside-down; you can configure this later. Press
Ctrl + Cto exit the preview.
Run the command
lsto see the files in your home directory; you should see
- Click the file manager icon in the taskbar and you should see some folders and files. Double-click
image1.jpgto preview it.
Take a picture with Python
Open Python 3 (IDLE) from the main menu:
File > New Windowfrom the menu to open a Python file editor.
Carefully enter the following code into the new window (case is important!):
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() sleep(3) camera.capture('/home/pi/Desktop/image.jpg') camera.stop_preview()
File > Savefrom the menu (or press
Ctrl + S) and save as
F5to run the script.
You should see
image.jpgsaved on your Desktop. Double-click the icon to open the image.
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.rotation = 180
camera = PiCamera(), so it becomes:
from picamera import PiCamera from time import sleep camera = PiCamera() camera.rotation = 180 camera.start_preview() sleep(3) camera.capture('/home/pi/Desktop/image.jpg') camera.stop_preview()
- Run the file again and it will overwrite
image2.jpgwith 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 a hardware button
Using your breadboard and jumper leads, connect the Pi to the button as shown in the diagram below:
gpiozeromodule at the top of the code, create up a
Buttonconnected to pin 17, and change the
sleepline to use
from picamera import PiCamera from time import sleep from gpiozero import Button button = Button(17) camera = PiCamera() camera.start_preview() button.wait_for_press() 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.
Take a selfie
If you want to take a photograph of yourself with the camera board, you are going to have to add in a delay to enable you to get into position. You can do this by modifying your program.
Add a line to your code to tell the program to sleep briefly before capturing an image, as below:
camera.start_preview() button.wait_for_press() sleep(3) camera.capture('/home/pi/Desktop/image3.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. You can run the program again to take another selfie.
Stop motion animation
Now that you have successfully taken individual photographs with your camera, it's time to try combining a series of still images to make a stop motion animation.
IMPORTANT You must create a new folder to store your stills. In the terminal window, enter
Modify your code to add a loop to keep taking pictures every time the button is pressed:
camera.start_preview() frame = 1 while True: try: button.wait_for_press() camera.capture('/home/pi/animation/frame%03d.jpg' % frame) frame += 1 except KeyboardInterrupt: camera.stop_preview() break
while Truegoes on forever, you have to be able to make it exit gracefully. Using
exceptmeans it can deal with an exceptional circumstance - if you force it to stop with
Ctrl + Cit will close the camera preview and exit the loop
frame%03dmeans the file will be saved as the name "frame" followed by a 3-digit number with leading zeroes - 001, 002, 003, etc. This allows them to be easily sorted into the correct order for the video.
Now set up your animation subject (e.g. LEGO), ready to start the stop motion animation.
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 + Cto terminate the program.
- Open the
animationfolder in the file manager to see your stills collection.
Generate the video
To generate the video, begin by returning to the terminal window.
Run the video rendering command:
avconv -r 10 -i animation/frame%03d.jpg -qscale 2 animation.mp4
Note you're using
%03dagain - this is a common format which both Python and
avconvunderstand, and means the photos will be passed in to the video in order.
You can adjust the frame rate by editing the rendering command. Try changing
-r 10(10 frames per second) to another number.
- You can also change the filename of the rendered video to stop it from overwriting your first attempt. To do this, change
animation.mp4to something else.
- Why not share your video? Try uploading it to YouTube!
- Now you know how to wire up a button to take a picture with the camera module, what else could you use this for?
- Could you do something similar for a time-lapse video?
- What could you use instead of a button? A motion sensor?
- Instead of making a video, what else could you do with photos taken with the camera module? You could post them to Twitter, or another social media site.