# Arduino 8 bit Binary LED Counter

I made this Arduino 8 bit binary led counter as a solution for one member from Arduino forum. He asked if somebody can make a project that displays a decimal number in 8 bit binary format using 8 leds where a 1 is represented as a lit led. This project can be used in schools to display a binary number using leds.

The decimal number increases from 0 to 255 each time the momentary push-button is pressed. For example the number 0 is 00000000 in binary so no leds must be ON, number 1 is 00000001 so only the rightmost led will be on, number 2 is 00000010 so only the second led from the right will be on.

I must admit that it was a hard task but after a few hours I finally found the solution. The easiest part was to connect the 8 LEDs 🙂 and the most problematic part was writing the code that you can see below! I offer it for free because ElectroSchematics encourages the development of open-source projects even though I pulled my hair to come up with this solution. But hey, this is how we learn!

Watch this short video to see the 8 bit binary led counter in action:

Now lets see the schematic and how I’ve connected the 8 leds + resistors and 1 button + pull-up resistor to the Arduino Board (this was the easy part).

In the first picture you have the schematic as usual and in the second one is how the schematic looks on the Fritzing breadboard layout.

Arduino Binary LEDs Sketch Code

```/* source: http://www.electroschematics.com/9809/arduino-8-bit-binary-led/ */
int button = 2;			// pin to connect the button
int presses = 0;		// variable to store number of presses
long time = 0;			// used for debounce
long debounce = 100;	// how many ms to "debounce"
const byte numPins = 8; // how many leds
int state;				// used for HIGH or LOW
// pins to connect leds
byte pins[] = {5, 6, 7, 8, 9, 10, 11, 12};

void setup()
{
/* we setup all led pins as OUTPUT */
for(int i = 0; i < numPins; i++) {
pinMode(pins[i], OUTPUT);
}
pinMode(button, INPUT);
/* use pin 2 which has interrupt 0 on Arduino UNO */
attachInterrupt(0, count, LOW);
}

void loop()
{
/* convert presses to binary and store it as a string */
String binNumber = String(presses, BIN);
/* get the length of the string */
int binLength = binNumber.length();
if(presses <= 255) {	// if we have less or equal to 255 presses
// here is the scary code
for(int i = 0, x = 1; i < binLength; i++, x+=2) {
if(binNumber[i] == '0') state = LOW;
if(binNumber[i] == '1') state = HIGH;
digitalWrite(pins[i] + binLength - x, state);
}
} else {
// do something when we reach 255
}
}

/* function to count the presses */
void count() {
// we debounce the button and increase the presses
if(millis() - time > debounce)	presses++;
time = millis();
}
```

How does the code works?

From line 2 to 9 we use some variables to store different values (you can see the comments in the code for each line). Tbe byte pins[] is an array where we store each pin number where we’ve connected the leds but remember that the led at pin 5 is the rightmost led in our 8 bit configuration.

In the setup() function we set all led pins as OUTPUT using a for loop. I won’t insist on how the for() loop works but I promise I’ll start a C++ tutorial to explain some of the most used functions in the Arduino and microcontrollers environment.

Also, in the setup() you can see this line attachInterrupt(0, count, LOW); that is used to attach an interrupt on pin 2. In our case Arduino UNO has 2 external interrupts: on pin 2 with ID 0 and on pin 3 with ID 1. So the first parameter is the ID of the interrupt, the second one is a function we call (in our case count) and the third parameter is when to call our function (in our case we trigger the interrrupt when pin 2 is low). Here you can read more about it.

In the count() function we debounce the button and increase the value of presses each time we press the button (when we trigger the interrupt from the setup()).

In the loop() we convert the presses value to binary and store its value as a string in the binNumber variable. Then we get its length and store it as integer in the binLength variable. The if() statement checks if we pressed less or equal to 255 times and if so executes the code inside the for() loop. If the numbers of presses becomes higher than 255 then we can decide what to do. I’ve left this section empty because I had no idea what to do when the number is higher than 255 but probably we can flash all leds a few times and then reset it to 0.

In this for() loop I’ve lost a lot of my hair so let me tell you that you can donate some of you hair to my email address. Thank you! I won’t explain how the code works but I’ll let you decrypt it from the picture below.

One hint: number 5 is 101 in binary so we must turn ON leds on pin 7 and 5, the rest are OFF (as you can see in the first picture). I had to reverse the order of a certain amount of pins depending on how many bits are received.

• mariusherta

Hi there,

I need to do the same project for somebody.
Can you send me the plan and the needed products that I have to purchase?
TK

• queenofspace101

Hi, right now I am making this as a project for school, but I have been having problems with my build. I built it but, every time I upload the code it counts by itself. Since this is a project for school I have a deadline coming up and need to know asap. What should I do?

• mntvni93gmail-com

Hi guys, i’m new to arduino, i went to IT school 4 years ago and then done jobe completly different to programming code, now i want to restart coding using arduino, i’m waiting to get the board at home and meanwhile im using circuits.io to simulate program i tried to run the code but can’t turn on led witth encrease button… can someone help? thanks in advance

• Bits4Bots

I got it working with up and down.

• Bits4Bots

I’m getting some small errors. Any fix solutions?

int buttonup = 2; // pin to connect the button up
int buttondn = 3; // pin to connect the button down
int presses = 0; // variable to store number of presses
long time = 0; // used for debounce
long debounce = 100; // how many ms to “debounce”
const byte numPins = 8; // how many leds
int state; // used for HIGH or LOW
// pins to connect leds
byte pins[] = {5, 6, 7, 8, 9, 10, 11, 12};

void setup()
{
/* we setup all led pins as OUTPUT */
for(int i = 0; i < numPins; i++) {
pinMode(pins[i], OUTPUT);
}
pinMode(buttonup, INPUT);
/* use pin 2 which has interrupt 0 on Arduino UNO */
pinMode(buttondn, INPUT);
attachInterrupt(0, countdn, LOW);
attachInterrupt(1, countup, LOW);
}

void loop()
{
/* convert presses to binary and store it as a string */
String binNumber = String(presses, BIN);
/* get the length of the string */
int binLength = binNumber.length();
if(presses <= 255) { // if we have less or equal to 255 presses

for(int i = 0, x = 1; i < binLength; i++, x+=2) {
if(binNumber[i] == '0') state = LOW;
if(binNumber[i] == '1') state = HIGH;
digitalWrite(pins[i] + binLength – x, state);
if ((0 <= presses) && (presses 255) presses = 255;
if (presses debounce) presses++;
time = millis();
}
void countdn() { // down button
// we debounce the button and decrease the presses
if (millis() – time > debounce) presses–;
time = millis();
}

void countup() { // up button
// we debounce the button and increase the presses
if (millis() – time > debounce) presses++;
time = millis();
}

• Eric

Ah it is still deleting parts of my code!

• Eric

Add or change lines in setup to have:
int buttonup = 3; // pin to connect the UP button
int buttondn = 2; // pin to connect the DOWN button

pinMode(buttonup, INPUT);
pinMode(buttondn, INPUT);
attachInterrupt(0, countdn, FALLING);
attachInterrupt(1, countup, FALLING);

//Somewhere in the void loop() be sure to include

if ((0 <= presses) && (presses 255) presses = 255;
if (presses debounce) presses–;
dtime = millis();
}

void countup() { // up button
// we debounce the button and increase the presses
if (millis() – dtime > debounce) presses++;
dtime = millis();
}

• Eric

Bah some part of my code got lost!
//Somewhere in the void loop() be sure to include

if ((0 <= presses) && (presses 255) presses = 255;
if (presses debounce) presses–;
dtime = millis();
}

• Bits4Bots

Eric can you explain more about the countdown? Or show a pic please?
/* function to count the presses */
void count (){
// we debounce the button and increase the presses
if(millis() – time > debounce) presses++;
time = millis();
}
/*count down*/
void countdn ()
{
if(millis() – time > debounce) presses–;
time = millis();

}

Looking for the latest from TI?