All submissions

Solving Approach

How do you plan to solve it?

 

To demonstrate logic gates using an Arduino, you'll need to set up a circuit with two switches for inputs and four LEDs for outputs. The switches should be connected to the Arduino's input pins, while the LEDs should be connected to output pins. In the software, the Arduino will constantly read the state of the two switches, treating a pressed switch as a logical 1 and an unpressed one as a logical 0. It will then apply these values to the rules of four different logic gates: AND, OR, NAND, and NOR. The result of each logical operation will determine whether the corresponding LED turns on or off. For example, the AND LED will only light up when both switches are pressed, while the OR LED will light up if either or both switches are pressed. By following this logic in the code, you can use the hardware to visually represent the behavior of these fundamental digital gates.
 

 

 

 

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)

 

 

 

 

Submit Your Solution

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