Skip to Main Content
It looks like you're using Internet Explorer 11 or older. This website works best with modern browsers such as the latest versions of Chrome, Firefox, Safari, and Edge. If you continue with this browser, you may see unexpected results.
Michigan State University

Getting Started with Raspberry Pi

This guide contains many resources on setting up and creating your first Raspberry Pi project.

 

First open the python editor, Thonny.

 

Open the editor, Thonny

 

This is what the editor looks like.

Thonny editor

 

Let's start coding! First, import the packages we will use.

import your packages

 

Next, set up the camera. Also called, instantiating the camera.

instantiate camera

 

Now, write the code for taking the photo. 

test photo code

 

Before testing the code, it needs to be saved. Save the file in the folder where you want your image to be saved. 

save your python file in the folder you want your image to save

 

Click the run button to start the program.

 

Once your code is finished running, the new image will be saved in your folder!

photo was taken

 

 

Create a Simple Time Lapse Program

 

Now, let's add a little more code to create a time lapse video. 

 

Here we are adding a for loop which repeats the code indented underneath it as many times as you tell it. The range (0, 15) will run this code 15 times. However, we also need to change the file name of the image or else the image will be overwritten 15 times! Here I am adding the current value of i to the end of the filename. The value of i will move through the range during the for loop. 

set up for loop

 

 

After running the new code (saved as a new python file in a different folder) you can see the Pi has taken a series of images all named in sequential order. The numbers start with 0 and end with 14 which is a total of 15 images. 

files saved with new naming

 

The images are great, but it would be even better if python stitched them all together into a gif!

To do this, we will use the package, Image Magick. However, there are many options out there.

You may need to install Image Magick, simply type "sudo apt-get install imagemagick -y" in the Pi terminal.

Once you have installed the package, write in the code to import it into your python program.

import image magick

 

After the end of the for loop, add the code to stitch together the images. This can take a little while so I am adding a print statement below so that I know when it is finished. 

add time lapse stitching

 

After running the code, you can see the print statement in the shell at the bottom of Thonny.

print statement

 

Go to your folder and view your timelapse!

animation done

Where to go next?

Next, you can modify the code by setting the time lapse to start after pressing a button, to run only during specific hours, or to tweet you the pictures! The options are really endless. I'd recommend picking up The Official Raspberry Pi Camera book. You can download it for free and see tons of great projects along with the full code and an explanation of what the code does. You can make an underwater camera, an infrared bird box camera, a wildlife camera, and so much more!

You can also use computer vision on the Pi for more advanced image detection. A wonderful computer vision library is OpenCV. It can be a bit of a trick to install so follow the next page of the libguide for instructions. 

Happy coding!

Michigan State University