10. Logic Gate Implementation

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

I will connect two switches as inputs, read their states, calculate the AND, OR, NAND, and NOR results in code, and light up one of four LEDs to display each result.

 
 

 

 

 

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

Was this helpful?
Upvote
Downvote