# Calculate pi with a Raspberry Pi Spigot | The MagPi #106

*Here’s an ingenious way of using a Raspberry Pi to calculate pi – and why not? Nicola King runs the numbers in the latest issue of The MagPi magazine. *

Pi is an irrational number, which means it can’t be expressed as the ratio of two integers.** **Since it has an infinite number of decimal places, calculating it to ever greater accuracy has long been an objective of mathematicians. So what better project for Pi Day (14 March) than to get a Raspberry Pi to calculate pi?

That’s what Adrian Chung reckoned when looking to create a project for a ‘speed round’ contest. “I thought it would be neat to use a Raspberry Pi to compute pi to arbitrarily high precision,” he tells us.

After looking into various methods, he learned about a class of algorithms that differed from the usual summing up of a sequence of decimal approximations. “Intriguingly, these so-called ‘spigot algorithms’ computed the next digit of pi after every few iterations of applying a small set of operations on a handful of integers,” he notes.

**Numbers on tap**

Rather than simply using a Raspberry Pi to compute pi with a spigot algorithm, Adrian thought it required a more visual approach. “The need for a more visually explicit indication of what was actually running on the Raspberry Pi gave me the idea of creating a physical spigot prop with a tactile check valve that can be used to pause or resume the iterations of the algorithm,” he explains. Upon the user turning the spigot, digits appear to flow from the tap and along an LED matrix display below.

“The MAX7219 8×8 LED display modules are daisy-chained SPI devices that are hooked up to the SPI interface on Raspberry Pi,” says Adrian. “They are powered directly off the 5V rail; however, I had to add a separate power switch because they power up with all the LEDs turned on and this was pulling down the supply voltage during bootup.”

Three GPIO pins are used to animate the LED drips from the spigot. “The LEDs were cut from a Poundland Christmas decoration. Current is limited by 150 Ω resistors so that the drips don’t appear overly bright against the scrolling display.”

A potentiometer in the spigot is connected to two GPIO pins to check the valve position. “This works by using one pin to charge a capacitor through the potentiometer, forming an RC delay, and then timing how long until a logic high is read by the other pin.”

**Adjusting the flow**

Adrian adapted an existing scrolling text demo script in the luma.led_matrix source code library: “I had to choreograph the dripping LED animation with the previous digits scrolling off to the left and the reveal of a new digit under the spigot.”

He also needed to alter the potentiometer reading script, replacing the simple timing loop with regular system time queries for greater accuracy.

So, how accurately can his Raspberry Pi Spigot calculate pi? “I left it to run for about six hours,” says Adrian. “It computed more than the first 8000 digits. It can compute pi much faster than this, but the animation of the digits streaming from the spigot would just be a blur.

“Because those integer variables in the spigot algorithm only get larger, they continue to consume more and more RAM as more digits are cranked out. I don’t really know how many digits of pi my 1GB Raspberry Pi 2 would have been able to calculate if I had just let it run.”

Whatever the answer, the project has proved a hit with the community: Adrian’s original tweet video has over 10,000 views and was retweeted over 100 times.

## Get your copy of The Magpi #106 now!

You can grab the brand-new issue right now from the Raspberry Pi Press store, or via our app on Android or iOS. You can also pick it up from supermarkets and newsagents. There’s also a free PDF you can download.

## 4 comments

## frank balma

I’m a little slow, but just heard about rasberry pi, I like to look at what can be done with it.

## JBeale

It’s a fun idea, and a nice implementation of it!

## JEFFREY L GREER

What a great way to demonstrate the value of pi (and Raspberry Pis)!

## Rusty Haddock

I got a million digits plus out of the algorithm this past weekend.