Simulating Logic Gates
This project is a simple way of using the Arduino to simulate the behaviour of logic gates. Logic gates are explained on this page.
The project does not actually carry out the function of the logic gate, just turns a light on or off based on one or two inputs. Effectively showing the truth table for a given logic gate.
You Will Need
- 2 x LEDs Yellow
- 1 x LED Red
- 3 x 330 Ohm Resistors
- 2 x 10 KOhm Resistors
- 2 x Pushbuttons
- Jumper Wires
The yellow LEDs will be used to represent the input values, the red LED the output. The colour choice is up to you but it helps to make them different.
Making The Circuit
The following diagram shows how to connect the components,
The pushbuttons are connected with 10K resistors to GND. An LED is placed on the same row as the connections to the input pins (8 & 9). This gives us a push-to-make configuration for the pushbuttons. It also means that we don’t have to write any code to make the two input indicator LEDs light up. You can test the button connections early on. The Yellow LEDs in this diagram should light when the buttons are pressed and be off when not.
Programming The Arduino
int pinOut = 7;
int pinA = 8;
int pinB = 9;
boolean pinAState = digitalRead(pinA);
boolean pinBState = digitalRead(pinB);
pinOutState =pinAState & pinBState;
This is the basic code to demonstrate the behaviour of an AND gate. When uploaded, you should be able to see the output LED light when, and only when, both pushbuttons are pressed at the same time. Notice that boolean values are used here.
If you replace the line, pinOutState =pinAState & pinBState; with one of the following lines, you can simulate the other common logic gates. For the NOT gate, you only have a single input.
pinOutState = pinAState | pinBState;
pinOutState = !pinAState;
pinOutState = pinAState ^ pinBState;
pinOutState = !(pinAState & pinBState);
pinOutState = !(pinAState | pinBState);
The challenge for this circuit is to come up with a much more interesting way to display the information. One way might be to do away with the pushbuttons for input and display a truth table. The following image shows how you might arrange the LEDs,
The red LEDs would be the inputs, the yellow the outputs. The left hand column of red LEDs would represent the state of input A, the right hand column input B. No resistors are shown here but you would need one per LED that can be on. Some of the red LEDs would never be on – these would simply be left unconnected. None of the red LEDs would need to be connected to Arduino pins either. Just connect the ones that represent a 1 to 5V on the anode with a resistor from the cathode to GND.
Each of the yellow LEDs needs a resistor and a connection to a different Arduino pin. If you can lay your hands on another breadboard, you could put the yellow output LEDs on that breadboard and line it up to make a nice table.
This would show the full truth table for a single logic gate. What would be nice would be to have some way to switch between logic functions using components. Some clever use of a pushbutton would allow you to move between logic functions.
For more detail: Simulating Logic Gates
This Post / Project can also be found using search terms:
- Arduino Logic Gates