4-Bit Adder/Subtractor

by Abdur-raheem in Circuits > Arduino

21 Views, 1 Favorites, 0 Comments

4-Bit Adder/Subtractor

IMG_0641.jpeg

In this project, I built a 4-bit Adder/Subtractor that uses Boolean logic in order to carry out basic addition and subtraction operations. The circuit utilizes logic gates to perform the desired operations and receives inputs using an Arduino Uno combined with a 4x4 matrix keypad.

Unlike many logic gate circuits that force the user to input binary numbers, I wanted to make my simple calculator more practical. To accomplish this, I allowed the user to input decimal numbers (0-9), which are then converted to binary by the Arduino so that the logic gate circuit could perform the required operations.

Not only is this circuit fun to build, but it is also an excellent project for aspiring computer engineers as it teaches the builder the fundamentals of binary logic and digital circuit design as well as the importance of making a product practical and usable. In the real world, practicality and usability are just as important as functionality.

Supplies

IMG_0647.jpeg

Logic of the Circuit

In order to build this circuit, it is important to first understand how the circuit works as well as what the logic behind the circuit is.

This circuit utilizes two major concepts, binary addition and two's complements.

A binary number is composed of bits/digits which can be either on or off, represented by 1 or 0 respectively. This is what computers and circuits understand and is what is used to tell a circuit what to do. By arranging these 1s and 0s in certain patterns we can represent different numbers. However unlike standard numbers, which have a 1s place, 10s place and so on, binary digits work in powers of 2 meaning the first digit represents the decimal value 2^0, the second represents 2^1, the third represents 2^2 and it continues in this sequence. This means that, to convert binary numbers to decimal numbers we must look at if they are 1 (on) or 0 (off) and then look at what number that digit represents. We only care about the digits that are 1. Once we identify which digits are 1 (on) and what decimal value each digit represents, we add the decimal values identified in order to get the decimal number that the binary number corresponds to.

Binary addition works by using some of the same principles ad decimal addition. In order to preform binary addition you add each digit to it's corresponding digit in the second number, 1st digit to 1st digit, 2nd digit to 2nd digit etc. However there is one important rule, since binary is only 1s and 0s we cannot simply add 1 and 1 to get 2. Instead we must carry the one over to the next digit, similar to adding numbers that add to 10, and leave the digit we carried from as 0, unless there was a carry form the previous digit, in which case we leave the digit as 1. Adding 1 and 0 however works the same as regular addition since 0 is off and that would not affect anything in the addition.

Two's complements is a method used for subtracting two binary numbers. This form of subtraction works by flipping the second numbers digits, 1 becomes 0, 0 become 1, and then adding the binary number 1 onto it and then simply adding the two numbers. Once the second number converted to it's two's complements form regular binary addition is carried out. However there is a catch, when observing the binary output of subtraction when using this method the highest digit that is possible to output will always be 1 (on) so it must be ignored/disregarded. The highest bit possible in the output will be one more than the amount of bits in the numbers you are adding or subtracting, for example if you are adding 4-bit numbers the highest bit possible in the outputs is the 5th bit, so when subtracting 4-bit numbers this 5th bit must be ignored as it is always going to read as 1 (on). Ignoring this will not mess with calculations as it is only possible for a 5th bit to be present during the adding of 4-bit because in addition you can get numbers higher than your inputs. In subtraction you cannot because when subtracting two numbers it is impossible to get a higher number than what was inputted.

Build the Logic Gate Circuit

schematic (1).JPEG
IMG_0646.jpeg

The logic gate portion of this circuit is a 4 bit adder. It links together multiple smaller circuits that individually add each binary digit in order to get the final output. It adds each digital individually similar to how addition is done on paper.

The resources here are to be used in order to complete the construction of the logic gate portion of this circuit. When building this circuit the inputs should be connected to the Arduino in the following way:

A1 - pin 2 B1 - pin 6

A2 - pin 3 B2 - pin 7

A3 - pin 4 B3 - pin 8

A4 - pin 5 B4 - pin 9

Above is a picture of the completed logic gate circuit as well as the circuit schematic. I would advise following the circuit schematic and information above rather than the picture as there are many wires and following it form a picture can get confusing.

As for the outputs we will be connecting them to LEDs. However for now just wire the outputs to 5 empty rails on the breaboard.

In order to gain a stronger understanding of how the circuit works watch this the following video.

4-bit Adder and Subtractor Circuit Explained

Note: In the video an XOR gate and an extra switch is used to convert the binary into two's complements but since we are using an Arduino to interpret the inputs, the conversion can be done in the Arduino. This is what I did for this circuit.

Connect the Logic Gate Circuit to LED Outputs

IMG_0645.jpeg

In order to visualize the output of the logic gate circuit we must wire the outputs of the circuit to LEDs. Since the logic gate circuit took up so much space on the breadboard we will connect the outputs of the circuit to LEDs which will be on another breadboard. To do this we will wire the LEDs as shown in the images above. The output from the circuit goes to LED positive. There should be five LEDs and five outputs, ensure you have the LEDs in the right order otherwise the binary output will be messed up. S1 goes on the far right and S5 goes on the far left (in the picture as this is how binary is read). LED ground goes to the 330 ohm resistor and then to ground. Additionally it is important that the grounds of the two breadboards are connected otherwise the LEDs will not turn on.

Connect the Keypad to the Arduino

IMG_0648.jpeg
Screenshot 2026-06-18 144735.png

After fully building the logic gate circuit we must now connect the input device to the Arduino. To do this we must connect the keypad to the Arduino as shown above. The connection is rather simple however it is important to connect the right pins together otherwise your Arduino will not read the right values when you press the buttons on the Keypad.

Important Note: Connect the keypad to the second Arduino as there are not enough pins to connect it to the first Arduino (with the outputs for the circuit)

Connect the Arduinos Together

Screenshot 2026-06-18 145106.png
IMG_0644.jpeg
IMG_0642.jpeg

After wiring everything for both Arduinos we must now connect the Arduinos together so that they can communicate and pass information to each other. To do this we will connect the Arduinos by wiring the following pins together:

A4 (SDA) - A4 (SDA)

A5(SCL) - A5(SCL)

GND -GND

For additional guidance refer to the above photos.

Code

Now that all the wiring is done we must now upload the code.

You will need to use the Arduino IDE to upload the code to the Arduinos and you will need to download two libraries.

  1. Keypad
  2. Wire

You will need to download the Keypad library however the Wire library is already installed once you have the Arduino Uni board installed as board. In order to download the Keypad Library follow these steps:

Open the Arduino IDE - Click on Tools - Click on Manage Libraries - Search for "Keypad" if it is not already installed, install it.

Once you have the Keypad Library downloaded you can upload the code.

Make sure you upload the correct code files to the correct Arduino.

Calculator Master is to be uploaded to the Arduino that is connected to the Keypad.

Calculator Slave is to be uploaded to the Arduino that is connected to the circuit. This Arduino will output the numbers in binary to the circuit

NOTE: All the code is fully commented and explained inside the code files.

Project Completed

IMG_0641.jpeg

Congrulations!!. You have built a simple calculator using logic gates.

If you want to check if it works watch this video and input the same numbers and see if you get the right outputs.

Working Video