# Exercise 1

## Binary Counter

Implement an LED array that counts in binary.

### Learning Goals

• Understand digital output
• Simple circuit design
• Resistors and LEDs
• Sketch code structure
• Linear code logic

Now that you have had a chance to experiment with the Arduino hardware and software, the next step is to make sure you can design a basic circuit and program a sketch to manipulate it.For this exercise, you will create a 3-bit binary counter using three LEDs.

Your counter should display the decimal numbers from zero to seven in binary, using one LED to represent each binary digit.Recall that to count in binary, each digit represents either a zero or a one. You will represent a zero by having the LED off, and a one by turning on the LED. The counting sequence looks like this:

```   Binary :  Decimal
0 0 0  :    0
0 0 1  :    1
0 1 0  :    2
0 1 1  :    3
1 0 0  :    4
1 0 1  :    5
1 1 0  :    6
1 1 1  :    7```

Create a breadboard circuit like the one in the photos below. You can use any kind of LED. Use 220 Ω (ohm) resistors for each one.

The circuit diagram represents this design. You should get used to looking at these kinds of diagrams and working from them to build your circuit. You won’t always be provided with a photo reference; in fact, usually you won’t.

Connect the common ground on the breadboard (the black wire in the photo) to a GND pin on the Arduino. Then connect the three colored wires from the resistors in front of the LEDs to pins 10, 11, and 12 on your Arduino board. They will represent the binary number as follows:

• Pin 10: the least significant bit (the “ones” digit)
• Pin 11: the middle significant bit (the “twos” digit)
• Pin 12: the most significant bit (the “fours” digit)

Then write an Arduino sketch that flashes the LEDs in the sequence above, cycling through the numbers from zero to seven.

Here is an example sketch you can use if you need help getting started with this. This program simply flashes each of three LEDs in sequence. You will have to add the logic to do the counting. You can copy and paste this code into a new Arduino sketch, and then save it.

```/*
ahdavidson: 4/9/11

Blinks 3 LEDs in sequence, turning each one on for 1/2 second.

The LEDs are connected on pins 12, 11, and 10 and are blinked int hat order.
*/

void setup() {
// set all LED pins to OUTPUT mode
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
}

void loop() {

// turn the first one on and leave it on for 1/2 second
digitalWrite(12, HIGH);
delay(500);

// now turn the first one off and the second one on, then leave it on for 1/2 second
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
delay(500);

// second one goes off and then the third one goes on for 1/2 second
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
delay(500);

// finally turn the third one off, and pause for another 1/2 second before starting over
digitalWrite(10, LOW);
delay(500);
}```

Now, if you want to jump ahead to a solution, or compare what come up with to my solution, click here: Binary3, then copy and paste that into a new Arduino sketch.

For extra credit, I suggest extending your circuit and sketch code to count with more bits. For example, you can add one LED, for a total of four, and count up to 15. If you don’t already know about loops, you will soon want to understand them so you can greatly simplify your code!