The following instructable will go through the steps to build a simple binary counter using the following materials.

1 x Arduino Uno

1 x Pushbutton

5 x LED's (however many bits you want your counter to be)

1 x 10k ohm Resistor

1 x 220 ohm Resistor (possibly 2 or 3 for a larger counter)

## Step 1: Build Your Circuit

Attached is a schematic view of the circuit that we will be using. For demonstration, I have built a 5-bit counter. You should assemble this circuit on a protoboard and attach to your Arduino.

## Step 2: Understanding the Circuit

This circuit is going to have 1 input and 5 outputs to the Arduino board. Using the 10K resistor, the board will be able to read when the button is pushed and when it isn't. If the button is not pushed, then pin number 2 will essentially be at 0V. When it is pushed, it will be brought up to +5V.

The 220 ohm resistor is current limiting. LED's require around 20mA of current to light up. Using ohm's law, if the output of pin 2 is put to 5V then 5V/220 = 23mA which will light up our LED.

When you have multiple LED's turned on, they will be dimmer. If you find that your LED's are too dim, add another current limiting resistor and separate the nodes. It would be best practice for each LED to have its own 220 ohm current limiting resistor, however, since this is such a simple circuit I tried to use as few as possible for the sake of easy wiring.

## Step 3: Program Your Arduino

Either download the attached arduino code, or copy paste the code below into your Arduino IDE. The code is explained in the next step.

int buttonState = 0;
int count = 0;

void setup() { Serial.begin(9600); pinMode(2,INPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); }

void loop() { digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW); digitalWrite(7, LOW);

if(buttonState == LOW) {

} else { count++; delay(200); Serial.print(count);

if((count % 2) > 0) { digitalWrite(3, HIGH); } else { digitalWrite(3, LOW); }

if((count % 4) > 1) { digitalWrite(4, HIGH); } else { digitalWrite(4, LOW); }

if((count % 8) > 3) { digitalWrite(5, HIGH); } else { digitalWrite(5, LOW); }

if((count % 16) > 7) { digitalWrite(6, HIGH); } else { digitalWrite(6, LOW); }

if((count % 32) > 15) { digitalWrite(7, HIGH); } else { digitalWrite(7, LOW); } delay(200); } }

## Step 4: Understand Your Code

In the setup loop for this code, we specify the function of each pin. Pin 2 is going to be the input. We will use this to read whether or not the pushbutton has been pressed. The remaining pins will all be outputs. If you would like to do a different number of LED's, simply specify as many output pins as you would like.

We will begin our loop by setting all the output pins to LOW. Then, we will do a digitalRead to see if the pushbutton has been pressed. If it has not been pressed, we will do nothing. If it has been pressed, we will add 1 to our integer count variable. We will then wait for 250 milliseconds. This will prevent our arduino from reading multiple counts from one button push. It will also determine how long your LED will stay on for.

We would also like to do a Serial.print function in order to verify that our count is outputting correctly on the LED's. Now the logic behind the circuit is the following

count % 2 will equal 0 for even numbers and 1 for odd numbers

count % 4 will be 2 or 3 (i.e. count % 4 > 1) when count is a multiple of 2 or 3

count % 8 will be 4,5,6, or 7 (i.e. count % 8 >3) when count is a multiple of 4,5,6, or 7

etc...

in this way, we can logically define when we want our outputs to be high and when we want them to be low. Upload your code to the board and test it to make sure that it works!