This instructable will show you how to read the number on a binary pushwheel or thumbwheel switch using **LED’s or an AVR** microcontroller (I’m using an **ATmega328p** but this can be adapted for any **AVR** or probably another microcontroller of your choice). Multiple thumbwheel switches can be strung together to make an electronic combination lock, protecting your valuable…um….cookies!

Thumbwheel switches have detent buttons (technically called a “pushwheel”) or a wheel that you can push to change a number up or down by one. Inside they have switch logic that encodes the number on the dial as, typically, octal, hex, BCD (Binary Coded Decimal) . This encoding is then sourced to or sank from the external pads where it can be read and displayed (as with LED’s), decoded with IC’s (e.g. 4029,74HC93,and 74HC590), or hooked into input pins of a microcontroller and further processed.

My favorite local electronics shop had a bin full of ones that looked like old SCSI device ID switches so I grabbed a few to see how they worked. The numbers go from 0 to 15 so I think this would traditionally be called a hex switch, but I’ll be referring to them as binary because of the output we’re working with. Don’t confuse binary with BCD; they are not the same thing. Ok, let’s go!

## Step 1 Parts List

## You Probably Need These

- A binary pushwheel or thumbwheel switch. These can be had at the major electronics outlets online, or if you’re in Colorado, check out J.B. Saunders in Boulder. They have them on the cheap!
- A solderless breadboard
- A soldering iron, solder, hookup wire (I used 24 gauge stranded because it’s very bendable)
- An AVR microcontroller
- I’m using an ATmega328p sitting in a Bare Bones Board from Modern Device
- I’m using the USB BUB to program the AVR

## Optional

- Four LED’s (for illustration of the encoding)
- Four resisters (for the LED’s. I used 200 Ohm that I had already on my bench (I’m lazy
^{2}))

Modern Device]

- Four diodes if you’re paranoid about running reverse polarity into the pins of your MCU

## Something to write or compile your firmware

- I use AVR Studio 4.16 SP1 but you can also use:

*Windows*

- WinAVR for Windows AVR development

*FreeBSD / Linux (other *NIX variant)*

pkg_add -r avr-gcc -or- cd /usr/ports/devel/avr-gcc/ && make install clean pkg_add -r avr-libc pkg_add -r avr-gdb

- For Linux, you’ll be installing the same thing but using your particular package management utility.

*Mac*

- I have no experience here, so I’d suggest Ladyada’s tutorial for this.

There are other utilities, like Eclipse, that you may also like to use but this should be a good start.

Now that you know what you need, we’ll put it together, but in order to do that you need to know how the binary switch works…..so let’s move on to the next step!

## Step 2 Binary switches andTruth Tables

As I briefly mentioned in the introduction, a binary switch is a switch that encodes it’s state in binary. Binary is a method of encoding using two discrete states — one’s and zero’s — to represent other symbols when put together. Each one or zero is called a “bit” and these bits are usually strung together into groups of eight called a “byte.” Not all bits in a byte have to be used for encoding, however, and the ASCII character set is a notable example that only uses 7-bits (there is an extended version). The pushwheel switch in this instructable has a range from 0 to 15 and uses four bits to encode the number on the dial.

### A Brief Binary Digression

In order to understand how four bits can represent 16 different numbers, we have to know a little about how binary encoding does its job for this switch (and in general).

From the first picture, you should be able to see there are actually five pads coming out from the back of the switch. These are labeled 1, 2, 4, 8, and C. The C is considered a common terminal. That is to say, it’s a part of the switching circuitry that is always connected to whatever switches are flipped on in the switch. In my setup, I’ll have the “C” pad connected to ground and any current will flow from the open switches and out the C terminal and down to ground. Therefore, the “C” pad doesn’t contribute to the bits used in encoding the digit on the front of the dial; this is the sink of current for the switching circuitry inside the thumbwheel switch.

The 1, 2, 4, and 8 pads encode the bits for the number. In binary, the placement and number of 1’s and 0’s in a byte are used to determine the identity of an individual unit in a character set, or for our purposes, the number selected on the dial. Because we have four bits in our set, the maximum number of unique combination of ones and zeros is 2^{4} = 16. The best way to remember this is to raise the number of possibilities (here we have two possibilities: a 1 or a 0) by the number of bits (here we have four bits: pads 1, 2, 4, and 8). If we were to write out all unique combinations of 1’s and 0’s in four places it would look like this:

No 1's One 1 Two 1's Three 1's Four 1's ------ ----- ------- --------- -------- 0000 0001 0011 0111 1111 0010 0101 1011 0100 1001 1101 1000 0110 1110 1010 1100

So we have 16 unique possibilities arranging 0’s and 1’s and, conveniently, this switch’s counter goes from 0 to 15. It’s nice when a plan comes together. But you may be wondering if there’s a pattern for how each of those four bit arrangements (called a Nibble, by the way) represent the numbers. I’m glad you asked.

Remember the 1, 2, 4, and 8 designations for the pads? That’s not capricious labeling by the marketing folks. Let’s look again at our pattern of numbers. Each bit location has a “place” that can be thought of as it’s “power of two” location reading with the least significant bit on the right starting at the 0th bit. That is to say, we use a 0-based index and start counting from zero to one to two, etc, instead of counting in the vernacular where we would start with one. So, 2^{0} = 1, 2^{1} = 2, 2^{2} = 4, and so on. Arranging this from right to left we have something that looks like this for the four-bit pattern 0101 :

Place --> 8 4 2 1 0 1 0 1

To determine what number that “0101” represents, all we need to do is find every place that has a 1 in it and add it’s power of two number. In our example, there are ones in the 1 and 4 place. Add 1 + 4 and you get 5, so, the binary number 0101 represents the decimal number 5. Isn’t that exciting? Let’s try another one, this one a full 8 bits, even though our switch only has four bits. It’s good practice and is something you can impress with on your next date.

What is the decimal number that 01101110 represents? Here it is in their places:

128 64 32 16 8 4 2 1 0 1 1 0 1 1 1 0

Find the places that have 1’s and add those place numbers together. That would be 64 + 32 + 8 + 4 + 2 = 110. Pretty easy, huh? Knowing this now we are able to take our 4-bit switch and make a matrix of the pin numbers that have a 1 (logical one in digital is a higher voltage than a logical zero, in our case 5V and 0V respectively) against the decimal number they will represent. See the 2nd picture below for a table I made to represent the pads on the switch that would be at a logical 1 depending on the number selected on the front of the dial.

Now we can start building things. Follow me to the next step!

For more Detail: How to Read Binary/Hex Thumbwheel Switch with an AVR Microcontroller

Low cost PCB on PCBWay - only $5 for 10 PCBs and FREE first order for new members

PCB Assembly service starts from $88 with Free shipping all around world + Free stencil

**PCBWay 2nd PCB Design Contest**