Pi on a Pi for Pi Day

Calculating Pi on a Pi

Here’s a quick and dirty post for Pi Day which uses Python code by David Bau (http://davidbau.com/archives/2010/03/14/python_pipy_spigot.html) to compute pi to an arbitrary number of digits. The code implements a “spigot algorithm” developed by Jeremy Gibbons (http://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf). This algorithm can generate the nth digit of pi independently from the preceding digits and runs O(n) in time.

In honor of Pi Day, I ran the code on a Raspberry Pi 3. I slightly modified the calling function to write the generated digits to files in batches of 10000. The computations get slower as the decimal place gets larger. I didn’t time the code exactly, but the time stamps on the files written indicate the first 10000 digits took less than 1 minute, the next 10000 took approximately 2 minutes, then 4 minutes, 6 minutes, 8 minutes, 12 minutes, etc…

Sure you could find and download pi from some online source, but it’s much more fun to do it yourself. I’m running it on my Raspberry Pi 3 right now, and plan to write a another quick and dirty project by tomorrow display them in some way for Pi Day.

Modified source code is below (original code on David Bau’s blog)

#Pi digit generator function
def pi_decimal_digits():
  q, r, t, j = 1, 180, 60, 2
  while True:
    u, y = 3*(3*j+1)*(3*j+2), (q*(27*j-12)+5*r)//(5*t)
    yield y
    q, r, t, j = 10*q*j*(2*j-1), 10*u*(q*(5*j-2)+r-y*t), t*u, j+1

#Caller program writes digits to disk in batches of 10000
count, digits = 0, pi_decimal_digits()
n_per_file = 10000
while 1:
  fn = "pidigits_" + str(count+1) + "_to_" + str(count + n_per_file) + ".txt"
  f = open(fn, 'w')
  print "at " + str(count)
  with open(fn, 'w') as f:
    for j in xrange(n_per_file):
  count += n_per_file


YAWP – Yet Another Wall Plotter

 The Inspiration:

I’ve been very slow to create posts these last few months.  I like to think it’s not due to laziness, but just that I’ve been so busy playing with other projects.  I’m pushing myself to write up the more interesting ones.  These days I’ve been obsessed with machines that draw.  The biggest of these projects has been my wall plotter, nicknamed YAWP (Yet Another Wall Plotter).

A wall plotter (also known as V-plotter or polargraph) works by moving a pen around a vertical (or slightly angled) drawing surface by means of two motors attached to string or a timing belt.  Although wall plotters are really just very slow, low resolution printers, the potentially unlimited scalability and numerous variations in the output make them compelling to watch.  Because of their simple design and because it’s fun to watch a machine drawing with a pen, they are far more engaging than a typical desktop printer.

Read moreYAWP – Yet Another Wall Plotter

Remote Raspberry Pi Access

Raspberry Pi Setup
The Raspberry Pi setup requires a lot of cables and power cords!
Minimalist Pi setup.
Minimal Pi: The new setup has only the Pi, a power cord, Wi-Fi dongle and cable for the webcam.  It goes anywhere!

I’m back to playing around with the Raspberry Pi.  Right now I’m considering webcam projects, and am looking for interesting subjects around the house.  The goldfish have been seeing a lot of air time because the dog refuses to stay in camera range.  Animal subjects aside, I’ve found an issue with the Pi that has really been bugging me.

The Pi’s best features are its low price and small size.  However, to fully access one, you need a mouse, keyboard and monitor.  If you want wireless network access, you’ll also need a Wi-Fi dongle.  Between the mouse, keyboard and Wi-Fi, you then have three serial components, but since the Pi has only two serial ports, you now need to add a powered USB hub.  Suddenly, there are wires and cables all over the place, and the Pi isn’t so compact and portable any more.

Read moreRemote Raspberry Pi Access

Raspberry Pi Configuration Settings with raspi-config

Initial raspi-config screen with setup options.
Initial raspi-config screen with configuration options.

While working on our latest project with the Raspberry Pi, I was installing various modules on the Raspbian version of Linux, when I was informed that I had run out of disk space. I was confused, because the SD card I was using had 4 GB, and I was sure that the modules I had installed were not that large.  It turned out that when you initially flash the SD card, it sets up the root partition as though you have 2 GB free on the SD card.  To fully utilize the available space on the SD card, you need to repartition the root drive.  Fortunately, that turns out to be very easy with a built-in utility called “raspi-config”.  Just type in “raspi-config” at the prompt, and you see the screen pictured at right.  Choosing the second option “expand_rootfs” automatically configures the root partition to utilize all available space on the SD card.  As I mentioned in a previous post, this utility is also useful for changing the default keyboard setup and timezone if you don’t happen to be from England.  I had to do a bit of googling to figure this out, so I hope this post saves someone a little bit of effort.

Adventures with Raspberry Pi – Setting Up

Raspberry PiThe Rasbperry Pi is a fully functional cleverly designed microcomputer that you can buy for only $35 (a more stripped-down version will soon be available for $25).  The developer’s intent was to design an inexpensive computer to be used by kids in developing countries to learn programming.  There has been a great demand by hobbyists, however, who have used it in a variety of projects.  I won’t list them all here, but I recommend Googling “Raspberry Pi project ideas” for inspiration.  There is a large online community devoted to all things Raspberry Pi related.

As soon as we heard about the Raspberry Pi, my middle son and I were excited to check it out, so we went online and ordered two of them.  There are only two online retailers who sell the Raspberry Pi for $35 in the U.S. – Allied Electronics and Element14.  If you purchase anywhere else, including Amazon, you’re probably paying a marked-up price from a reseller.

While waiting for our Pis to arrive, I downloaded the Raspberry Pi User Guide from the Amazon Kindle Store (it is also available in paperback) to read up on it.  I highly recommend this guide as it gives detailed instructions on setting up your Pi for initial use, and provides links to many online resources.

Read moreAdventures with Raspberry Pi – Setting Up