Arduino AND Gate Truth Table: Wiring, Code and Simulator

To build this Arduino AND Gate Truth Table project you need an Arduino Uno and an AND gate, a 220 Ω resistor and a green LED. Pins 2 and 3 drive an AND gate; pin 4 reads its output and an LED shows it. Make the 6 connections in the wiring table, upload the Arduino C++ code, and watch the truth table print. Swap the AND for an OR or XOR gate and compare.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1AND GateOUT is HIGH when all inputs are HIGH. Set 2–8 inputs in the inspector.
1220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1green LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.

Wiring

#FromTo
1Arduino Uno pin 2AND Gate A
2Arduino Uno pin 3AND Gate B
3Arduino Uno pin 4AND Gate OUT
4AND Gate OUT220 Ω resistor leg 1
5220 Ω resistor leg 2green LED anode (A, long leg)
6Arduino Uno GNDgreen LED cathode (C, short leg)

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add an AND gate, a 220 Ω resistor and a green LED from the parts palette.
  3. Wire 1. Connect Arduino Uno pin 2 to AND Gate A.
  4. Wire 2. Connect Arduino Uno pin 3 to AND Gate B.
  5. Wire 3. Connect Arduino Uno pin 4 to AND Gate OUT.
  6. Wire 4. Connect AND Gate OUT to 220 Ω resistor leg 1.
  7. Wire 5. Connect 220 Ω resistor leg 2 to green LED anode (A, long leg).
  8. Wire 6. Connect Arduino Uno GND to green LED cathode (C, short leg).
  9. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  10. Run it. Press Run. Watch the truth table print. Swap the AND for an OR or XOR gate and compare.

Arduino C++ code

// Logic gates: pins 2 and 3 feed an AND gate; pin 4 reads its output back.
// The sketch walks the truth table and prints what the gate answers.
void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, INPUT);
  Serial.begin(9600);
  Serial.println("A B | A AND B");
}

void loop() {
  for (int a = 0; a <= 1; a++) {
    for (int b = 0; b <= 1; b++) {
      digitalWrite(2, a);
      digitalWrite(3, b);
      delay(5);
      Serial.print(a);
      Serial.print(" ");
      Serial.print(b);
      Serial.print(" | ");
      Serial.println(digitalRead(4));
      delay(600);
    }
  }
  Serial.println("---");
}

What to try

Watch the truth table print. Swap the AND for an OR or XOR gate and compare.

Frequently asked questions

How do I wire the AND Gate Truth Table project on an Arduino Uno?

Make 6 connections: Arduino Uno pin 2 to AND Gate A; Arduino Uno pin 3 to AND Gate B; Arduino Uno pin 4 to AND Gate OUT; AND Gate OUT to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to green LED anode (A, long leg); Arduino Uno GND to green LED cathode (C, short leg).

What parts do I need for this Arduino project?

An Arduino Uno and an AND gate, a 220 Ω resistor and a green LED.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

Pins 2 and 3 drive an AND gate; pin 4 reads its output and an LED shows it.

More Arduino Uno projects