I built more logic gates… It was shocking!

The surprising difference between toy cars and transistors

--

Welcome back everybody! If you read my last blog you would know I built a computer out of Hot Wheels cars. For some context I recommend you read my last few blogs, but if not, I’ll sum up here. Basically, all computers are made out of logic gates. There are only seven basic logic gates and these logic gates allow your computer to function. Logic gates can be used to add, subtract, multiply, etc. Normally logic gates are made out of transistors, diodes, and other electrical components. Technically, though, they can be made out of anything, which is the interesting part! I used Hot Wheels as an analogy for electricity flowing through wires to build logic gates. Because of the limitations of mechanical computers, there were a few logic gates which I couldn’t build, like the NOT gate.

So, I decided to go back to basics and build real logic gates, using transistors, diodes, and a Raspberry Pi. The Pi acted as an easy 5v DC power source. I got this idea from a camp I went to, where we built a few simple logic gates. I expanded on this at home by building more logic gates like the NOR and NAND gates. The only 2 gates I wasn’t able to build were the XOR and XNOR gates. I tried many times to build these gates and I came very close but never quite got it. In fact, it was while I was trying to build an XNOR gate I got shocked by my transistor! Now, I’m no electrical engineering expert, I’m just a kid, so I don’t know exactly what happened, but I do know that I was touching a transistor while current was flowing through the circuit. I wasn’t injured, but it definitely shocked me 😆.

Anyways, here is the electrical diagram and truth table for a NOT gate:

When I say “1” in an input, I am referring to a button being pressed. When I say “1” as an output, I am referring to an LED turning on.

To understand how this logic gate (or any logic gate for that matter) works, we need to understand how a transistor works.

A transistor has 3 pins, as seen in the image above. When looking at the flat side of a transistor, the pin on the right is the input, the pin in the middle is the base, and the pin on the left is output. With the base pin we can use the transistor as a switch. Let me explain: if current is passed to the input pin, the transistor doesn’t let it through to the output, unless a current is applied to the base pin. The transistor acts almost like a gate, allowing current to flow through only when current is sent to the base pin. If we attach a switch to the base pin, we can pass current through the transistor whenever we want! This is the principle which all the logic gates I’m going to show you work on.

Now that we understand how transistors work, let’s get back to that logic gate diagram!

As we can see, there are 3 paths for the current to take from the power source: to the button, the transistor, and the LED. When current first flows into the circuit, it takes all three paths equally. The current is then stopped by the button (assuming it’s not pressed) and the transistor. Because MOST current takes the path of least resistance, the current flows through the LED and completes the circuit, lighting up the LED. Since the button is hooked up to the base pin of the transistor, when we press it, current is applied to the base pin and the transistor allows current to flow through and complete the circuit. Because this path doesn’t have a resistor, this is the path of least resistance and the current mostly takes this path, which avoids the LED, so it doesn’t light up.

Most logic gates work on this same principle of changing the path of least resistance. Current doesn’t ALWAYS take the path of least resistance. Most of it does, but some current takes every path. This is why building these logic gates doesn’t always work perfectly. Anyways, back to logic gates!!

Here is a video of the NOT gate working:

The next gate I built was an OR gate. Here is the diagram:

This circuit is slightly more complicated because it uses 2 transistors for 2 inputs. As we can see, current flows to the transistors and the buttons, but gets stopped by all of them initially, meaning current doesn’t get to the LED. When we press either of the buttons, current is sent to the base pin of the transistors, allowing current to pass through and light up the LED. Either of the transistors can be on for current to pass through to the LED. That’s why this is an OR gate.

Here is a video of the OR gate in action:

The next logic gate I built was the AND gate, and this one I built at home by myself. This is what it looks like:

As we can see, current passes through the LED, but it doesn’t light up because the transistor blocks it and doesn’t let it complete the circuit. If we press only one button, the circuit still isn’t complete because the other transistor is still blocking the current. Only when we press both the buttons is the circuit complete. This is why this is an AND gate.

Here is a video of it:

The next gate I built was a NOR gate. This gate I built without any online diagrams. I reasoned that a NOR gate is just a NOT gate except with 2 inputs instead of 1. Here is the diagram I came up with:

This gate is very similar to the NOT gate. If none of the buttons are pressed, the LED turns on, because the current can’t flow through the transistors. Once we press any of the buttons, a current is now applied to the base pin of the transistor, which allows current to flow through the transistor. Because the path through the transistor has less resistance than the path through the LED, most of the current takes the path through one of the transistors, which avoids the LED.

Here is a video of the gate:

This gate doesn't work perfectly, but there is a clear difference between the 2 states.

The 5th and final logic gate of the day is the NAND gate. Basically, it’s the opposite of an AND gate. Here is the diagram:

In this circuit, the LED will light when none of the buttons are pressed, because that is the path of least resistance. Before any buttons are pressed, current can’t flow through the transistor, so it flows through the LED, which lights it up. If we only press one button, one of the transistors opens up, but the other transistor is still closed, meaning that the path of least resistance is still the path of the LED. Only when both buttons are pressed is when both the transistors open up, changing the path of least resistance from the LED to the transistors, so the LED doesn’t light up.

Something interesting happens when building the actual NAND gate:

If you look closely you can see that when only 1 of the buttons is pressed, the brightness of the light is slightly changed. If you look at the logic table for the NAND gate, you will see that the light shouldn’t be affected when only one button is pressed. Why is this happening? Well, I’m not exactly sure, but I’m pretty sure it has something to do with how electricity doesn’t always take the path of least resistance. Most electricity does, but some electricity takes every single path. Opening up one transistor might slightly decrease the resistance of that path (even though there is another transistor blocking the path), so less electricity might go to the LED and some more might go to the blocked path of the transistor. It’s really interesting to observe the weird stuff which electricity does!!

The only 2 gates I wasn’t able to build were XOR and XNOR. I came really close but I never got it. The logic made sense, but sometimes it doesn’t always work out perfectly on the breadboard. The XOR and XNOR gates are really complex gates to build. If any of you figured any of these gates out, please let me know down in the comments.

Thanks for reading everybody! I hope you enjoyed today’s blog and learned something new. Stay tuned for more “Computer Science Coolness” and other fascinating facts.

--

--

Ankit Durbha
Computer Science: A teenager’s perspective

I created the publication Computer Science: A teenager's perspective with the goal of creating a community of like-minded, technology enthusiastic peers.