How do a diode and a transistor work?
A diode is normal built by touching two different pieces of semiconductor together to form what is called a “p-n junction.” Semiconductors are materials that are in between good conductors and good insulators. A pure semiconductor is a very poor conductor of electricity. With careful chemical processing, a semiconductor can be made into n-type semiconductor—a semiconductor that contains a small number of mobile electrons that permit it to carry electric current. With different processing, a semiconductor can also be made into p-type semiconductor—a semiconductor that contains a small number of mobile holes for electrons that permit it to carry electric current. It may seem strange that a hole for an electron can allow electricity to flow, but imagine a highway packed with cars (electrons) bumper to bumper. If there are a couple of empty places (holes) in the bumper-to-bumper traffic, then cars (electrons) can rearrange enough that the traffic can flow. Both mobile electrons and mobile holes allow these two chemically treated semiconductors to carry current.
When an n-type semiconductor touches a p-type semiconductor, a diode is formed. The mobile electrons at the edge of the n-type semiconductor flow over the boundary (a p-n junction) and fill the mobile holes at the edge of the p-type semiconductor. This rearrangement creates a depletion region—a region near the p-n junction in which there are neither mobile electrons nor mobile holes. This depletion region normally won’t carry electricity at all. But if you push electrons onto the n-type semiconductor, they will flow toward the p-n junction and replenish the missing mobile electrons. As these mobile electrons approach the p-n junction, they will repel the electrons that are filling the mobile holes on the p-type side of the junction and reopen the mobile holes. Electrons will begin to cross the p-n junction and current will flow through the diode. However, if you push electrons onto the p-type semiconductor, they will fill even more of the mobile holes there and the depletion region near the p-n junction will grow larger and more uncrossable. No current will flow through the diode. Thus a diode (a p-n junction) only carries current in one direction—electrons can only flow from the n-type semiconductor side to the p-type semiconductor side.
There are many types of transistors, so I will only describe an n-channel Metal-Oxide-Semiconductor Field Effect Transistor, or n-channel MOSFET. In this device, three layers of semiconductors are sandwiched together: an n-type piece (the source), a long, thin p-type piece (the channel), and another n-type piece (the drain). Two p-n junctions form between these three components and, since the junctions are arranged in opposite directions, they completely block current flow from the source through the channel to the drain. But a metal surface (the gate) that’s separated from the channel by an extremely thin layer of oxide insulator can control the number of electrons on the channel material. If you put even a tiny bit of positive charge on the gate, it will attract electrons onto the channel and turn it from p-type semiconductor to n-type semiconductor. When that happens, both p-n junctions vanish and current can flow from the source to the drain. The MOSFET goes from being an insulating device when there is no charge on the gate to a conductor when there is charge on the gate! This property allows MOSFETs to amplify signals and control the movements of electric charge, which is why MOSFETs are so useful in electronic devices such as stereos, televisions, and computers.