Logic Gate Implementation

Solving Approach

How do you plan to solve it?

 
 Define the problem

  • Two buttons = inputs A and B
  • Four LEDs = outputs for OR, AND, NOR, NAND
  • Goal: Light the correct LED(s) based on button states.
  1. Wire the circuit
    • Buttons → Arduino pins D2 and D3 (other leg to GND, use INPUT_PULLUP)
    • LEDs → Arduino pins D4–D7 (cathodes to GND).
  2. Understand input logic
    • With INPUT_PULLUP:
      • Pressed = LOW → logic 1
      • Released = HIGH → logic 0
  3. Apply Boolean equations
    • OR = A + B
    • AND = A · B
    • NOR = ¬(A + B)
    • NAND = ¬(A · B)
  4. Build truth table

    A  B | OR AND NOR NAND
    -----------------------
    0  0 |  0   0   1    1
    0  1 |  1   0   0    1
    1  0 |  1   0   0    1
    1  1 |  1   1   0    0

  5. Test systematically
    • Case 1: no button → expect NOR & NAND ON
    • Case 2: only A → expect OR ON, NAND ON
    • Case 3: only B → expect OR ON, NAND ON
    • Case 4: both A & B → expect OR ON, AND ON
  6. Verify in Wokwi
    • Run the sketch
    • Press buttons one at a time, then both together
    • Compare observed LEDs with the truth table.

 

 

Code

/*Paste your code here*/
const int inputA = 2;   // Push button A
const int inputB = 3;   // Push button B

const int ledOR   = 4;  // OR gate output LED
const int ledAND  = 5;  // AND gate output LED
const int ledNOR  = 6;  // NOR gate output LED
const int ledNAND = 7;  // NAND gate output LED

void setup() {
  pinMode(inputA, INPUT_PULLUP);
  pinMode(inputB, INPUT_PULLUP);

  pinMode(ledOR, OUTPUT);
  pinMode(ledAND, OUTPUT);
  pinMode(ledNOR, OUTPUT);
  pinMode(ledNAND, OUTPUT);
}

void loop() {
  
  int A = digitalRead(inputA) == LOW ? 1 : 0;
  int B = digitalRead(inputB) == LOW ? 1 : 0;

  int OR_gate   = A || B;
  int AND_gate  = A && B;
  int NOR_gate  = !(A || B);
  int NAND_gate = !(A && B);

  digitalWrite(ledOR, OR_gate);
  digitalWrite(ledAND, AND_gate);
  digitalWrite(ledNOR, NOR_gate);
  digitalWrite(ledNAND, NAND_gate);
}


 

 

 

Output

Video

Add a video of the output (know more)

 

 

 

 

Upvote
Downvote

Submit Your Solution

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