The Counting Box, Version Two

January 11, 2013

The Counting Box, Version Two

My Counting Box article in MAKE magazine was meant as a short “How I Made This” story, not a detailed “How You Can Make This Yourself” guide. Because people might want some instructions to follow in order to make their own counting box, I built a new version and documented it.

But first, an update on Counting Box Version 1. It’s been in service for 17 months and is showing a count of 121,860. This is three less than the highest number it’s reached. My son doesn’t seem to be a fan of negative numbers because -93 is the lowest the box has counted. The increment button has been pushed 18,422 times and the decrement button 4227. It’s only been recharged twice that I know of, so that giant battery was total overkill.

Now here’s how to build your own.

 

Please read through the entire build first and get an idea what you’re in for. I write with the assumption that you’ve got a basic understanding of electronics assembly, programming, and how to use the Arduino IDE. I’m only going to tell you to solder X to Y, not how to solder, what temperature to use, and so on. Also be aware that these instructions are only for the circuit, not for the enclosure. As you build it, think ahead to what kind of case you want to put it into because that could influence decisions like how long to make certain wires.

Circuit Parts & Pieces

  • Arduino
    An Uno, Duemilanove, or other similar board using an ATMega328. We’ll be pulling that ATMega chip out and putting it in our own standalone circuit.
  • ATMega328 w/ Arduino bootloader
    This will replace the chip you pull from your Arduino. I know you can burn your own bootloader, but this is easier.
  • 74HC00 quad NAND IC for debouncing the buttons
  • 24LC256 EEPROM chip for storing the count and related statistics
  • 28 pin IC socket for the ATMega328
  • 14 pin IC socket for the 74HC00
  • 8 pin IC socket for the 24LC256 EEPROM
  • 2 Adafruit 7-segment displays with I2c backpacks (choose any color you like)
    For the display and its driver in the first version of the Counting Box I used a MAX7219 IC and a pair of 4-digit seven-segment LEDs. These Adafruit displays are a little bit more expensive but really simplify the wiring.
  • 2 arcade-style buttons
    I used this and this from SparkFun. The circuit is built for SPDT switches but would work with SPST if you used a different method for debouncing.
  • 10-position coded rotary switch and knob
  • 2 22pF capacitors
  • 16MHz crystal
  • 2 perf boards
    I like the half-size Perma-Proto boards from Adafruit
  • Resistors: 10kΩ (x5), 220Ω (x1)
    I’m not going to link to each for purchase at 2 cents each because if you’re building a project like this you should have a big assortment already. If not, go get one.
  • 1 LED

Power Supply Parts

I’m breaking these out separately because you might want to do something else for the power supply. My original counting box used a USB charger and battery combined with a step-up voltage converter.

Tools

  • Soldering iron & solder
  • Breadboard
  • Wire, the more colors the better
  • Wire stripper
  • Multimeter

With all of that gathered, let’s start building!

Assemble the Displays

Instead of replicating the steps here, I’m just going to tell you to follow the assembly instructions that came with the 7-segment displays. You’ll be soldering the 4 digit display to the I2C backpack, then soldering a 4-pin header to that.

Short the A0 jumper on one of the backpacks to change its address, as seen circled below.

Wire the Rotary Switch

The rotary switch has 4 switch pins on one side and a ground pin on the other. One pin is for a value of 1, the next for a value of 2, the next for 4, and the last for 8. Hey, that sounds like a binary number system! It is. When the switch is set to 7, for example, the pins would read 0111.

Review the datasheet for your switch to understand which pin is which and how the pins are read.

I found the leads on the switch to be pretty fragile, so I soldered it onto a small perf board (here, from SparkFun). I routed the ground to the same side as the switch pins, then ran it all to a ribbon cable about 6″ long. Individual wires work fine if you don’t have ribbon cable available.

 

Wire the Buttons

The arcade switches are SPDT, so each has 3 leads. Solder an 8″ wire to each lead. I recommend using different colored wires so you can keep track of which is which. The switch itself can be removed from the button to make it easier to work with.

You can use buttons that aren’t SPDT, but you’ll need to come up with a different method for debouncing them.

Breadboarding

We’ll build the counting box circuit on a breadboard first to make sure everything works.

A Note on Pin Numbering

The ICs we’re using all have a notch on one end so you can orient them correctly. Pin numbers start with 1 at the top left as you’re looking at the IC with the notch on top. The numbers proceed down the left side, hop over to the bottom of the right side, then go up to the top right in a counter-clockwise pattern.

Breadboard the EEPROM

When I first wrote about the original Counting Box, some people recommended that instead of using the external EEPROM chip I should make a circuit that would only write to the internal EEPROM when power was switched off. This way I could use the internal memory but not worry about hitting its write limit. It’s a good idea, but the problem is that I’m not an electrical engineer and I couldn’t figure out a reliable circuit to do that. It would require a way to accurately sense the incoming voltage and a capacitor that can keep the circuit running for the 66ms or so that it would take to write all of the data (3.3ms per byte x 20 bytes).

Or I could buy a $1.50 chip and not have to worry about it.

The EEPROM needs pins 1 through 4 connected to ground. 1-3 are used to set the address in case you want multiple chips in the same circuit. Pin 4 is the ground. Connect pin 8 to +5v.

Pin 6 on the EEPROM is the clock. It connects to analog pin 5 on the Arduino (via the green wire in the photo below). Pin 5 on the EEPROM is the data line, and it connects to analog pin 4 on the Arduino (via the yellow wire below).

Breadboard the Display

Put the two displays side-by-side on the breadboard and run +5V and ground to the appropriate pins on each one. The displays are on the same I2C bus as the EEPROM chip, so run the data and clock lines from the 24LC256 to the displays. EEPROM pin 5 is data and will connect to the pin marked “D” on the display backpack, and EEPROM pin 6 is the clock connecting to the “C” pin on the backpack.

For the code I’ve written, the display with the shorted jumper needs to go on the right-hand side.

Breadboard the Debounce Circuit

Without some way to debounce the switches, each press will register as multiple presses and change your count by dozens or more. A pair of NAND gates coupled together will ensure that the SPDT switch is only in one state at a time, and the 74HC00 IC has 4 NAND gates on it. You can learn more about debouncing here.

Insert the 74HC00 into the breadboard with the notch toward the power supply. Run wires between pins 2 and 6 and between pins 8 and 12. Then between 3 and 4 on the left and 10 and 11 on the right. Connect 10k resistors between +5v and pins 1, 5, 9, and 13. The ground from the switches goes to ground, and the normally-open and normally-closed switch wires go to the same pins as the resistors.

Connect pin 14 to +5v and pin 7 to ground.

Connect pin 3 of the 74HC00 to pin 3 on the Arduino, and pin  8 on the 74HC00 to pin 2 on the Arduino.

To help myself understand and remember where everything went I made the breadboard diagram below. It might help you too.

Connect the Rotary Switch

The common wire from the rotary switch connects to GND on the Arduino.

Switch value 1 connects to Arduino digital pin 8, switch value 2 to pin 7, switch value 4 to pin 6, and switch value 8 to pin 5. Even though there’s the gap between pins 7 & 8 on the Arduino, the pins have a convenient placement on the standalone circuit we’ll build later.

When everything’s assembled it should look roughly like the photo below, except you probably don’t have the prototyping tray I made.

Programming

Download my code here (CountingBox2_1.ino.zip), unzip it, and install. You’ll also need to download and install the Adafruit GFX library and the Adafruit LED Backpack library. I’ve tried to comment my code as much as possible, but here’s a general overview of the main processes:

On startup the code loads the variables for the count, the minimum number reached, the maximum number, and the number of button presses. It initializes all of the buttons then checks to see if the counting box is being launched in standard counting mode, delete mode, or stats mode.  Delete mode is triggered by turning the rotary switch to 8 while holding the decrement button during startup. It’ll confirm you want to delete, and revert to standard mode if there’s not a prompt response. Stats mode is triggered by setting the rotary switch to 3 while holding the increment button during startup. Each push of the green button will step through a stat — max number, min number, increment button presses, and decrement button presses — before reverting to standard mode.

During the main loop the program compares the current count to the previous count. If they’re the different it saves to the EEPROM, if they’re the same then it does nothing. The button presses are triggered by interrupts, so there’s no checking for button presses during this loop.

The count (or any other number displayed) is broken into an array of its individual digits, then the array is stepped through and each digit is sent to the displays individually. I would have preferred sending a 4-digit number to each, but the library for the displays pads the left side with zeroes, so that sending 45 would end up as 0045.

A  timer blanks the display after one minute and puts the ATMega into low-power sleep mode. Pushing any button wakes it up.

Building a Standalone Circuit

When you’re satisfied that it all works then it’s time to tear it apart, move it away from the Arduino, and build it onto a standalone board.

Power Regulator

The counting box is going to need power, and you’ve got a couple of options for providing it.

In my original counting box I used a huge battery and a separate converter & charger. If this is your plan, skip this power regulator step and instead connect the +5v and ground from your power supply to the power rails on the prototyping board.

To keep things a little  simpler—not to mention cheaper—we can build our own circuit to convert 9v from common batteries to the nice, steady 5v the circuit needs.

Solder the 7805 into the first three rows on the protoboard. Run a wire from the middle pin to the ground column on the rail, then another from the right-most pin to the + column. In front of the 7805 put a 10uF capacitor between the input line on the left and the ground line in the middle. The silver stripe on the capacitor goes on the ground side.

Over on the other rail put another 10uF between power and ground.

You can connect the two rails now or later with a pair of wires across the bottom.

The breadboard diagram below might help clarify what goes where.

Put 9v into the input and use your multimeter to check that you’re getting 5v on the rails. I left off a power-indication light to save a tiny bit of power, but you could add one with a 220Ω resistor and an LED between +5V and ground.

ATMega 328

Solder the 28 pin socket with the notch toward the power supply. Be sure it doesn’t share any rows with the power supply.

Connect pin 7 to +5v and pin 8 to ground. Connect pins 20 & 21 to +5v on the other side, and pin 22 to ground.

Solder a 16MHz crystal between pins 9 and 10. It’ll fit parallel to the socket if you bend the leads in. Connect a 22pF capacitor between pin 9 and ground, and another between pin 10 and ground.

Solder a 10k ohm resistor between pin 1 and +5v.

To get power over to the left side, connect the two power rails at the bottom of the board if you didn’t already in the previous step.

Connect a 9v power source (6AA cells or 1 9v battery) to your power regulator. Be sure that the positive and negative leads go to the right location.

Load the sample blink sketch on your Arduino, then disconnect it from power. Gently pry the ATMega328 chip from the Arduino using a chip extraction tool or a small flat-head screwdriver. If you’re using a screwdriver, alternate sides and go slowly so that the pins aren’t bent.

Insert the ATMega into your socket, ensuring that the notch is facing the right direction. Connect a 220 ohm resistor and LED between pin 19 on the ATMega328 (equivalent to Arduino pin 13) and ground, then connect your power. If everything is assembled correctly the LED should blink.

Congratulations, you’ve built a standalone microcontroller! The design up to here can be used in other projects, but now we’re going to add components specific to the counting box.

24LC256 EEPROM

Solder the 8 pin socket onto the board right next to the 28 pin socket. The notch should be pointing toward the power supply.

Next, connect pins 1-4 to ground (the black wires circled below) and pin 8 to +5v.

I forgot to take a picture of this step, so pretend that I haven’t already soldered the 14 pin socket. Connect pin 6 of the EEPROM to pin 28 of the ATMega, and pin 5 of the EEPROM to pin 27 of the ATMega. Pins 27 and 28 on the ATMega are the equivalent of analog pins 4 & 5 on the Arduino.

The breadboard diagram below might clarify what goes where.

74HC00 Debounce Circuit

Solder the 14 pin socket right below the 8 pin socket, making sure again that the notch is toward the power supply.

Solder a wire between pins 2 and 6, and another between pins 8 and 12. It can help to refer to the circuit you built on the breadboard, because this is exactly the same.

Solder a wire between pins 3 and 4, and another between pins 10 and 11. With the wires connecting the power rails and the circles I drew to highlight the wires in this step, doesn’t the picture below look like a smiling face?

Wire pin 7 to ground and pin 14 to +5V. Now the face is getting a little cockeyed.

10k resistors go between +5V and pins 1, 5, 9, and 13.

Now connect the debounce circuit to the ATMega by running a wire from 74HC00 pin 3 to ATMega pin 5, and another from 74HC00 pin 8 to ATMega pin 4. The wire on the right-hand side will need to be routed to the left.

Solder the buttons to ground and to the same pins that you added the resistors. The normally-open side of the switches should go to pin 1 on the left side and pin 9 on the right.

Rotary Switch

Solder the common line from the rotary switch to ground, then switch 1 to ATMega pin 14, switch 2 to ATMega pin 13, switch 4 to ATMega pin 12, and switch 8 to ATMega pin 11.

Display

Get out a new protoboard to mount the seven-segment displays on. Don’t solder them yet, but find a good placement and note where each header pin is going. Remove the displays and connect the + and – rows where the display backpacks will connect to the + and – rails on the protoboard, circled below.

Now we need to route the clock and data signals to the appropriate rows. The photo-illustration below shows how it’ll be wired, but the wiring will be done on the back so it doesn’t get in the way of the display backpacks.

And this is what it looks like after you’re done and flip it over. Please excuse the sad condition of this protoboard; I desoldered a previous build of this step.

We need to get power and signal from the main board to the display board, so solder on a 4-conductor ribbon cable (or individual wires) to the +5v column, the ground column, the first row for clock, and the second row for data.

Solder the displays to the protoboard. Make sure that the pins line up correctly with the wires you attached and that the displays are parallel. There’s a surprising amount of room in which to get components out of alignment in those little holes.

Now solder the other end of the cable/wires to the main board. Ground and +5V go to their respective locations on the power rail, the clock line goes to pin 6 on the EEPROM, and the data line goes to pin 5 on the EEPROM (remember, the display and the EEPROM are on the same I2C bus).

Carefully insert all of the chips into their sockets. Be sure that the notches on the chip line up with the notches in the sockets.

Battery & Switch

Solder a switch on the input line from your power source. Now flip the switch and start counting!

 

The Box

I’m not going to give detailed instructions on how to make a box to put this into because I think that should be a decision that matches your sense of style, your ability, and the tools at your disposal. In my original counting box I used laser-cut bamboo from a pattern I made. Some other things that might work for housings would be a lunchbox, a cigar box, or even a clear plastic storage bin so the circuit can be seen. The buttons could be mounted on the top or from the side like on a pinball game.

Whatever box you use, you’ll need to at least do the following:

  • Drill a 1/4″  hole for the shaft of the rotary switch. You’ll also want to print out and glue down a label for the numbers around the switch. You can download a label that I made to fit around the knob linked to in the parts section. Push the rotary switch through the hole from the inside, thread the nut on to hold it in place, then push the knob on. You may need to rotate the switch to get the locations of each stop to line up with the labels.
  • Drill a 1-1/8″ hole for each arcade button. Separate each switch from its button and unscrew the large nut. Drop the button in from the outside of the box, tighten the nut against the inside, then re-attach the switch.
  • Drill a hole for your power switch and mount as appropriate, whether screw-in or snap-in.
  • Cut an opening for the display. I measure mine at .75″ x 4″, but you should check your own just in case they’re slightly different. There are holes at the corners of the LED backpacks you could use to attach to your box with screws, or you could use hot glue.
  • Be sure there’s a way to change the batteries when they run out. You don’t want the box permanently sealed, but if it’s for a child you don’t want the interior easily accessible either.

Finally, please share if do build your own counting box. Post a link to your website, Flickr account, etc. in the comments, or email pics and let me know whether or not it’s OK to post.

If you’ve got any questions I’ll do my best to answer, but I’m not an electrical engineer so I can’t get too technical. If you’ve got suggestions or can improve the design, I’d love to hear those ideas too.

UPDATE March 21, 2013:

Reader Marty has kindly created a schematic and shared it. Thanks Marty!
Counting_Box_V2_Schematic.pdf (16KB)

Leave a comment

Comments will be approved before showing up.


Also in Projects

The Laboratory Lamp
The Laboratory Lamp

February 17, 2016

First created in 1860, the Erlenmeyer flask has come to symbolize chemistry, and by extension science as a whole. I wanted to use that symbol to create an item of functional decor, and what better for that than a lamp, with the relationship between the light bulb and the “aha!” of an idea?

 

View full article →

Garbage In, Garbage Out
Garbage In, Garbage Out

February 04, 2016

For over a decade I’ve had kicking around in my garage’s attic the cases from a couple of old original-style Macintoshes, waiting for just the right project.

One day, after staring at them stacked in my office, I realized that they had a similar form factor to the classic bullet-top garbage cans with the swinging lid. I could give one of these a new (slightly unceremonious) life as a garbage can!

View full article →

Moon Phase Purse
Moon Phase Purse

January 20, 2015

I’ve been going through a moon phase recently (pun absolutely intended), and thought it would be fun to make a wearable or totable that displayed the current state of the moon. There would be a bit of hardware involved, so a purse seemed like the perfect vehicle—you’re already carrying a load of other things, so there’s no harm in the addition of a small battery pack or a pile of LEDs.

 

View full article →