A Spigot That Streams Digits of Pi

by bornach in Circuits > Raspberry Pi

11339 Views, 78 Favorites, 0 Comments

A Spigot That Streams Digits of Pi

spigot_stream_pi.jpg
The Spigot That Streams Digits of Pi #PiDay
components.jpg

There are several methods for calculating the value of Pi. One approach that was devised by mathematicians (Gibbons, Rabinowitz, and Wagon) generates the digits iteratively one after the other using what is called a spigot algorithm. There are a number of implementations of spigot algorithms that will run on the Raspberry Pi. But wouldn't it be cool if you can control the spigot algorithms using a physical spigot from which each digit of Pi drips out?

The following steps will describe how to dress up a Raspberry Pi to look like a shiny spigot from which the digits of Pi will be issued when opened.

Supplies

  • Raspberry Pi
  • A case for the Raspberry Pi - I used one from Pimoroni
  • MAX7219 8x8 LED modules - I used 5 of these
  • 3 LEDs
  • 10K potentiometer
  • 3x 150 ohm resistors
  • 2x 1K ohm resistors
  • 0.47 microfarad capacitor
  • Perforated prototyping circuit board
  • Male and female headers
  • Wire
  • Soldering equipment
  • Cardboard - both single layer and corrugated
  • Popsicle sticks
  • Zip ties
  • Glue

Daisy Chain the MAX7219 Modules

components_MAX7219.jpg
components_lollysticks.jpg
ziptied_MAX7219.jpg

The digits of Pi will be displayed on 5 MAX7219 8x8 LED matrix modules that have been daisy chained to create one scrolling display.

You can often buy these 8x8 LED matrix MAX7219 modules individually or already daisy chained in groups of four. They can be soldered together with connecting wires however this is not as rigid at using header pins with jumpers. If you don't have any spare sets of jumpers, the solder connection will work just as well but lacks the rigidity of jumpers. To keep all the modules level and straight, drill holes into a pair of popsicle stick to which the MAX7219 modules can then be zip-tied.

A Pi Hat for the MAX7219 Modules

components_perfboard.jpg
female_headers.jpg
headers_glued_MAX7219.jpg
headers_glued_MAX7219_underside.jpg
header_wires_MAX7219.jpg

There are probably MAX7219 compatible hats for the Raspberry Pi that one can purchase. I didn't have any on hand so created one using perfboard, header sockets, wire, and popsicle sticks.

The headers only need to cover pins 11 to 24 and a 5V pin on the Raspberry Pi. I used a pair of 1x8s headers and a 2x3 to provide 5V. However you can save a lot of work by buying a full pre-built 40-pin Raspberry Pi header socket.

To wire up the MAX7219, you need to solder:

  1. the VCC to 5V (Pin 2 or 4),
  2. GND to one of the ground pins (6, 9, 14, 20, 25)
  3. DIN to Pin 19
  4. CLK to Pin 23
  5. CS to Pin 24

In later testing, I discovered that my MAX7219 powered up with all the LEDs switched on which pulled down the 5V line of the Raspberry Pi during boot-up. So adding a switch to the 5V pin is strongly advised.

Connection for Spigot LEDs and Potentiometer

header_6pin_MAX7219.jpg
header_6pin_wires_MAX7219.jpg

The header socket that was soldered in the previous step will also be used to animate the 3 LEDs under the spigot, and to read the position of a 10K potentiometer that forms the spigot check valve. 6 pins will to broken out to a separate 1x6 header:

  • For reading the 10K Potentiometer
    • GPIO 18 (Pin 12)
    • GPIO 23 (Pin 16)
  • LED animation
    • GPIO 17 (Pin 11)
    • GPIO 27 (Pin 13)
    • GPIO 22 (Pin 15)
  • The 6th pin connects to a ground pin

Prepare the Potentiometer and Shape the Spigot

pot10K_wires.jpg
gold_cardstock.jpg
pot10K_nut_spacers.jpg
spigot_2cylinders.jpg
spigot_2nd_bend.jpg

  1. Solder two wires to the 10K potentiometer - one wire to centre tap, and one wire to one of the outer taps - doesn't matter which.
  2. Cut a 8 cm x 9 cm rectangle of card stock. Cut a hole, wide enough to accommodate the potentiometer shaft, positioned along the long axis near the centre. Also cut 2 slits perpendicular to the long axis as shown in the photo. I'm using card with a metallic finish on one side. You can also use white card and paint it a brass colour later.
  3. Cut out spacer rings from corrugated packaging cardboard that will fit over the potentiometer shaft. Unscrew the nut on the potentiometer before fitting the two spacer rings. Insert the shaft through the hole that you cut in the previous step
  4. Bend the cardboard rectangle into a cylinder so that it encloses the body of the potentiometer leaving just its shaft protuding through the hole. Fix the potentiometer in place by screwing on the nut.
  5. Bend the cylinder where you made those two slits in the cardboard rectangle.
  6. You can create further bends in the spigot outlet by cutting more parallel slits
  7. Tape or glue the cylindrical segments to fix them in place. I found that the metallic finish of card I was using made PVA glue less effective so I applied tape on the inside surface instead.

Helical Thread (Optional)

gold_rings_cut.jpg
gold_helix.jpg
spigot_helix_thread.jpg

Many spigots that you find on the outside of buildings and on water tanks often have threaded rims. You can create the appearance of a helical thread by cutting two cardboard rings and gluing them together to form a helix. Wrap this helical ribbon around the end of the spigot a few times. A few dots of glue will hold it in place.

The Spigot Assembly

LEDs_heatshrink.jpg
spigot_LEDs.jpg
cardboard_parts.jpg
spigot_cardboard_assembly.jpg
spigot_wall_assembly.jpg
spigot_assembly_glue.jpg
  1. 3 LEDs are used to depict the digits streaming out from the spigot. Solder their cathodes together. I'm using 3 LEDs cut from a cheap string of Christmas lights which were already connected in parallel so I only had to cut and break out the anodes to individual wires. Arrange the LEDs roughly into a column.
  2. Thread the 3 anode wires and common cathode wire through the spigot mouth so they emerge at the other end along side the pair of wires from the potentiometer. If you use different coloured wire it would make it easier to tell them apart. Otherwise you will need differentiate each wire by some alternative labelling scheme.
  3. Cut the following pieces from corrugated packaging cardboard
    • Large rectangle (14 cm x 10 cm) in which a hole has been cut to accommodate the spigot. Fold the long edges over - the intention is for this piece to partially enclose the Ethernet jack and USB ports on a Raspberry Pi, so use the placement of those ports to guide where to position these folds.
    • Square (6 cm x 6 cm) in which a hole has been cut that is large enough to pass the 6 wires emerging from the back of the spigot
    • Small cardboard (10 cm x 2 cm) brace
    • (Optional) circle (radius 3 cm) in which a hole has been cut to accommodate the spigot
  4. Insert the spigot through the holes in the cardboard circle and rectangle. Cut 6 slits in the rear of the spigot cylinder to create 6 tabs which can be splayed outwards. Thread the 6 wires through the hole in the cardboard square. Press the splayed out tabs flat with the cardboard square.
  5. Glue all touching surfaces of cardboard together. Glue the cardboard brace to pull the folded over edges toward each other so that each fold is held at a 90 degree angle.

The Spigot Electronics

circuit_components.jpg
circuit.png
resistors_capacitor_in_prefboard.jpg
spigot_circuitboard.jpg
spigot_circuitry_final.jpg
spigot_wired_6pin.jpg
The Raspberry Pi does not provide an analogue-to-digital converter on any of its GPIO pins, so reading the 10K potentiometer requires a bit of extra circuitry. Since we are only interested in detecting whether the spigot check valve is either open or closed, the positional accuracy is not that important. We use a common hack of charging a capacitor through the resistance of the potentiometer - a classic RC delay circuit - and then timing how long it takes for the capacitor to reach the threshold for a logic high voltage level. One GPIO pin does the charging while another GPIO pin reads the logic level of the capacitor. The spigot circuitry also includes current limiting resistors for the LEDs. I opted for 150 ohm resistors which stops these red LEDs from shining too brightly because I didn't want to detract from the digits scrolling on the display. You might want to try lower resistance values for a brighter LED animation, especially if you use different coloured LEDs that have a higher forward voltage.
  1. Cut a piece of perforated prototyping board that has at least 6 columns
  2. Place the 3x 150 ohm resistors, 2x 1K ohm resistors, and 0.47uF capacitor to create the circuit as shown in the diagram
  3. Solder the 6 wires from the spigot to their appropriate places in the circuit board consistent with where the LEDs and potentiometer are shown connected in the circuit diagram
  4. Solder 6 wires for the GPIO connections and ground connection
  5. Solder these 6 wires to a 1x6 pin header, paying close attention to the ordering - this must match the order of the connections on the 1x6 header socket on the MAX7219 hat that was wired up in an earlier step.

Final Assembly

raspberry_pi_mounting.jpg
wooden_sticks_cardboard.jpg
raspberry_pi_spigot_assembly.jpg
spigot_MAX7219_connect.jpg
raspberry_pi_MAX7219_hat.jpg
pi_on_tap.jpg

This step depends largely on which Raspberry Pi and what case you're using, so I describe only what I used while providing general guidelines.

  1. Prepare the Raspberry Pi case. I'm using a Raspberry Pi 2 case from Pimoroni that includes mounting holes into which wooden skewers can be inserted -- these are actually two halves of a stick from a Pick-up-Sticks game. Other Raspberry Pi cases might require drilling new holes or gluing attachment points on the top.
  2. Insert the wooden skewers into the air spaces of the corrugated carboard in the spigot assembly. This may not work if the direction of the corrugations go the wrong way, or the corrugations are too small for the skewers to fit. Otherwise, glue or tape the cardboard to the skewers.
  3. Plug the 6-pin header from the spigot assembly into the 6-pin header socket on the MAX7219 hat. Double check that the correct wires go to the appropriate GPIO and ground pins.
  4. Plug the MAX7219 hat into the Raspberry Pi GPIO header. Double check that you have plugged it into the correct rows of pins -- it is easy to be one row off which could result in damaging your Raspberry Pi when it is powered up.
  5. Attach a handle to the potentiometer. I made the handle from a popsicle stick and attached a card with the Greek symbol for Pi on it.

Load the Software

piday.gif

This step assumes you've already setup your Raspberry Pi, installed Raspian (or similar OS), connected it to the Internet, etc.

The digits of Pi are generated using Python. It requires the following packages be installed:

  • PiDigits - install this using the command:
    pip3 install --upgrade pidigits
  • luma.led_matrix - follow the installation guide to set up SPI, GPIO user permissions, and install the Python package.
  • Copy the two scripts, pi_day.py and read_10kpot.py, to your user directory on the Raspberry Pi.

Run the potentiometer calibration script:

python3 read_10kpot.py

and try turning the potentiometer 90 degrees as you would to open and close the spigot. If the numbers output to the terminal are not changing you might have the potentiometer reading circuit hooked up backwards. Try swapping the pinA and pinB values in the script.

Make note of the value output by the calibration script when the potentiometer is halfway between open and closed. Open the pi_day.py script in an editor and modify the valvePos comparison accordingly. Try running the modified script:

python3 pi_day.py

What might need tuning:

  • The order of the GPIO list dripPins to get the spigot LEDs to light up in the correct sequence.
  • The orientation, cascade, rotation configuration of the MAX7219 device

device = max7219(serial, cascaded=nDigits, block_orientation=90, rotate=0, blocks_arranged_in_reverse_order=True

  • The pinA and pinB GPIO setting in the ValvePot class

Once you have all the pi_day.py script setting configured according to your spigot wiring and particular MAX7219 display module, you should be rewarded with a stream of digits of Pi being generated on a Raspberry Pi, apparently flowing out of a spigot that can be manually opened and closed.