10. Logic Gate Implementation

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 We solve this by connecting the LED to the GPIO pins 5, 6, 7, and 8. And the switches to the pins 3 and 4. Using DigitalRead(), we can read the state of the switches, as we know the truth table for each of the gates. When the corresponding switch activates, the corresponding LED's glow.

 


 

 

 

 

Code

/*Paste your code here*/

const int ledAND = 5;
const int ledOR  = 6;
const int ledNAND = 7;
const int ledNOR  = 8;

const int button1 = 3;
const int button2 = 4;

void setup() {
    pinMode(ledAND, OUTPUT);
    pinMode(ledOR, OUTPUT);
    pinMode(ledNAND, OUTPUT);
    pinMode(ledNOR, OUTPUT);

    pinMode(button1, INPUT_PULLUP);
    pinMode(button2, INPUT_PULLUP);

    Serial.begin(115200);

    digitalWrite(ledAND, LOW);
    digitalWrite(ledOR, LOW);
    digitalWrite(ledNAND, LOW);
    digitalWrite(ledNOR, LOW);
}

void loop() {
    // Read switches (invert logic because INPUT_PULLUP)
    int A = (digitalRead(button1) == LOW) ? 1 : 0;
    int B = (digitalRead(button2) == LOW) ? 1 : 0;

    // Logic gates
    int andGate  = A && B;
    int orGate   = A || B;
    int nandGate = !(A && B);
    int norGate  = !(A || B);

    // Drive LEDs
    digitalWrite(ledAND, andGate);
    digitalWrite(ledOR, orGate);
    digitalWrite(ledNAND, nandGate);
    digitalWrite(ledNOR, norGate);



    delay(200);
}




 

 

 

Output

Video

Add a video of the output (know more)

 

Image

 

 

 

 

Was this helpful?
Upvote
Downvote