jordin

I built an LED binary counter

January 02, 2022

It's the morning after I finished watching the 5th video in Paul McWhorter's series on Arduino and electronics. He spent much of the video reviewing the basics of switches and binary, so his assignment at the end was to build a small project with 4 LEDs (representing bits) that would visualize counting from 0 to 15.

Around 30 minutes into the project, I had everything wired up and all of the code written. Since the last project I worked on involved 3 LEDs, I was able to reuse the hardware setup, and I simply added one more LED to the fray. I excitedly uploaded my code to the Arduino board, but was dismayed to see that nothing happened.

After a few minutes of troubleshooting my code, I was greeted with a reward when a couple of the LEDs lit up. However, they weren't lit up as I was expecting based on the code that I wrote. For example, this is what was displaying when I sent the signals to display the binary number for 10 (1010):

My LEDs incorrectly representing the number 10 in binary

I immediately jumped back into analyzing my code, inserting Serial.print statements to inspect values and combing through Arduino documentation to see if there was something I had written that was misconceived. I've never written Arduino code before, and I haven't written C/C++ since college, so I figured there was probably something that I was misunderstanding.

After combing through every line of code and reaching the brink of frustration, I glanced away from my computer monitor back to my hardware -- only this time, I didn't just look at it, I really LOOKED at it. To my surprise, I noticed that three of the jump wires that connected my breadboard to the Arduino were connected to pins that they weren't supposed to be. I HAD CROSSED THE WIRES.

I had crossed the wires!

After fixing the wires so that they connected to the appropriate places, things almost immediately began working; the final issue to resolve turned out to be a loose jump wire that was occasionally creating an open circuit for one of the LEDs, preventing it from receiving power when it should've been lit up.

It was thus that my biases as a software engineer came back to bite me, and I learned some of my first real lessons in the world of hardware:

  1. Problems are just as likely to occur with the hardware as they are with the software
  2. Test circuits individually in the simplest way possible prior to integrating them into broader logic (would've caught the loose jump wire)
  3. Double check and then triple check lines of wiring to ensure everything is going to the expected places (would've caught the criss-crossed connections)

Lo and behold, once I had resolved my two hardware issues, everything worked like a charm. As has happened countless times in my career, I spent a significant amount of time debugging my code for nothing :)

The result:

The code:

#define DIGIT1 13
#define DIGIT2 12
#define DIGIT3 11
#define DIGIT4 10

#define INTERVAL 1000

// could theoretically support as many bits as there are pins
// to write to on the Arduino
#define NUM_BITS 4

int MAX_INTEGER = pow(2, NUM_BITS) - 1;

int currentBinary[NUM_BITS] = {0,0,0,0};

void setup() {
  pinMode(DIGIT1, OUTPUT);
  pinMode(DIGIT2, OUTPUT);
  pinMode(DIGIT3, OUTPUT);
  pinMode(DIGIT4, OUTPUT);
}

void loop() {
  for (int i = 0; i < MAX_INTEGER + 1; i++) {
    decimalToBinary(i);
    displayBinary();
    delay(INTERVAL);
  }
}

void displayBinary() {
  digitalWrite(DIGIT1, currentBinary[0]);
  digitalWrite(DIGIT2, currentBinary[1]);
  digitalWrite(DIGIT3, currentBinary[2]);
  digitalWrite(DIGIT4, currentBinary[3]);
}

void decimalToBinary(int decimal) {
  // edge case: a number is given that we don't
  // have enough bits to display
  if (decimal > MAX_INTEGER) {
    return 0;
  }

  // reset the output array
  for (int i = 0; i < NUM_BITS; i++) {
    currentBinary[i] = 0;
  }

  // divide input number by 2 until 0 is reached, calculating each
  // binary digit by
  for (int i = 0; decimal > 0; i++) {
    currentBinary[i] = decimal % 2;
    decimal = decimal / 2;
  }
}

Previous I'm learning electronics!Next My first original project! An LED Light Scale 💡