Implementing Digital Logic

Digital Logic

Implementing “AND” and “OR” Gates with the chipKIT™

Introduction

In the Button-Controlled LED project, you used a button to turn an LED on or off. In this project, two buttons will be used to implement the digital logic “AND” function. The input logic will be controlled with a simple two-button circuit. The logic operation will then be implemented in the sketch that we upload to the chipKIT™ board. When a certain combination of the two buttons is pushed, a separate output pin on the chipKIT board is set to logic HIGH. This pin will be connected to another LED circuit so, when the pin is HIGH, the LED will light up.

In short, digital logic “reads” several inputs and sends a single resulting output signal. The output will be either logic HIGH or logic LOW, depending on the specific combination of input signals. For example, in an “AND” function reading two inputs, which we will be implementing, the output signal is logic HIGH if the first and the second inputs are both logic HIGH. If you want to know more about how digital logic works, follow the red tab at the right.

The circuit in this project will be similar to the circuit in the Button-Controlled LED project. If you are not familiar with using buttons in a circuit, visit the orange tab at the right.

Before you begin, you should:
  • Know how to build a simple circuit on a breadboard.
  • Understand how to use a button to control digital signals. (This will require an understanding of pull-up and pull-down resistors.)
After you're done, you should:
  • Understand simple digital logic, i.e., “AND” and “OR” functions.
  • Be comfortable programming conditional logic and using “if” statements.

Inventory:

Qty Description Typical Image Schematic Symbol Breadboard Image
2 Two-port button
1 LED
1 220Ω resistor
2 10 kΩ resistor

(10 kΩ = 10,000Ω)

Step 1: Assembling the circuit

In the circuit for this project, each button will allow a signal to be sent to a corresponding input pin on the chipKIT board. Looking at Fig. 1 below, when button A is not pressed, all current is flowing through the 10 kΩ resistor to ground. No current is reaching chipKIT pin 7. However, when button A is pressed, it allows current to flow through to the lower side of the button and to the chipKIT board. This drives pin 7 on the chipKIT board to logic HIGH. The same principles apply with button B.

The other portion of this circuit works as follows. Depending on the combination of buttons pressed, pin 11 will be logic HIGH or logic LOW. From that point, this is just a simple LED circuit. If pin 11 is HIGH, the LED will light up. The specifics of what determines the status of pin 11 will be covered when we go over the sketch that will be uploaded to the board.

Figure 1. Digital logic circuit.

Assembly Steps:

  1. Connect the chipKIT 3.3V pin to the red/“+” bus strip. Use a jumper wire to send 5.0V to the other red/“+” bus strip as well.
  2. Connect the chipKIT ground pin to the blue/“-” bus strip. Use a jumper wire to connect the other blue/“-” bus strip to ground.
  3. Place the two buttons so that they are connected across the valley of the breadboard. The buttons are labeled as “A” and “B”. This labeling is arbitrary but will make for clearer instructions in later steps.
  4. Connect the right side of each button to the 5.0V bus strip.
  5. Connect a 10 kΩ resistor to the left side of each button. These are being used as pull-down resistors.
  6. Connect the other ends of the 10 kΩ resistors to the grounded bus strip.
  7. On the other side of the breadboard's valley, connect the left side of button “A” to chipKIT pin 7.
  8. Connect the left side of pin “B” to chipKIT pin 6.
  9. Near the middle of the breadboard, place an LED with the cathode (shorter) to the left and the anode (longer) to the right.
  10. Connect the LED's cathode to the grounded bus strip.
  11. Connect the LED's anode to a 220Ω resistor. This is used as a current-limiting resistor.
  12. Connect the other end of the 220Ω resistor to pin 11 on the chipKIT board.

Figure 2. Circuit schematic.

The schematic for the circuit can be seen in Fig. 2 above.

Step 2: Programming the chipKIT Board and the Use of Variables

As was done in the Button-Controlled LED project, if statements and comparison operators will be used to control when the LED turns on or off. Within the if statements of this sketch, logical operators will also be used to implement the digital logic “AND” function mentioned above. Follow the green tab at the right to read about how to use logical operators in C/C++. For more information on if statements and comparison operators, follow the blue tab.

The rest of the code required for this sketch may begin to seem familiar. The chipKIT pins that will be used need to first be defined and then configured appropriately for input or output in the setup() function. The signals coming through the buttons will be read in the loop() function and the appropriate if statements will determine if the LED illuminates or remains off.

                    // Define button controlled pins
                    const int btnPinA = 6;
                    const int btnPinB = 7;

                    // Define the LED-controlling pin
                    const int LED_Pin = 11;

                    void setup ()
                    {
                        // Configure buttons for input
                        pinMode (btnPinA, INPUT);
                        pinMode (btnPinB, INPUT);
                        
                        // Configure LED pin for output
                        pinMode (LED_Pin, OUTPUT);
                    }

                    void loop ()
                    {
                        // Read the signals from each button
                        const int btnStateA = digitalRead (btnPinA);
                        const int btnStateB = digitalRead (btnPinB);
    
                        if ((btnStateA == HIGH) && (btnStateB == HIGH)) // BOTH buttons are pressed
                        {
                            digitalWrite (LED_Pin, HIGH); // LED turns on
                        }
                        else // Neither of the two buttons are pressed or only one is pressed
                        {
                            digitalWrite (LED_Pin, LOW); // LED is off
                        }
                    }

Test Your Knowledge!

The digital logic implemented above is “AND” logic; the LED illuminates only when button A and button B are pressed.

  • Modify the sketch so the circuit implements digital “OR” logic; the LED will illuminate if either one (or both) of the buttons are pressed.
  • Now try doing the inverse of this using “NOR” logic. In this case, the LED will be on when no buttons are pressed. Pressing one (or both) of the buttons will turn the LED off.


  • Other product and company names mentioned herein are trademarks or trade names of their respective companies. © 2014 Digilent Inc. All rights reserved.
  • Circuit and breadboard images were created using Fritzing.