Introduction To Coding Class

Lesson #9: Binary Counting

Mission Brief: The Language of Machines

The control room hums with tension as another system failure cascades through Cogsworth City's networks. Chief Engineer Hartwell stares at the diagnostic readouts, her weathered hands gripping the console's edge. "The problem isn't the hardware," she mutters, pointing to a wall of blinking status lights. "It's how we're talking to it."

She turns to you, her newest apprentice engineer. "Every machine in this city speaks the same fundamental language. Not English, not code symbols, but something far more primitive and powerful: the ancient tongue of ones and zeros. Binary. It's how your HERO Board thinks, how it counts, how it makes every decision that keeps our automated systems running."

The status panel flickers again, and you notice something curious. Six indicator lights blink in sequence: off-on-off-off-on-on. Then they change: off-on-off-on-off-off. It looks random, but Hartwell nods approvingly. "Those lights are counting," she explains. "Each pattern represents a different number, but not in the decimal system you learned in school. They're counting in binary, the way machines naturally think."

She hands you a breadboard with six LEDs and two buttons. "Today, you're going to build a binary counter. Not just to flash some lights, but to understand the fundamental language that every computer, robot, and smart device in Cogsworth City uses to communicate. Master this, and you'll think like a machine. Fail to grasp it, and you'll forever be speaking a foreign language to your circuits."

What You'll Learn

When you finish this lesson, you'll be able to:

  • Build a 6-bit binary counter that displays numbers from 0 to 63
  • Control the counter with two buttons (add and subtract)
  • Understand how binary numbers work and why computers use them
  • Read binary patterns on LEDs and convert them to decimal numbers
  • Work with arrays to manage multiple LED states efficiently
  • Use functions to organize complex code into manageable pieces
  • Handle button debouncing to prevent multiple triggers
  • Apply bitwise logic concepts in real hardware projects

Understanding Binary: The Universal Machine Language

Think about how you count on your fingers. You have ten fingers, so our number system naturally uses ten digits (0-9). When you reach 9 and want to count higher, you run out of single digits and have to start combining them: 10, 11, 12, and so on. This is called base-10 or decimal counting.

Now imagine you're a machine with only two fingers. You can only show two states: finger up (1) or finger down (0). To count higher than 1, you need more "fingers" (in our case, LEDs). With two LEDs, you can represent four different numbers. With three LEDs, you can represent eight numbers. Each additional LED doubles your counting capacity.

This binary system isn't just a curiosity, it's the foundation of all digital technology. Every text message, every photo, every video game runs on this simple on-off language. Your smartphone might have billions of these tiny switches, but they're all just counting in binary, making decisions one bit at a time.

With six LEDs, we can count from 0 to 63. That might seem limited, but remember: the first computers that put humans on the moon had less computing power than a modern calculator. Understanding how to work within constraints, how to make every bit count, is what separates good engineers from great ones.

Wiring Your Binary Counter

Binary counter wiring diagram

This circuit uses six LEDs to create a visual binary display. Each LED represents one bit position, with the rightmost LED being the "least significant bit" (worth 1) and the leftmost being the "most significant bit" (worth 32).

  1. LED Array: Connect six LEDs to digital pins 2-7. Each LED needs a 220-ohm resistor to limit current flow. The resistors protect both the LEDs and your HERO Board from damage.
  2. Add Button: Wire a push button to pin 12 with a pull-down resistor. This button will increment our counter by 1 each time it's pressed.
  3. Subtract Button: Wire another push button to pin 13, also with a pull-down resistor. This button decrements the counter.
  4. Power Rails: Connect the positive rail to 5V and the negative rail to GND. All LED cathodes and button grounds connect to the negative rail.
  5. Current Path: Each LED's anode connects through its resistor to a digital pin. When the pin goes HIGH, current flows through the resistor and LED to ground, lighting it up.

The Complete Binary Counter Code