All submissions

Logic Gate Implementation

Solving Approach

I connected 2 switches as inputs and 4 LEDs as outputs in Wokwi. The switches act like binary inputs (0 or 1). In the code, I used logical operators to make each LED represent a gate: AND, OR, NAND, and NOR.

 
 

 

 

 

Code

const int inputA = 12;
const int inputB = 13;

const int pinAND  = 7;
const int pinOR   = 4;
const int pinNAND = 6;
const int pinNOR  = 5;

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

  pinMode(pinAND, OUTPUT);
  pinMode(pinOR, OUTPUT);
  pinMode(pinNAND, OUTPUT);
  pinMode(pinNOR, OUTPUT);
}

void loop() {
  bool A = (digitalRead(inputA) == LOW);
  bool B = (digitalRead(inputB) == LOW);

  digitalWrite(pinAND,  (A && B)  ? HIGH : LOW);
  digitalWrite(pinOR,   (A || B)  ? HIGH : LOW);
  digitalWrite(pinNAND, (!(A && B)) ? HIGH : LOW);
  digitalWrite(pinNOR,  (!(A || B)) ? HIGH : LOW);
}


 

 

 

Output

  • If both switches are OFF → AND=OFF, OR=OFF, NAND=ON, NOR=ON
  • If one switch is ON → AND=OFF, OR=ON, NAND=ON, NOR=OFF
  • If both switches are ON → AND=ON, OR=ON, NAND=OFF, NOR=OFF

Video

Add a video of the output (know more)

 

PHOTO OF OUTPUT

 

 

Submit Your Solution

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