All submissions

Logic Gate Implementation

Solving Approach

How do you plan to solve it?

 
The logic gates' states are managed by two switches (Input A and Input B),i use the internal pull-up resistors (set by INPUT mode) and connect them to digital pins 2 and 3 of the Arduino. The matching input pin is pulled LOW when a button is pressed. The internal pull-up resistor pulls the pin HIGH when it is not pressed, the AND, OR, NOR, and NAND gates' outputs are represented by four LEDs attached to pins 4, 5, 6, and 7. The corresponding LED will turn on (HIGH) when the gate condition is satisfied; if not, it will remain off (LOW).

Gate AND:Only when both inputs are 1 (i.e., both buttons are pressed) is the output 1 (LED ON). The output is 0 (LED OFF) for all other combinations.

OR Gate: When at least one input is 1 (i.e., at least one button is pressed), the output is 1 (LED ON), the output is 0 (LED OFF) if neither button is pressed.

NOR Gate: When both inputs are zero, the output is one (LED ON), the output is 0 (LED OFF) if any of the buttons are pressed.

NAND Gate: When at least one input is zero, the output is one (LED ON), only when both buttons are pressed does the output become 0 (LED OFF).
 

 

 

Code

/*Paste your code here*/
// Define input and output pins
const int inputPinA = 2;  // Switch A connected to pin 2
const int inputPinB = 3;  // Switch B connected to pin 3
const int ledPinAND = 4;  // LED for AND gate on pin 4
const int ledPinOR = 5;   // LED for OR gate on pin 5
const int ledPinNOR = 6;  // LED for NOR gate on pin 6
const int ledPinNAND = 7; // LED for NAND gate on pin 7

void setup() {
  // Initialize the input pins as input
  pinMode(inputPinA, INPUT);
  pinMode(inputPinB, INPUT);
  
  // Initialize the LED pins as output
  pinMode(ledPinAND, OUTPUT);
  pinMode(ledPinOR, OUTPUT);
  pinMode(ledPinNOR, OUTPUT);
  pinMode(ledPinNAND, OUTPUT);
  
  // Start the serial communication (optional for debugging)
  Serial.begin(9600);
}

void loop() {
  // Read the state of the input switches
  int inputA = digitalRead(inputPinA);
  int inputB = digitalRead(inputPinB);

  // Perform logic operations and control the LEDs
  
  // AND gate: LED ON when both inputs are HIGH
  int andResult = inputA && inputB;
  digitalWrite(ledPinAND, andResult);

  // OR gate: LED ON when at least one input is HIGH
  int orResult = inputA || inputB;
  digitalWrite(ledPinOR, orResult);

  // NOR gate: LED ON when both inputs are LOW (opposite of OR)
  int norResult = !(inputA || inputB);
  digitalWrite(ledPinNOR, norResult);

  // NAND gate: LED ON when at least one input is LOW (opposite of AND)
  int nandResult = !(inputA && inputB);
  digitalWrite(ledPinNAND, nandResult);

  // Optional: Print input and logic gate results to Serial Monitor for debugging
  Serial.print("Input A: ");
  Serial.print(inputA);
  Serial.print("\tInput B: ");
  Serial.print(inputB);
  Serial.print("\tAND: ");
  Serial.print(andResult);
  Serial.print("\tOR: ");
  Serial.print(orResult);
  Serial.print("\tNOR: ");
  Serial.print(norResult);
  Serial.print("\tNAND: ");
  Serial.println(nandResult);

  // Delay to avoid overwhelming the serial monitor
  delay(100);
}

 

 

 

Output

Video

Add a video of the output (know more)

 

 

 

 

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!