4 Bit Binary Adder

by 728055 in Circuits > Arduino

48 Views, 0 Favorites, 0 Comments

4 Bit Binary Adder

IMG_1722.jpg

This Instructable explains and teaches the use of my project, how to wire it, how to code it, as well as a brief description of the purpose of both the code and materials used. To begin with, the purpose of this Instructable is to a 4 bit binary adder that displays the results in bits and in numbers on a LCD, and can be turned on and off using a slide switch. This is similar to a calculator, however this one will only calculate in binary inputs. It does this by using a slide switch, and a LED connected to the slide switch to if it is turned on when the slide switch moves to the rights, and turns off when the slide switch moves to the left. two dip switches to get the inputs as ones or zeros like in binary. Next, the calculations are done by the us of 1 half bit adder and 3 full bit adders, which consist of two 'AND' gates, two 'XOR' gates, and one 'OR' gate. After these gates perform there calculations the Arduino excepts the results as either one or zeros and then displays it on the LCD using the code from the Arduino.

Supplies

Screenshot 2024-06-18 at 12.27.30 PM.png

Brief Explanation of Each Components Use in General and in the Build

IMG_1722.jpg
  • Dip Switch

What it does - Dip Switches allow for the user to have control over where electricity flows in the circuit.

Purpose in the build - The purpose is to act as a the turning on the ones and zeros.

  • 'AND', 'XOR', and 'OR' Gates

What it does - These gates get true and false as inputs and based on that chips truth table it outputs a true or false

Purpose in the build - Is to perform the calculation from the user inputs of true and false, in turn providing the result to act as a binary adder.

  • LED 

What it does - LEDs which are also known as light-emitting diode are objects that emit light in different colours depending on the colouring of the LED. They only function when there is current flowing though it.

Purpose in the build - As mentioned in the "What it does" section it only turns on when a current is flowing through it, therefore it acts as a visual indicator for the device being on or off.

  • Slide Switch 

What it does - A slide switch is the median for an individual to turn on a device or off a device physically, through allowing or preventing a current to go through it.

Purpose in the build - The purpose in the build is to allow an individual to turn on or off the device physically.

  • Resistor 

What it does - Resistors are used to regulate or limit the flow of current a device is receiving.

Purpose in the build - The purpose in the build is to limit the flow of current to the LED, so the LED won't receive enough electricity to fry it, meaning the light source inside of the LED does not work anymore.

  • LCD

What it does - LCD's function is to display text or numbers on a screen.

Purpose in the build - This allows for the results of the calculation to be displayed to the user.

Wiring

IMG_1722.jpg

Connecting power and ground:

  1. Place the slide switch near the top of the breadboard
  2. Connect a red wire from the 5 V pin to the terminal one of the slide switch
  3. Connect the common of the slide switch to the horizontal row that has a positive symbol at the bottom using a red wire
  4. Connect terminal two to horizontal row that has a negative symbol using a black wire
  5. Connect a black wire to the GND on the arduino to the horizontal row with a negative symbol using a black wire at the top of the breadboard
  6. Connect the 220 Ω from the same row as the slide switch that has a negative charge
  7. Connect the green from the same row as the slide switch that has a positive charge
  8. Add an LED so the side that has a bent piece of metal is on the row of the green wire and the straight piece of metal is on the row with the resistor
  9. Add a red wire from the positive symbol on to right to the one on the left at the bottom of the breadboard
  10. Add a black wire from the negative symbol on to right to the one on the left at the bottom of the breadboard


Connecting Dip Switch:

  1. Place a dip switch near the top of the breadboard
  2. Connect the positive end of the dip switch to the power line on the breadboard
  3. Connect a black wire from opposite row that has a negative symbol then connect the ground from there with 10 kΩ to the dip switch


Connecting 'XOR' Chip:

  1. Place the 'XOR' next to the dip switches, make sure the that the 'XOR' chip's semi circle is facing the dip switches
  2. Connect a red wire from the horizontal row that has a positive symbol to the row that the first pin on the top left is on
  3. Connect a black wire from the horizontal row that has a negative symbol to the bottom right
  4. Repeat this step with another chip being placed from the first one with a one chip separation so that the 'AND' chip can fit there
  5. Connect the input wires with the first two pins and the third pin being the output
  6. Connect the output wire to where necessary wether it be another gate or to the Arduino itself


Connecting 'AND' Chip:

  1. Place the 'AND' next to the right of the 'XOR' gates, make sure the that the 'AND' chip's semi circle is facing the dip switches
  2. Connect a red wire from the horizontal row that has a positive symbol to the row that the first pin on the top left is on
  3. Connect a black wire from the horizontal row that has a negative symbol to the bottom right
  4. Repeat this step with another chip being placed from the first one with a one chip separation so that the 'AND' chip can fit there
  5. Connect the input wires with the first two pins and the third pin being the output
  6. Connect the output wire to where necessary, which would be to other gates


Connecting 'OR' Chip:

  1. Place the 'OR' next to the right of the 'AND' gates at the very end, make sure the that the 'OR' chip's semi circle is facing the dip switches
  2. Connect a red wire from the horizontal row that has a positive symbol to the row that the first pin on the top left is on
  3. Connect a black wire from the horizontal row that has a negative symbol to the bottom right
  4. Repeat this step with another chip being placed from the first one with a one chip separation so that the 'AND' chip can fit there
  5. Connect the input wires with the first two pins and the third pin being the output
  6. Connect the output wire to where necessary wether it be another gate or to the Arduino itself


Connecting 'AND' Chip:

  1. Place the 'AND' next to the right of the 'XOR' gates, make sure the that the 'AND' chip's semi circle is facing the dip switches
  2. Connect a red wire from the horizontal row that has a positive symbol to the row that the first pin on the top left is on
  3. Connect a black wire from the horizontal row that has a negative symbol to the bottom right
  4. Repeat this step with another chip being placed from the first one with a one chip separation so that the 'AND' chip can fit there
  5. Connect the input wires with the first two pins and the third pin being the output
  6. Connect the output wire to where necessary, which would be to other gates


Connecting LCD:

  1. Connect power from the breadboard to the LCD where it says VCC using a male to female jumper wires
  2. Connect ground from the breadboard to the LCD where it says GND using a male to female jumper wires
  3. Connect pin A4 to the LCD where it says SDA using a male to female jumper wires
  4. Connect pin a5 to the LCD where it says SCL using a male to female jumper wires

Coding

Screenshot 2024-06-18 at 12.29.08 PM.png

Initializing the variables:

You will need to download a library for the LCD to work making it more efficient and easier to code. Access this link to download the library.


Initializing the variables:

To begin with we will be initializing our variables means to give a name to a variable so the computer knows to what you are referring to. Firstly, the variables int Sum0 = 7;, int Sum = 5;, int Sum2 = 4;, int Sum3 = 3;, and int C4 = 7;. These are the variables used to track if the calculations resulted in a one or zero. Followingly, int bit1 = 0, int bit2 = 0, int bit4 = 0, int bit8 = 0, int bit16 = 0; to store if the results are a one or zero and displays them on the LCD. Subsequently, int a = 0, int b = 0, int c = 0, int d = 0, and int e = 0; are initializations to store the numerical value that the binary digits have. Lastly, int totalSum = 0; stores the numerical result after adding all of the numerical values of the binary digits.


void setup:

Now that we have initialized are variables, we need to perform our void setup, which runs the code placed in here once at the beginning of our program, as well as stating the pin modes, this means if they are either a input when we give something to the computer and output means giving something to us. Firstly, Serial.begin(9600); is used to print the results so that the Ardunio can compare them and see if they match in the calculation function. Followingly, lcd.init();, lcd.setCursor(0,0);, lcd.backlight();, and lcd.display(); is used to initialize the display, set the location of where the text will be written, and turn on the backlight of the LCD. Next, pinMode(Switch, INPUT); is used to track when the switch is on or off.


void calculation:

This void allow the computer to know what to do when these phrases are said in the void loop. As one may guess by the name of the function, its purpose is to perform calculations that will be displayed. The code for forward it works by using numerous if and else statements, so that if a Ardunio pin is high the bit and numerical value are changed, else they remain at zero. At the end the totalSum variable adds all of numerical values.


void loop:

The void loop functions to repeat the code that is within it. It will consistently repeat what it does and to flick a switch on the LCD, then perform the calculations when it is called, and finally display the answer on the LCD. This will all work only if the switch is high meaning that it is on.

Errors an Individual May Make

Screenshot 2024-06-18 at 12.28.42 PM.png
  • Not turning on the device using a slide switch
  • Faulty equipment, like a burnt out LED
  • Putting wires in the wrong pins
  • Did not download the file for the LCD to work
  • Forgot to add a { or } at the start of end of a void function
  • Forgetting to end each line with ';'