# How To: Build a Half-Adder using Arduino!

The half-adder is a very important bit of logic, forming the main building block of binary number manipulation in computing. Why not make your very own?

I’ve previously created a project like this using Altera FPGA boards and Verilog… needless to say, C and Arduino are a far cheaper and less frustrating combination!

However, unlike some fancy FPGA boards, for this project you’ll have to ‘roll your own’ circuitry (LEDs and switch setup) using a breadboard.

This is a fun learning exercise, giving you some practice at circuit assembly as well as some basic digital logic. Let’s get started!

Here’s what you’ll need to get this experiment off the ground:

• 4x 1k Resistors
• 2x LEDs (any colour you like)
• 2x two-way push button or toggle switches
• Wires, wires, wires! Solderless, plug-in breadboard wires were what I used here

## Let’s Build It!

### 1) Build your LED layout

This circuit has a couple of different parts, so let’s build and test them one at a time. We’ll start with the LEDs, as these will be our outputs and it’s dead easy to see whether you’ve connected them properly.

One of the LED modules: circuit diagram.

Connect pin 13 to the first rail on your breadboard — these are the two long ‘border’ parts of your breadboard — they’re different because they conduct down the length of the board, rather than horizontally like the main body. This is great for connecting a bunch of components to the same source, or just saving space.

Next, connect your LED’s anode (long leg) somewhere along the length of the rail. Plug the short leg (cathode) into a hole on the main body of the breadboard.

Now, grab a resistor and plug one of its wires into the same row that you plugged the LED’s short leg. Place the other side of the resistor in a free row (this avoids the dreaded short circuit).

Finally, grab a wire and connect this row to ground (an analog port marked ‘GND’) on your Arduino.

#### Code Time

It’s now time to tell the Arduino to actually do something. The code below will cause any LED connected to pin 13 to blink.

``` void setup() {
pinMode(13, OUTPUT);
}

void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}

```

If you see your LED flash, you’ll know everything is working smoothly. If not, carefully check each connection — this is a great exercise in circuit debugging!

Once you’ve achieved one blinking light, build another one next door and connect it to whichever pin you’d like (I picked pin 10). Try to alter the code above to test your new addition, and get them to blink in a pattern or one at a time.

HINT: make sure your second pin and LED are connected to a different column of the rail than your first LED circuit!

### 2) Set up your input switches

Now here’s where it starts to get a little messy with the wiring. Pay close attention to each connection because if you’re anything like me, it’s easy to start thinking about nachos. I can sympathise.

Build your switches on the opposite side of the ‘valley’ in the middle of your breadboard. That valley keeps both sets of connections separate, so you don’t get any strange interactions between your sub-circuits.

Using your new knowledge of breadboard conduction, plug your Arduino’s 5V port into a column of the rail on this new side of the breadboard. Then, connect the other rail column to your Arduino’s ground pin.

Using another wire, bridge this rail to one end of your switch. Connect the other end of your switch to a new row, and plug a resistor to the end of this row. Plug the other end of this resistor into a new row, and connect that row to ground.

Now, connect a wire from pin 5 (or whichever pin you’re using) to this row, between the switch’s wire and the resistor (see circuit diagram for more nuance).

#### Test it!

```int buttonOne = 5;

void setup(){
pinMode(buttonOne, INPUT);
pinMode(10, OUTPUT);
pinMode(12, OUTPUT);
}

void loop(){
if(inputA == 1){
digitalWrite(10, HIGH);
} else{
digitalWrite(10, LOW);
}
}```

Run this code, and see whether your new switch has any effect. If not, be sure you’ve got your circuit grounded properly, check each connection and if you’re using push button switches like me… make sure they’re turned the right way around!

(TIP: You might also find that you can connect the LED modules you built in the first part of the tutorial to the ground rail you connected in this second part.)

Once you’ve done this, create another switch and edit the code to test it. We’re nearly there!

In this section, we’re essentially getting our circuit to emulate the behaviour of a half-adder circuit using a behavioural description

The complete circuit diagram (click to enlarge)

approach.

(I’ll let the philosophers amongst you ponder whether we’ve in fact created a ‘true’ half adder… or merely a simulation of one #spooky).

Once your entire circuit is wired up, it’s time to upload the final logic!

```int buttonOne = 5;
int buttonTwo = 7;

void setup() {
pinMode(buttonOne, INPUT);
pinMode(buttonTwo, INPUT);
pinMode(10, OUTPUT); //Carry, GREEN LED
pinMode(12, OUTPUT); //Sum, RED LED
}

void loop() {

if(inputA == 1 && inputB == 0 || inputA == 0 && inputB == 1){
digitalWrite(10, HIGH);
digitalWrite(12, LOW);
}
else if(inputA == 0 && inputB == 0){
digitalWrite(10, LOW);
digitalWrite(12, LOW);
}
else if(inputA == 1 && inputB == 1){
digitalWrite(10, LOW);
digitalWrite(12, HIGH);
}

}```

Simply flash this to your Arduino board, and with some luck you’ll have a fully functional 1-bit binary half adder! Here’s my hastily-drawn explanation of the finished product, to give you an idea of what I’ve done with my LED setup:

If all is well, you should be able to mess around with your new creation! Check your logic using the truth table below: