

Calculate pi with a Raspberry Pi Spigot | The MagPi #106
source link: https://www.raspberrypi.org/blog/calculate-pi-with-a-raspberry-pi-spigot-the-magpi-106/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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?
You’ll find Adrian on YouTube as bornach
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.
Recommend
-
23
If our recent release of Retro Gaming with Raspberry Pi ,
-
7
Star Wars Arcade Cabinet | The MagPi #105 Why pay over the odds when you can build an accurate replica, and have fun doing it?For the latest issue of The MagPi...
-
4
‘Epigone drone’ pays homage to NASA’s Mars Helicopter | The MagPi #107 Inspired by NASA’s attempt to launch a helicopter on Mars, one maker made an Earth-bound one of her own. And she tells Rosie Hatt...
-
4
Humane mouse trap | The MagPi #108 Safely catching mice is a better way of fixing a problem, and using Raspberry Pi means it needs less supervision. In the
-
11
AuthMeReloaded "The best authentication plugin for the Bukkit modding API!" Type Badges General: Code quality: Jenkins CI: Other CIs:
-
6
Machine Learning Prosthetic Arm | The MagPi #110 This intelligent arm learns how to move naturally, based on what the wearer is doing, as Phil King discovers in the
-
4
How to Install Spigot Minecraft Server on Ubuntu 20.04 Spigot is a modified Minecraft server software based on CraftBukkit. It provides additional performance optimizations, configuration options and features while maintaining compa...
-
12
<?xml encoding="utf-8" ??>Introduction Spigot is a modification of the Minecraft server software, CraftBukkit. Spigot optimizes server resource usage, ensuring your players have the best experie...
-
6
<?xml encoding="utf-8" ??>Introduction Spigot is a modification of the Minecraft server software, CraftBukkit. Spigot optimizes server resource usage, ensuring your players have the best experie...
-
10
<?xml encoding="utf-8" ??>Introduction Spigot is a modification of the Minecraft server software, CraftBukkit. Spigot optimizes server resource usage, ensuring your players have the best experie...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK