# Let’s Calculate Pi on a Raspberry Pi to Celebrate Pi Day

Breaking NewsDot PhysicsPiPi DayRaspberry PiScienceScience Blogs March 12, 2017 BigNews 0

I love pi. No, not pie. Pi. The number. This crazy number shows up in all sorts of weird places. If you take the square root of the gravitational field (*g *= 9.8 N/kg) you approximately get pi. Place a mass on a spring and let it oscillate? Yup, you get pi. Pi also one of the 5 super numbers in this magical equation:

These are just a few of the reasons I tend to get excited by Pi Day—March 14, for the uninitiated—and try finding new ways of celebrating it. This year, I decided to make an artistic expression of pi using my Raspberry Pi computer. Pretty cool, right? I wanted to somehow create something I can display in the hallway at school to share pi with students. Then they too might find themselves addicted to the awesomeness of pi.

You can calculate pi all kinds of ways, but most people just Google it: 3.1415926535897 and on and on and on to infinity. But what’s the fun in that? Wouldn’t you rather figure it on your own? Of course you would. This is my favorite way of calculating pi:

- Generate two random numbers between 0 and 1. Assign them to the point (x,y), for example, (0.332,0.818).
- Calculate the distance from the origin (0,0) to this random point
*r*. This can be written as*r*^{2}=*x*^{2}+*y*^{2}. - If the value of
*r*is less than 1, increase the value of a counter. I will call this*n*_{in}. - While you’re at it, increase another counter (call it
*n*_{total}) even if the value of*r*is greater than 1. - Repeat this until you get bored.
- You can estimate the value of pi as four times the value of
*n*_{in}divided by*n*_{total}.

Yes, this seems crazy, but it works every time. A picture might help. This one shows 1000 random points calculated using the process I just outlined. Those with an *r* value less than 1 are red; those greater than 1 are blue.

Notice that the red points approximate a quarter circle. In fact, the ratio of red dots to total dots should be the same as the ratio of the area of a quarter circle (with a radius *r*) to the area of a square with a side length *r*. The equation looks like this:

There you go—one cool way of finding pi using random numbers. Let me give you the Python code so you can play with it yourself. Click play to run and the “pencil” to see the code.

If you want some homework, see how many random points you need to get a calculated value of Pi to display correctly to the second decimal—so 3.14. What about the next digit at 3.141? You can keep going if you like. I often do.

### Building a Poster for Pi Day

Now, I admit that using random numbers to calculate pi is nothing new. But building an interactive poster that uses Raspberry Pi to find pi might be new.

Yes, it’s a bit crude, but that’s how I like it. I glued a Raspberry Pi to the poster. It runs a version of the random number calculation of pi. The board features a 16×2 LCD character display that provides a real-time value of the estimate of pi, along with a running count of the number of points. I added a reset button; push it and the random pi calculation starts over.

Because I am a physics professor, I added three pages of info explaining pi, how to calculate pi, and all the strange places you can find pi. I also added a beeper that emits a small (and very annoying) beep every time the computer adds another point to the diagram. I like the beeper because it draws attention to the poster as people pass it.** **I also really like the completely unnecessary LCD display because it looks cool.

OK, I’m sure you want to build one yourself. Here are some tips. (If you want to print this out, here is a pdf.)

To be honest, the 16×2 LCD required a bit more work than I would have liked. In the end, this guide on Adafruit did the trick. I found the buzzer and the beeper pretty easy using the python gpiozero library for the Raspberry Pi. This library makes using the input-output pins on a Raspberry Pi super easy.

For actual calculation, I use the python turtle library. It creates a graphical window with a small “turtle” you can control with Python. It’s not the fastest for calculations but it comes installed on the Raspberry Pi. If you want to look at the code, here it is. Make sure your LCD, buzzer, and button use the same gpio pins as Raspberry Pi.

That’s it. I hot-glued everything onto poster board and put a monitor next to it. Oh, one last thing. Disable the screensaver on your Raspberry Pi so everyone can enjoy pi as much as you do.

No comments so far.

Be first to leave comment below.