2 Bit Adder to Decimal Learning Tool/calculator
by 762490 in Circuits > Arduino
19 Views, 0 Favorites, 0 Comments
2 Bit Adder to Decimal Learning Tool/calculator
In this instructable you will learn to recreate a 2 bit adder which can convert to decimals. This circuit can also be used as a binary number learning game. This project uses a 2 bit adder to take a binary input and then an Arduino to convert the binary input into a decimal and then displaying the decimal onto a seven segment display. If you go into the code you can turn this into a 2 bit binary addition learning game as when you add 2 2 bit numbers correctly to a decimal sum a buzzer will play noise signifying you are correct and then if you go back into the code you can switch to a different decimal number and learn different 2 bit binary combinations to add to a decimal sum.
To utilize the 2-bit adder, The adder takes two binary numbers as inputs, here we will refer to them as A1 A0 and B1 B0. In both numbers the A0 and B0 are the least significant bits while A1 and B1 are the most significant bits. In decimal representation, A and B could represent the decimal values of 0-3. Thus, when both A and B are added together the maximum possible decimal sum is as follows (3+3=6). In binary representation this sums to be represented as 110. The adder uses several logic gates to produce the final output (the binary result) via both sum and carry. This demonstrates through using binary numbers how binary calculations are processed by digital circuits.
A binary number is produced as an output from the adder and sent to a seven-segment display. The code then converts the binary number to the combination of segments that results in the display showing the decimal value of the sum. A slide switch is provided to turn the system on and off when the switch is disabled, the display and remaining circuitry receive no power.
We use a Light Dependent Resistor (LDR) to determine how bright or dim a Seven Segment Display is based on what the surrounding light levels are. An LDR has a lower resistance when there is a bright light source and therefore allows more current to pass through which makes the LED portion of the display very bright; on the other hand, in lower light conditions or at night, the LDR's resistance increases causing less current to go through and thus dimming the LED portion of the display. This is an example of how an Analog Sensor can control or indirectly affect a Digital Output.
After comparing the output of the 2-bit adder to a predetermined target value, the program will check to see if they match. If so, and if the slide switch is in the on position, the code will turn the buzzer on to show that the result is correct otherwise, the buzzer will remain off.
Supplies
1 × Quad AND Gate - $0.97
1 × Quad OR Gate - $0.97
1 × Quad XOR Gate - $1.61
1 × DIP Switch (SPST ×4) - $0.98
1 × Slide Switch - $0.99
1 × Arduino Uno R3 - $15.99
1 × Anode 7-Segment Display - $0.89
1 × Photoresistor (LDR) - $1.07
1 × Piezo Buzzer - $0.95
3 × Red LEDs - $9.28
4 × 1 kΩ Resistors - $8.91
3 × 330 Ω Resistors - $9.11
2 × 10 kΩ Resistors - $9.07
1 × Wires - $14.99
1 × Breadboard - $4.99
Wiring 2-Bit Adder
NOTE: If you do not know how to use logic gates, it is advisable that you learn about the different logic gates (AND, OR and XOR) before creating this circuit. It will help you to know how these gates work before obtaining a complete understanding of how the 2-bit adder operates.
To create a 2-bit adder, we require two half adders to sum two 2-bit binary numbers. Each half adder receives two inputs and generates two outputs: a sum output (S) and a carry output (C). In this circuit, we are going to use the first half adder to sum the least significant bits of binary numbers, A0 & B0. The output of this will be the first sum, S0, and the carry output of this half adder will be C0.
In the second half adder, we will add the most significant bits (A1 & B1) and add the carry (C0) from the first half adder. The outputs from this will be the second sum, S1, and the final carry of this whole adder will be C2. The final binary sum of the two input numbers will be represented by S0, S1 & C2. Since each of the two input numbers can be in decimal from 0 to 3, the maximum output that the adder could produce would equal 6 (Most Significant Bit = 1, Second Most Significant Bit = 1).
Prior to constructing the breadboard circuit, plan the arrangement of the components to fit the 2-Bit Adder and accommodate the 7-Segment Display and other components. Organizing the circuit will lead to fewer wiring mistakes and assist in locating troubles that occur. Using Tinkercad or another simulation program to view the layout before physically building the circuit will aid in seeing how everything fits together on the breadboard.
When beginning the assembly of the circuit, ensure all Logic Gates, Switches, LEDs and Arduino Boards are connected to their respective Power and Ground Rails. The 4-position DIP Switch should be connected to Ground through 10kΩ Pull-down Resistors which provide stability to the input value of the DIP Switch. The LEDs should be wired in series with 330Ω Resistors to limit current and avoid possible damage. It is important to orient all Logic Gate ICs with the Notch facing the same direction for uniformity when wiring the circuit.
Build the Adder incrementally, beginning with S0, followed by S1 and then C2, and for each step, test the output for accuracy. You can use the LEDs to help confirm that the Displays of Sum and Carry produce the correct results given the combination of inputs used. Testing during each step provides the opportunity to address any errors immediately rather than wait until the entire project has been assembled. If your tested output does not match what you expected it to be, make sure to double-check the connections of all of your wiring and Logic Gate Inputs.
Follow the tinkercad wiring above for the 2 bit adder wiring.
Adding 7-Segment Display & LDR
The Arduino is utilized in this circuit for the individual control of each segment of the seven segment display by designating each segment 'a' through 'g' and connecting them to an Arduino Digital output pin. The following shows how these pins are defined in the code and should be connected using the diagram above:
a = Arduino Pin 2
b = Arduino Pin 3
c = Arduino Pin 4
d = Arduino Pin 5
e = Arduino Pin 6
f = Arduino Pin 7
g = Arduino Pin 8
An LDR is used to automatically adjust the brightness of the 7-Segment Display in response to ambient light levels. The LDR consists of two terminals - one connected to the positive power rail and the other connected to the negative power rail through a 10kΩ resistor. When ambient light levels increase, the resistance value of the LDR decreases, resulting in more current flowing through the entire circuit.
The LDR also supplies power to the common pins of the 7-Segment Display. Thus, in this configuration, the LDR must supply the current required to power the display before it can connect to the common pins. Increased current will result in increased brightness on the display, while decreased current will result in decreased brightness on the display. Therefore, this feature allows the 7-Segment Display's brightness to be automatically adjusted based on its surroundings without any additional programming effort
Connect a buzzer's positive terminal to your arduino's pin 11 and negative terminal to the negative power rail through a 10kΩ resistor. Lastly Connect the A0(on arduino) to S0, A1(on arduino) to S, and A2 to C(on arduino)
Coding Variables
Variables a, b, c, d, e, f, and g are the representations of the seven segments of a 7-Segment Display. Each variable holds the Arduino Digital Pin Number that controls that particular segment of the display. For instance, segment a corresponds to pin 2, segment b corresponds to pin 3, and segment g corresponds to pin 8. When the code changes the pin state from HIGH to LOW, it either turns ON or OFF the respective segment of the display.
The pin number that controls the piezo buzzer is assigned to the variable buzzer (pin 11), and the code uses this variable to turn ON or OFF the buzzer, such as during the time when it detects a valid sum.
The code uses the variable sum to keep track of the results from the binary addition calculations done by the 2-Bit Adder. The sum starts at 0 and is then updated as the program continues to execute. The code compares the sum to a target for the 7-Segment Display as well as determining if the corresponding buzzer needs to make a sound.
Coding Setup
The setup() function allows the Arduino to control all the components of your project and perform the function of everything connected to it. This function sets the pin that connects to the buzzer as an output, allowing the Arduino to activate the buzzer as needed for alerts or other notifications. In addition, this function configures the pins which connect to the segments (from a to g) of the 7-segment display as outputs, allowing the Arduino to activate the appropriate segments in order to display numbers correctly. The setup() function also calls Serial.begin(9600) to initiate the Arduino's ability to use Serial to communicate by sending data to the computer at a baud rate of 9600 (9600 bits per second), enabling the user to monitor and debug the values in their programs. Thus, the setup() function prepares the Arduino to display the sum produced by the 2-bit adder on the 7-segment display, respond to light level changes captured by the LDR, and sound the buzzer if the correct sum is produced.
Functions used to control a 7-segment display will enable you to light up and turn off certain segments so that digits 0 through 6 can be represented on a 7-segment display. In each function, the digitalWrite() function is used to turn each segment (a-g) On or Off (HIGH or LOW), resulting in a sequence that matches the associated number. The zero() function lights up all segments except g so that the numeral “0” can be displayed, while the one() function lights only the segments required to make the numeral “1.” This pattern continues for all of the numerals, so when the two() or five() functions are called, the correct segments of the 7-segment display will light up to make the correct digit. There is also a Serial.println("h") statement in the five() function, which will send the letter “h” to the Serial Monitor This is used for debugging purposes and can be removed if you would like.
Coding Loop Function
In the loop() function, you continuously read the analog input values from three terminals (A0 - A2) which represent the binary inputs for your 2-bit adder (sum). The loop() will calculate a sum of either 1, 2 or 4 based on which of the analog pins are currently being monitored as active (input). At the conclusion of the calculation, both the most current read values as well as the calculated sum are printed to the Serial Monitor, allowing you to debug the data being read by the circuit. Once the sum has been calculated, the code uses a series of if and else if statements to call the appropriate function zero() to six() that will display the correct number on the 7-segment display. The code also controls the buzzer located on pin 11, which produces a sound only when the sum is equal to 5. Otherwise, the buzzer will not sound . After each display and buzzer action takes place, there will be a delay of one second, allowing you to visually/aurally confirm each output.
Finalized Project
Here is How to use the circuit.
- Turn the slide switch ON to power and activate the circuit.
- Set the binary inputs for the two numbers using the input switches or sensors.
- The Arduino reads the inputs, adds the two 2-bit numbers, and displays the decimal result on the 7-segment display.
- Observe the display brightness adjusting automatically depending on the surrounding light due to the LDR.
- If the input combination adds up to the correct value programmed in the code, the buzzer will sound to indicate success.
- Turn the slide switch OFF when finished to disable the circuit and save power.