2-Bit Adder With 7-Segment Display and LCD
by Ana Jeyakumar in Circuits > Arduino
492 Views, 6 Favorites, 0 Comments
2-Bit Adder With 7-Segment Display and LCD

Hello, my name is Ana Jeyakumar, and this Instructable will walk you through my final project, a 2-Bit binary adder with a seven-segment display and an LCD screen. The project might seem complicated at first glance, but once you understand the basic concepts, it will be easy to catch up on! Here is a simple explanation below:
The project works by taking 2 binary numbers (a way of representing numbers using only two digits: 0 and 1) as the input. Each number is made from 2 digits; one number is made from input A1 and A0, and the other from input B1 and B0. These numbers can be either 0 or 1, and depending on the combination, each pair represents a number from 0 to 3. The project adds the two numbers together using logic gates (AND, OR, XOR), which act as electronic switches that decide whether an output is on or off (1 or 0) based on the inputs they receive. The sum of these numbers (from 0 to 6) is shown in two ways. The decimal sum is displayed on a seven-segment display (the one you see on digital alarm clocks or calculators), while the binary number is displayed on a small screen called an LCD (liquid-crystal display), with supporting LEDs that act as visual aids. This project demonstrates how computers use binary numbers and how basic circuits can carry out actual math operations and present the results in an understandable manner.
After some preliminary research and discussions with teachers, I decided that I wanted to work with logic gates, which were my favorite component throughout the semester. The 2-Bit adder was appealing because it produced a sum with a visual output, which I believe was a good way for beginners like myself to learn and retain information! Although I struggled with wiring and coding at first, this Instructable will show you how to build and code this project in a few simple steps. Let's get started, starting with the materials!
Supplies


For this project you will require (links are included):
1x Standard 4 Position DIP Switch
1x Common Anode 7-Segment Display
1x Wire Pack
1x Breadboard
The project's estimated costs are shown above!
NOTE: If you already have certain components, they are optional.
Wiring 2-Bit Adder on Breadboard


NOTE: If you're not familiar with logic gates, I HIGHLY recommend reading this Instructable on logic gates before getting started.
Remember that a 2-Bit adder is simply just two half adders. By looking at the half adder schematic, it takes inputs A and B and gives you a sum (S) and a carry (C). In this 2-Bit adder, the first half adder adds the A0 and B0 inputs (least significant bits), resulting in S0 and C0. Then, using a second half adder, it adds A1, B1, and the carry (C0) from the first half adder. This gives you S1 and C2 (final carry). S0, S1, and C2 are outputs that show the sum of any possible combinations.
Before you begin placing your components and wiring, you should plan the layout of your 2-Bit adder on your breadboard to ensure there is enough space for the seven segment display. Despite the large amount of breadboard space, cramming everything together would not look good and could be difficult to rewire if mistakes are made. I recommend using a program like Tinkercad to do this. Once you have your initial layout, start building!
First, start by connecting your components to power and ground. The Standard 4 Position DIP Switch should be connected to ground using four 10kΩ resistors, as well as the 3 LEDs using 330Ω resistors. Remember to connect your logic gates to the power ground as well, ensuring the gate is placed with the groove on the left side to make wiring easier. Here is a link for a XOR gate pinout (it's the same for AND and OR). Use the circuit diagram above to build your adder step by step (first S0, then S1, and finally C2) testing each output as you go. This is essential because you can correct errors in writing immediately rather than finishing and attempting to decode the error, which is time-consuming. Testing can be done with the LEDs; try out a combination using this interactive CircuitVerse schematic to see if the correct output lights up for each input combination. If it doesn’t work, try rewiring and double-check that your wires and components are in the right pins.
Adding 7-Segment Display and LCD



Once your adder works, you're going to need to connect wires from the LED outputs (S0, S1, and C2) directly to 3 pins of the Arduino to display the result. Start by wiring the outputs of the adder to the Arduino pins listed below. These connections are important in the upcoming coding sections, which will be further explained. Here are the pin connections for the outputs:
S0 = Pin 3
S1 = Pin 4
C2 = Pin 5
The next step is to wire your 7-segment display and LCD. The seven-segment display has seven LEDs arranged to form numbers, with each segment labeled a through g. Two pins are connected to power via 330Ω resistors. To control the display, connect each segment pin (a, b, c, d, e, f, and g) to the corresponding Arduino digital output pin. In this way, you can set the corresponding Arduino pin HIGH or LOW in your code to turn each segment on or off, displaying the corresponding adder sum (further explained in the coding section). Make sure the wires are in the correct pins and are deep enough in the Arduino, as if they are not securely connected, they will flash and become glitchy. Here are the pin connections for the seven-segment display:
Segment a = Pin 9
Segment b = Pin 10
Segment c = Pin 13
Segment d = Pin 12
Segment e = Pin 11
Segment f = Pin 8
Segment g = Pin 7
The next step is to add your LCD. An LCD (Liquid Crystal Display) is a screen that shows text, numbers, and symbols so that users can see output straight from an Arduino. In this case, the 16 x 2 shows 16 characters across each line, with two rows. To simplify writing, the LCD uses a I²C interface using two communication pins, Serial Data (SDA) and Serial Clock (SCL). The SDA pin is used to send and receive data between the Arduino and the LCD (letters, numbers, and commends), whereas the SCL pin carries the clock signal (which indicates when to read data sent via the SDA line). Both are connected to analog pins: in the example above, the SDA line (white wire) is connected to A4, and the SCL line (brown wire) to A5. Here are the pin connections for the LCD:
SDA Line = Pin A4
SCL Line = Pin A5
Coding (Variables)

Before starting the code, I will leave the file for the code at the end of this section so you can follow along:
At the very top of the code, special tools called libraries are included using (#include <Wire.h>) and (#include <LiquidCrystal_I2C.h>). In short, they help the Arduno do more difficult tasks without the need for excessive and complicated wiring, and in this project they help the Arduino communicate with the LCD. The line (LiquidCrystal_I2C lcd(0x27,16,2);) sets up the screen so we can display messages, like binary sum, later using commands.
Next, we create variables to give names to different pins on the Arduino, which helps the Arduino remember important information. For example, instead of remembering pin numbers, like pin 9, names are given (int a = 9;) so the code is easier to write, read, and understand. This part is very easy, since we already know what pins everything goes to, we simply write variables using this format:
int (variables name) = pinumber;
EXAMPLE FROM CODE:
int a = 9;
int S0 = 13;
The other variables near the bottom are a bit different. The first variable (int = sum 0) creates a variable called sum that starts at 0, it is used to store the total value to the binary output (1 +2 +4). The second variable (int ones, twos, fours) adds these numbers together to find the total binary sum.
Downloads
Coding (Setup Function)


Now that you've initialized all of your variables, you must set them to input or output in order to connect the pin in void setup(). This section of the code tells the Arduino which pins are being used and what their functions are; they either send signals (OUTPUT) or read signals (INPUT). We also switch on the LCD screen. In this case, the variables S0, S1, and C2 will be set to INPUT. These will receive signals and allow the Arduino to determine what number the user is entering in binary. Pins a through g control each segment of the seven-segment display, which lights up depending on the digit to be displayed. These are set to OUTPUT because the Arduino determines whether the segments are on or off. To set up the pins, follow this format:
pinMode(Variable name),(Input or Output);
After that, the next section of the code might seem lengthy and scary, but it is actually quite simple. We are developing functions for each number (1-6) that will allow the Arduino to turn on the 7-segment display to show the corresponding digit. The code's digitalWrite() function switches a display segment on or off based on whether the variables (a through g) are set to LOW or HIGH. This may seem contradictory, but LOW indicates that the segment lights up, whereas HIGH indicates that it remains off. Let's use a section of the code to demonstrate. This is the function for the number 0 in the seven segment:
void zero() {
digitalWrite (a,LOW);
digitalWrite (b,LOW);
digitalWrite (c,LOW);
digitalWrite (d,LOW);
digitalWrite (e,LOW);
digitalWrite (f,LOW);
digitalWrite (g,HIGH);
}
The command digitalWrite() turns each segment on or off:
LOW = turn on the segment
HIGH = leave the segment off
Use this link to see which letters correspond to each segment; this function must be performed for all numbers 0-6! As you can see for the number 0, all segments except the middle line are set to LOW (light up), whereas the middle segment (g) is set to HIGH (stays off).This is significant because the variable number can be called on instead of repeating the digitalWrite(); commands later in the code! NOTE: there is also an error function in the code; it is used to see if there are any problems with displaying the digits on the 7-segment diplay when the code runs!
(Loop Function)


Next is the loop function, where the code runs over and over to display the binary sum both on the seven-segment and LCD screen. The first part of the code is quite simple and it's for the LCD. On the LCD, it displays the message "enter your two #" on the first (top) line, while the line below shows "in binary". It tells the user to enter their binary number, lets it display on the screen for 3 seconds for the user to read, then clears to show the sum.
NOTE: This sentence had to be written in two lines on the LCD because each line has a limited amount of space. If I typed "enter your two numbers in binary", it would only display the first line of the LCD, cutting off the sentence. This is why the sentence is separated into two lines!
The number being entered in binary is determined by using the LED input pins (S0, S1, and C2). S0 represents the 1s place, S1 the 2s place, and C2 the 4s place. Each pin represents a distinct binary value. If a pin is ON, its value is included in the sum: 1 for S0, 2 for S1, and 4 for C2. The final number that displays on the screen is then determined by adding these values together using the subsequent line (sum = fours + twos ones;).
Then, the code checks what the value of sum is, and displays the result accordingly. If the sum is zero, "binary answer is: 000" is displayed on the LCD screen, and the zero(); function is used to display the number 0 on the 7-segment display. The same applies for other numbers between 1 and 6: the screen displays the digit and the corresponding binary number. For example, if the sum is 5, 101 is displayed on LCD, and the five(); function is called on for the seven-segment display. There is a 2 second delay so the user can read the answer, and then the LCD clears to display the new sum that the user enters. The error()” function is to only run if the sum is outside of the expected range (0-6). For example, suppose the sum is zero, this part of the loop runs;
if (sum == 0){
lcd.setCursor(0,0);
lcd.print("binary answer is:");
lcd.setCursor(0,1);
lcd.print("000");
zero();
delay(2000);
lcd.clear();
}
LCD will display “binary answer is” on the first line and “000” on the LCD, demonstrating the binary sum. At the same time, the function zero(); is called and the seven-segment will display the digit sum. Both will show for 2 seconds, then clear. The loop repeats constantly, so the Arduino can respond to new inputs anytime.
Video Demonstration
Once you've finished the code, you're done! Congrats on building the project! Attached is a video link which shows the demonstration and all possble combinations.
Link for Project Explanation