Draw a Spirograph with a Raspberry Pi

Recommended Project Supplies
Get the right supplies — selected and tested to work with this project.
View Kit
Areas of Science Computer Science
Pure Mathematics
Difficulty
Time Required Very Short (≤ 1 day)
Prerequisites None
Material Availability This project requires a Complete Raspberry Pi Projects Kit, available from our partner Home Science Tools. The kit can be used to do seven other projects.
Cost Very High (over $150)
Safety No issues

Abstract

Have you ever used a toy like a Spirograph® to draw precise, repeatable patterns on a piece of paper? What if you could use a computer to automatically draw the patterns for you? This project will show you how to do just that using the Raspberry Pi Projects Kit. Check out the video to see what this simple, but fun, project looks like:

Share your story with Science Buddies!

I did this project Yes, I Did This Project! Please log in (or create a free account) to let us know how things went.

Credits

Ben Finio, PhD, Science Buddies

Cite This Page

General citation information is provided here. Be sure to check the formatting, including capitalization, for the method you are using and update your citation, as needed.

MLA Style

Finio, Ben. "Draw a Spirograph with a Raspberry Pi." Science Buddies, 22 Nov. 2019, https://www.sciencebuddies.org/science-fair-projects/project-ideas/CompSci_p060/computer-science/spirograph-raspberry-pi. Accessed 16 Dec. 2019.

APA Style

Finio, B. (2019, November 22). Draw a Spirograph with a Raspberry Pi. Retrieved from https://www.sciencebuddies.org/science-fair-projects/project-ideas/CompSci_p060/computer-science/spirograph-raspberry-pi


Last edit date: 2019-11-22

Introduction

A Raspberry Pi (Figure 1) is a tiny computer that you can use to write programs and build and control your own electronic circuits, like the button circuit in this project. The circuit uses sensors to gather information from the world around it. You can use information from those sensors to tell your computer program what to do. Many electronic devices we use every day, like phones and video game controllers, use sensors in the same way.

raspberry pi 3B+
Figure 1. A Raspberry Pi computer. (Image credit SparkFun Electronics, licensed under Creative Commons Attribution 2.0).

You can do this project just for fun, or turn it into an engineering or science project. To make it an engineering project, you will need to follow the engineering design process. Start by defining what you want your program to be able to do, then work through the rest of the design process until you have a completed project to show off at your science fair.

If you want to use this for a science project, you will need to follow the scientific method. Start by coming up with a question for which you can use the patterns as a tool to help answer. For example, can you make patterns that create an optical illusion of some sort, and see how people react to them?

Bibliography

News Feed on This Topic

 
, ,
Note: A computerized matching algorithm suggests the above articles. It's not as smart as you are, and it may occasionally give humorous, ridiculous, or even annoying results! Learn more about the News Feed

Materials and Equipment Product Kit Available

This project is written for the Science Buddies Raspberry Pi Projects Kit. There are two kit options available:

To set up your Raspberry Pi, you will also need the following parts (not included in either kit option):

  • TV or computer monitor with HDMI input*
  • USB keyboard and mouse

*If you have a TV or monitor with a DVI or VGA input, you will need an adapter. If your monitor does not have built-in speakers, you will need external speakers or headphones. See FAQ for more information.

To do this specific project, you will need the following circuit parts from your kit:

  • Male-male jumper wires (5)
  • Pushbuttons (2)

Recommended Project Supplies

Get the right supplies — selected and tested to work with this project.
Project Kit: $199.95
View Kit

Experimental Procedure

Introduction

Have you ever been interested in computer graphics? What about the animated characters in video games or movies? This project will give you a simple introduction to drawing 2D graphics using Scratch. If you have not already, you will need to set up your Raspberry Pi before you begin. Watch the video below to see several different designs:

Drawing a Simple Shape

If you have never written a program in Scratch before, watch this video to learn how:

To begin, you will make a program that will automatically draw a certain shape. Think about what motions you would make with your hand if you wanted to draw a simple shape, like a square. You would need to put your pen down on a piece of paper and draw four lines.

You can draw shapes in Scratch using the Pen menu, which contains the pen down block. Enter the following program and see what happens when you run it:

raspberry pi  scratch screen spirograph
Figure 2

The program should make your cat sprite move around the stage and draw a square, like this:

raspberry pi  scratch screen square stage
Figure 3

You can make the program a little less cluttered by using variables and loops, instead of repeating the same code four times. Try entering this code in a new program:

raspberry pi  scratch screen spirograph
Figure 4

Now, it is easy to change the size of your square by changing thedistance variable. There is just one problem, if you change distance and run the program again, your previous square will still be on the screen.

raspberry pi  scratch screen 2 squares
Figure 5

You can fix this by using the clear block (under Pen) at the beginning of your program, which erases anything previously drawn by the pen. You can also use the hide block (under Looks) to make the cat sprite invisible, so it does not clutter up your drawing (remember that each sprite has its own Scripts tab, so if you add more sprites, each one will need its own hide block). Finally, if the cat is moving too fast for you to see and you want to slow it down, you can add a wait block inside the loop.

raspberry pi  scratch screen
Figure 6

Now, each time you run the program, you should just see a single blue square on your stage. You will also see the angle and distance variables in the upper-left corner, but you can decide to hide them later.

raspberry pi  scratch screen single square
Figure 7

What if you want to create a shape with a different number of sides, like a triangle or a pentagon? It would be annoying to figure out the angle you need to use for each new shape in your program, and change the number of loops. Luckily, you can write a clever program that will do this automatically. All you have to do in the following program is change the number_of_sides variable to draw a new shape. You might want to save your previous program and start a new one, since this program has a different purpose.

raspberry pi  scratch screen spirograph
Figure 8

Try entering the program above and running it with different values for number_of_sides. Can you use the program to draw different shapes?


icon learn

Learn More About:  The x-y coordinate system

  [Show]

Change How it Looks

You might think your drawings look a little boring with just a plain blue pen on a white background. It can also be annoying to have the variables that automatically display on the stage clutter up your drawing area. This section will explain how you can make your drawing more colorful!

To get rid of the variables on stage, you can add the hide variable ____ block at the beginning of your code, or uncheck the checkbox next to the variable's name in the Data menu.

To change the background, use the buttons under Stage near the Sprites area on your screen. From left to right, the buttons will allow you to choose an image from the existing Scratch library, draw a new image, upload an image from your computer, or take a new picture with your computer's camera.

raspberry pi  scratch screen spirograph
Figure 9

You can change the appearance of the pen using various blocks in the Pen menu. For example, here is a program that will draw each side of the shape in a different color.

raspberry pi  scratch screen
Figure 10

Try experimenting with different pen sizes, pen colors, and background colors to decide what you think looks best. Remember to save your program!

Making a Pattern

Now that you are familiar with the basics of controlling the pen, you can try creating more-advanced patterns. For example, what happens if you draw a whole bunch of shapes, but rotate each new shape a little bit from the previous one? The program below will result in multiple drawn polygons, each new one rotated by angle2 degrees. Create a new program to try it out.

raspberry pi  scratch screen spirograph
Figure 11

With the variable values in the screenshot above, the program creates this rainbow-colored pattern of squares:

raspberry pi  spirograph square pattern
Figure 12

Try entering the program above and playing around with different values for the variables to see what types of patterns you can create. Or, alter the program even further by adding additional move and turn commands to make more-complex shapes. Do not be afraid to experiment and try new things!


icon trouble

Troubleshooting:  My program does not work!

  [Show]

Making Your Pattern Interactive

So far, your drawing is not interactive at all. You click the green flag and your program runs on its own. What if you add buttons to change the pattern as it is drawing? You can do this by putting buttons on your breadboard, just like in the Design Your Own Video Game with a Raspberry Pi project. To build your circuit, you will need to use a breadboard. If you have not used a breadboard before, watch this video before you continue:

To build the circuit, you will just need jumper wires and the two pushbuttons:

M-M jumper wires
Figure 13
raspberry pi video game push buttoon
Figure 14

Follow the directions below to assemble the circuit. Note that the buttons each have four pins.



Part Picture Reference First Hole Second Hole
Black M-M jumper wire black M-M jumper J17
(Pi Wedge GND)
Ground bus
(right side)
Blue M-M jumper wire blue MM jumper J22 Ground bus
(right side)
Blue M-M jumper wire blue MM jumper J27 Ground bus
(right side)
Red M-M jumper wire red M-M jumper J20A7
(Pi Wedge G4)
Green M-M jumper wire green MM jumper J25A8
(Pi Wedge CE1)
Pushbutton raspberry pi video game push buttoon E20


E22
F20


F22
Pushbutton raspberry pi video game push buttoon E25


E27
F25


F27

raspberry pi video game breadboard
Figure 15

raspberry pi video game controller
Figure 16

Now, modify your Scratch code to use button presses to change one of the variables in your program. The example below uses the buttons to increase or decrease the number of sides of the polygons. What happens if you run the program and push the buttons while the pattern is being drawn?

raspberry pi  scratch screen spirograph
Figure 17

Advanced: Making a Spirograph

If you are ready for an even more advanced design, you can try making a spirograph. The code to make a spirograph can get pretty complicated, so be careful! Try entering the program below, then running it with different values for the angle1, angle2, distance1, and distance2 and variables. Changing the values can result in drastically different spirograph patterns.

raspberry pi  scratch screen
Figure 18

The values in the screenshot above should result in a spirograph like this:

raspberry pi   spirograph advanced
Figure 19

Try tweaking the variables or making your own edits to the code. How many different spirograph designs can you create?


icon learn

Learn More About:  Using advanced math

  [Show]

Going Further

Here are some ideas for you to continue exploring with creating your own graphics:

  • In this project, you wrote several programs that control the pen to automatically draw shapes. What about letting the user control the pen, perhaps with the arrow keys or the mouse? Can you make your own simple drawing program?
  • Can you use multiple inputs (from your keyboard and/or breadboard) to change different variables in the spirograph or pattern as it draws?
  • What about using other sensors that came with your kit, like the magnet, motion, or light sensors? See the other projects in your kit to learn how to use the other sensors.
.

Troubleshooting

For troubleshooting tips, please read our FAQ: Draw a Spirograph with a Raspberry Pi.

If you like this project, you might enjoy exploring these related careers:

Mathematician at work

Mathematician

Mathematicians are part of an ancient tradition of searching for patterns, conjecturing, and figuring out truths based on rigorous deduction. Some mathematicians focus on purely theoretical problems, with no obvious or immediate applications, except to advance our understanding of mathematics, while others focus on applied mathematics, where they try to solve problems in economics, business, science, physics, or engineering. Read more
artist drawing at computer

Multimedia Artist or Animator

If you've ever watched a cartoon, played a video game, or seen an animated movie, you've seen the work of multimedia artists and animators. People in these careers use computers to create the series of pictures that form the animated images or special effects seen in movies, television programs, and computer games. Read more
Computer programmer typing on a keyboard

Computer Programmer

Computers are essential tools in the modern world, handling everything from traffic control, car welding, movie animation, shipping, aircraft design, and social networking to book publishing, business management, music mixing, health care, agriculture, and online shopping. Computer programmers are the people who write the instructions that tell computers what to do. Read more
NASA flight software engineer

Computer Software Engineer

Are you interested in developing cool video game software for computers? Would you like to learn how to make software run faster and more reliably on different kinds of computers and operating systems? Do you like to apply your computer science skills to solve problems? If so, then you might be interested in the career of a computer software engineer. Read more


Share your story with Science Buddies!

I did this project Yes, I Did This Project! Please log in (or create a free account) to let us know how things went.

Frequently Asked Questions (FAQ)

If you are having trouble with this project, please read the FAQ below. You may find the answer to your question.

Kit General Questions

Setting Up and Using Your Raspberry Pi FAQ

These answers apply to the Raspberry Pi Model 3B+ which comes with the Raspberry Pi Projects Kit. If you are using a different Raspberry Pi model you will need to do some research on your own—we suggest starting with the Raspberry Pi Foundation's Setting up your Raspberry Pi page.

Kit General Answers

Q: Who is the kit appropriate for?
A: The kit is meant for anyone (ages 8 and up) who wants to learn some basic programming and electronics skills while having fun. Students up to age 10, or older if their reading skills are behind grade level, may need adult assistance in reading and following the on-screen instructions. The projects included in the kit were beta tested and approved by students ages 8 to 16.

Q: Are the kit parts reusable?
A: Yes, all the electronics components in the kit can be re-used to do new projects or to repeat the projects.

Q: Aren't there other Raspberry Pi kits on the market? How is yours different?
A: Yes, there are other Raspberry Pi kits, and some of them are quite good! The Raspberry Pi Projects Kit and Raspberry Pi Circuit Parts Only Kit have been designed to contain the specific materials needed to do the accompanying Science Buddies Raspberry Pi projects. Our kit and associated projects are specifically meant for people who have no prior experience programming or connecting circuits. The projects are 100% beginner friendly with clear on-screen instructions, pictures, and videos. We think the kit, with its associated projects, is one of the most fun kits out there! But, if you are already an ace programmer or electronics guru, you may not find this the best fit for your own personal use. Even so, it may be a fun way for you to introduce others to programming and electronics.

Q: I already have a Raspberry Pi. Can I just buy the circuit parts separately?
A: Yes! We sell two different kits: the Raspberry Pi Projects Kit which includes a Raspberry Pi and the required accessories, and the Raspberry Pi Circuit Parts Only Kit, which only contains the additional circuit parts you need to do the Science Buddies projects. Both kits contain an SD card with the Raspbian operating system and a desktop shortcut to the Science Buddies project instructions.

Q: What programming language do the projects use?
A: The projects use Scratch 2. Scratch is a "graphical" programming language developed by the MIT Media Lab. It allows you to write code by clicking, dragging, and snapping together color-coded blocks. This allows beginners to write working code without worrying about formatting or typos. On the Raspberry Pi, Scratch allows you to control the general purpose input and output (GPIO) pins so your program can interact with a circuit in the physical world.

Note: three different versions of Scratch (1, 2, and 3) are available for the Raspberry Pi. The instructions for the Science Buddies projects (including example code) are specifically written for Scratch 2, which runs well on the Raspberry Pi model 3B+. If you have an older model Raspberry Pi, Scratch 2 may run more slowly, or may not run at all. If you want to use a different version of Scratch, you will need to consult the official documentation for Scratch 1.4 or Scratch 3 and modify the programming steps accordingly.

Setting Up and Using Your Raspberry Pi FAQ Answers

Q: How do I connect my Raspberry Pi to my TV or computer monitor?
A: The easiest way to set up your Raspberry Pi is to use an HDMI cable (included in the Science Buddies Raspberry Pi Projects Kit) to connect to a TV or computer monitor that has built-in speakers. If you are using a computer monitor with an HDMI port but no built-in speakers, you will also need separate speakers or headphones with a 3.5 mm audio plug (a regular "headphone jack").

If your TV or monitor does not have an HDMI port, you will need an HDMI to DVI or HDMI to VGA adapter (see pictures in table below). DVI and VGA do not transmit sound, so you will need separate headphones or speakers if you are using one of those options.

HDMI DVI VGA
HDMI port
DVI port
VGA port

Q: Can I use a laptop as a display and/or keyboard?
A: The short answer is "not easily." Many newer laptops have HDMI ports, but they only function as HDMI out, to send a video signal from the laptop to a television or projector. They do not work as HDMI in to display an external signal on the laptop's screen. The laptop's keyboard is only designed to work with the laptop itself, not as a standalone keyboard for an external device like the Raspberry Pi.

The longer answer is that, similar to the Remote Desktop feature on Windows and Mac computers, you can use special software to remotely operate a Raspberry Pi that is connected to the internet. This would allow you to control a Raspberry Pi using your laptop's screen and keyboard. This option is only recommended for advanced users, and you can find instructions here.

Q: How do I connect my Raspberry Pi to the internet?
A: Unlike earlier models, the Raspberry Pi 3B+ contains built-in Wi-Fi functionality. It does not require an external USB Wi-Fi adapter. You can connect your Raspberry Pi to the internet by clicking the internet icon in the taskbar and searching for available Wi-Fi networks, just like you would on a Windows or Mac computer. Your Raspberry Pi also has an ethernet port, which you can use to plug directly into a router for a hardwired connection.

Q: How do I shut down or reboot my Raspberry Pi? There's no power button!
A: Unlike most computers, the Raspberry Pi does not have a power button. You can shut down or reboot by clicking the raspberry icon in the upper left corner of your desktop, then select Shutdown. After the Raspberry Pi has shut down, it is safe to unplug the micro-USB power cable. Plug the cable back in to reboot. Important: never unplug the power cable while the Raspberry Pi is still running. This can corrupt the SD card.

Q: How can I adjust the Raspberry Pi's display resolution?
A: Click the Raspberry Pi logo in the top-left corner of your desktop. Select Preferences, then Raspberry Pi Configuration, then click the Set Resolution... button on the System tab.

Q: I have everything connected properly. Why can't I hear any sound?
A: Right-click the speaker icon on the desktop taskbar. This allows you to manually select HDMI or analog (the headphone jack) for sound output. Make sure you have the proper output selected. Also, make sure your Scratch program is set to play a sound. You can write a simple program to test if your sound is working using the "when space key pressed" and "play sound meow" blocks.

Q: Why won't my Raspberry Pi turn on?
A: If your Raspberry Pi will not turn on (the screen remains blank after everything is plugged in), go through this checklist to make sure everything is set up properly.
  1. Make sure your SD card is pushed in all the way (see Figure 1).
  2. Make sure the red power LED on your Raspberry Pi (labeled "PWR," near the micro-USB port, see Figure 2) is on. This means the Raspberry Pi is receiving power from the micro-USB port. If the LED is not on, make sure you pushed the micro-USB connector into the micro-USB port all the way.
  3. When you first plug the micro-USB cable in, the green LED (labeled "ACT," next to the PWR LED, see Figure 2) should flash several times. This LED flashes when the Raspberry Pi reads data from the SD card. After the Raspberry Pi is done booting up, it should turn off. If it does not flash at all, your SD card might not be inserted properly. Go back to step 1.
  4. Make sure your display (television or monitor) is turned on. If your display is turned off, you will not see anything on the screen, even if the Raspberry Pi is on.
  5. Make sure your display is set to the correct input. Many modern TVs have more than one HDMI input, and some computer monitors have DVI or VGA inputs in addition to HDMI.

Raspberry Pi SD card inserted properly and not all the way in
Figure 1. A micro-SD card that is inserted properly (left) and one that is not pushed in all the way (right).

Raspberry Pi status LEDs
Figure 2. The PWR (red) and ACT (green) LEDs near the micro-USB port.


Q: My Raspberry Pi starts to boot up, but then it freezes or the screen goes blank. What is wrong?
A There may be a problem with your Raspberry Pi or SD card. If you are using the Raspberry Pi or SD card that came with the Raspberry Pi Projects Kit or Raspberry Pi Circuit Parts Only Kit purchased from our partner Home Science Tools, please contact them directly at service@homesciencetools.com for assistance.

Q: My Raspberry Pi froze and is not responding to mouse or keyboard input. What should I do?
A: First, be patient and give the Raspberry Pi a few minutes to try and process whatever it was doing. If you click on a whole bunch of things in rapid succession, or run a really complicated Scratch program, the Raspberry Pi might slow down or freeze temporarily.

Next, if you are using a wireless keyboard and mouse, make sure they have fresh batteries.

Finally, as a last resort, if your Raspberry Pi is not responding, unplug the micro-USB cable and plug it back in. In general, you want to avoid doing this, because suddenly cutting power to the Raspberry Pi without properly shutting it down first can corrupt the SD card, and prevent the Raspberry Pi from working properly.

Q: My Raspberry Pi is acting strangely (e.g. it suddenly will not boot up properly, certain programs do not work, etc.). What is wrong?
A: If your Raspberry Pi is not "dead," but seems to be behaving strangely, there is a chance that your SD card has become corrupted. This can happen if you unplug the Raspberry Pi's power cord without properly shutting it down first. See the next question.

Q: I think I corrupted my Raspberry Pi's SD card. What should I do?
A: If the SD card came with the Raspberry Pi Projects Kit or Raspberry Pi Circuit Parts Only Kit you purchased from our partner Home Science Tools, please contact them directly at service@homesciencetools.com for assistance. Make sure to include a detailed description of the problem you are having. They will work with you to resolve the issue.
Q: I need help with a question, related to my Science Buddies Raspberry Pi Projects Kit or Raspberry Pi Circuits Parts Only Kit, not listed here. Who can I ask?
A: Science Buddies has a special area of our Ask an Expert forums dedicated to the Raspberry Pi Projects Kit. Please note that the forums are staffed by volunteers, and it may take a few days to get a response. If you are doing a science project, please do not wait until the day before the project is due to ask an urgent question.

Ask an Expert

The Ask an Expert Forum is intended to be a place where students can go to find answers to science questions that they have been unable to find using other resources. If you have specific questions about your science fair project or science fair, our team of volunteer scientists can help. Our Experts won't do the work for you, but they will make suggestions, offer guidance, and help you troubleshoot.

Ask an Expert

Contact Us

If you have purchased a kit for this project from Science Buddies, we are pleased to answer any question not addressed by the FAQ above.

In your email, please follow these instructions:
  1. What is your Science Buddies kit order number?
  2. Please describe how you need help as thoroughly as possible:

    Examples

    Good Question I'm trying to do Experimental Procedure step #5, "Scrape the insulation from the wire. . ." How do I know when I've scraped enough?
    Good Question I'm at Experimental Procedure step #7, "Move the magnet back and forth . . ." and the LED is not lighting up.
    Bad Question I don't understand the instructions. Help!
    Good Question I am purchasing my materials. Can I substitute a 1N34 diode for the 1N25 diode called for in the material list?
    Bad Question Can I use a different part?

Contact Us

Related Links

News Feed on This Topic

 
, ,
Note: A computerized matching algorithm suggests the above articles. It's not as smart as you are, and it may occasionally give humorous, ridiculous, or even annoying results! Learn more about the News Feed

Looking for more science fun?

Try one of our science activities for quick, anytime science explorations. The perfect thing to liven up a rainy day, school vacation, or moment of boredom.

Find an Activity