Intro: Make an XOR Gate Out of Transistors
OR gates are very useful, but they have one odd feature that can work just fine, but in certain applications can cause problems. That is the fact that if both of the inputs are ones, then the output is also a one. If we had an application where we didn't want this, perhaps we were building an adder, we would use something called an Exclusive Or Gate, which is abbreviated XOR or EOR.
Step 1: Design
One way to achieve the XOR behavior is to take a regular OR gate, then deal with the case where both inputs are positive. If we tie an AND gate to the inputs, we can get a signal when that case shows up. We can then take that signal, invert it, then tie that and the output of the OR gate to another AND gate. This will make it so that whenever it is not the case that both inputs are on, the OR gate will simply pass through the second AND gate, but when both inputs go high, the first AND gate will shut off the second AND gate and keep the output off regardless of the state of the OR gate.
One adjustment I did end up making in the final circuit is switching the AND/NOT combination for a NAND gate, which is just an inverted AND gate. The way this works will become apparent later on.
Now lets write up that same schematic, but with transistors and resistors. The type of transistor I used is the 2N2222 BJT, which is fairly common (the 2N4401 and 2N3904 also work). I used 6 transistors, 3 20k ohm resistors, 3 47k ohm resistors, 1 510 ohm resistor, two pushbuttons and an LED. I chose these resistor values based on my 5v power source, and the 0.1mA, or 0.0001A minimum current for the 2N2222. if you use Ohm's law to calculate the correct resistance to ground for those values you get 50,000 ohms. 47k ohms is close enough for the lower NAND gate, but why the lower value for the OR gate, and the first input of the second AND gate? The reason is because the emitter of the transistors making up the OR gate are hooked up through the base of another transistor therefore running through a second resistor, not directly to ground. (The current limiting resistor of the LED is a low enough value that it is insignificant in this calculation).
Step 2: Adding Transistors, Buttons, and the LED
Step 3: Adding Resistors
Step 4: Adding Wires
The way I am powering my board is hooking up the power rails to a lab bench power supply set on 5v and 500mA max current. The same kind of input can be achieved by hooking up power to an Arduino's 5V and and GND pins, but really an 5v power supply works (though a current limited one is recommended to reduce the risk of blowing up components).
Step 5: Testing and Troubleshooting
Now that it is hooked up, I'll let you test your own. If one or the other of the buttons is pushed, the LED should light up. If both are pushed, however, then the LED will turn off.
- If one input seems not to work like it should, and the case where both inputs are on still provides a zero, check the voltage at the input of the AND gate that comes from the OR gate when that button is pushed. If it is low ( < 2V), reduce the resistance of the resistor going from the OR to the AND gate.
- If the gate still acts like just an OR gate, meaning that when both inputs are on the output is on, check the voltage coming into the input of the AND gate that comes from the NAND gate. If that is high when both buttons are pressed, make sure your transistors in the AND gate are working, and check the resistance from there to ground when both buttons are pressed. If that resistance is high, and/or that voltage is low, replace those two transistors, or reduce the resistance of the inputs to the NAND gates.
Step 6: Want More?
If you liked this Instructable go ahead and check out my book on Amazon called "The Beginners Guide to Arduino." It goes over basic circuitry principles as well as going over the C++ code used to program an Arduino.