LED stands for Light-emitting Diode. The image, sourced from Wikipedia, shows a typical 5mm LED. A standard, 3mm or 5mm, one-color LED has two leads. Typically, one is longer than the other. The longer one is the anode, the other is the cathode. The anode (the longer lead) should be connected to the positive lead of the circuit, and the cathode should be connected to the negative or ground lead. If both of the LED's leads are the same length (which sometimes happens if the LED is removed from a circuit or the leads shortened to make it fit), then look for a flat side at the bottom of the LED's case. Look at the LED straight on from the top and you'll see one edge is not round but flat. The lead nearest the flat edge is the cathode, or negative lead.
What Is a Diode?
A diode is an electronic component that only allows current to flow in one direction. The LED is a diode, so it only allows current to flow in one direction, meaning that, when properly connected, it will shine a light. If incorrectly connected, it will not light up.
The concept of the LED was invented in 1927, but first real-world production didn't start until 1962 (created by Texas Instruments in 1961). One of the earliest uses was not as a visible light, but rather to use its ultra-violet light feature, which was used for television remote controls.
(external link: Wikipedia)
I, personally, started experimenting with them back in the mid-1970s. Nowadays, LEDs are everywhere. In model railroading they are used as headlights on locomotives, interior lights for cabooses, passenger cars, and structures, and inside of trackside signals. The infrared ones are used to detect the presence of something breaking the infrared light-beam, for example, as a circuit that responds to a train passing through (to close and open crossing gates, for example).
LED Sizes
The most common LEDs are the ones shown in this image, which from left to right are the 5mm, 3mm, and 1.8mm LED bulbs. The dimensions indicate the diameter of the LED's housing. The lip around the bottom of the LEDs housing can be used as a detent to keep the LED from falling out of an enclosure (e.g. a box, a panel). These LEDs come in a wide variety of displayed colors. Their entire housing reflects the light that the LED produces, but its brightest point is head-on from the top of the housing.
The next most-common LED you will find are the tiny SMD LEDs. SMD stands for Surface Mount Device. These are intended to be directly soldered to a printed circuit board and take up very little space. These are very bright, especially considering their small size. They come in a variety of colors. Their dimensions are indicated based on their length and width in ten-times-the-millimeter size. For example, ones that are marked as "5050" indicates that their housing is 5.0mm square. "3528" indicates a 3.5mm x 2.8mm LED, for example. These LEDs mostly aim their light straight out.
Some Calculations Required
If you hook up an LED directly to a battery or a power supply, it will yield a quick flash of light and nearly instantly burn up. This is because an LED will consume however much current the circuit can provide to it. The concept of restraining oneself from taking in too much current doesn't exist. The LED is a glutton!
To prevent that from happening, the circuit must provide a current-limiting resistor. To calculate the correct resistor value, we need to use Ohm's Law, which states that voltage ("volts") is equal to current ("amps") times resistance ("ohms"). Re-arranging this formula to calculate resistance, we need to know the power supply's voltage, the voltage drop that the LED causes in the circuit, and the current we are going to supply to this LED.
Determining the Voltage Value
The power supply's voltage should be a known factor, e.g. from a battery, or a power supply's transformer, or a DCC decoder's output. The LEDs voltage drop should be stated on the LED's package or on its manufacturer's web site (also called the LED's "forward voltage"). If you hook up more than one LED in series (i.e. back-to-back), you must make the LED's voltage drop in the formula be that of all of the LEDs added together (could be different values if you use different types of LEDs). If you put two or more LEDs in parallel (i.e. next to each other, with their positive leads all connected together and their negative leads all connected together), then the voltage drop will be as if there is only one LED.
Determining the Current Value
The resistor is supposed to control the current through the LED(s). Too little current and the LED will not light up or be very dim. The more current, the brighter the LED. If your LED is being run near its maximum at all times, it will have a shortened lifetime. If you give it too much current, it will burn up (immediately, soon, or a bit down the road, depending on how much is too much). This means that you can use the current to control the LED's effective brightness. For example, inside of a structure, you might want to make the LED be a bit dimmer, meaning give it less current. However, if you are modeling a football stadium in a night scene, you may want to have the LEDs run with a near-maximum current level.
Determining the Power Rating
The diagram above uses some reasonable, conservative values that can be used to calculate the value of the resistor. Please note that there are two measurements related to a resistor, the second being the amount of heat that the resistor can handle. For it to hold back the current that the circuit provides to restrict the current actually reaching the LED, the resistor gets hot. Its packaging is the determining factor for how much heat it can handle. This power value is given in watts. Its formula is:
P = I x I x R
or, the current (in amps) through the resistor (and given to the LED), squared, times the value of the resistor (in ohms). So, in the diagram above, the calculated wattage is
P = 0.01 x 0.01 x 860 = 0.086 watts
which means that a 1/8th Watt (0.125) resistor is large enough for this circuit. Remember that, if you do not calculate this rating correctly, and you mount the resistor against or near the plastic of a structure or an engine or car, it could get hot enough to actually melt or deform your item. So, be sure to calculate it correctly, and perhaps test you circuit out in the open, let it run for a while, and see if the resistor gets hot. Resistors don't cost very much, but your structure, car, or engine does!
An Alternative To All That Calculating!
There is a device, called "CL2", created in 2015, that provides a fixed (+/- 10%) current to the circuit that it is attached to. It is a temperature-compensated, constant-current LED driver IC that operates on any voltage between 5 and 90 volts, and outputs a consistent 20mA of current. This is a safe and reasonable current to provide a regular LED. My photo shows this installed on a breadboard. So, instead of a resistor that has to be accurately calculated and adjusted if the voltage changes, you have a small transistor-looking component that can handle a good-sized range of voltages to safely drive an LED. This photo shows a 9V battery connector, some wires to connect everything, a red 3mm LED, and the CL2.
The device has an accuracy of 10%, so the actual output can be anywhere from 18mA to 22mA. If for some reason you need more current, you can parallel (i.e. connect their legs together) two or more of these devices. Each device increases the current throughput by 20mA, so two of them would yield 40mA.
The TO-92 casing version is the one I am using (there are two others). The component is manufactured by Microchip out of Chandler, Arizona. It has three legs to conform to the TO-92 standard, but the center leg is not connected to anything. You connect the VA leg to the positive side of the circuit and the VB to the negative/ground side of the circuit.
(external link: Microchip CL2)
This photo shows the circuit connected to a 9-volt battery, and it lights up the LED just fine. I also tested it with hooking of three LEDs in series (back-to-back) and that worked great as well. So, if you work with LEDs, I would recommend that you buy a package of these CL2 component, as in a lot of scenarios they are smaller than a resistor, and are basically fool-proof to use.
Dual-color LEDs
There are also versions of the LED that can display two colors. These colors vary, but typical ones display a red and a green color. These types of LEDs are great for simulated railway signal lamps, turnout position indicators, or road traffic lights in our model railroads. They are available in the same physical sizes as the single-colored ones mentioned above.
These LEDs come with two or three leads. The two-leads version shows one color when the current is applied in one direction, and the other color when the current is applied in the other direction.
The three-leads version has a center lead that should be connected to the positive side of the circuit. The color is controlled by which of the outside leads are connected to the negative/ground side of the circuit. This type of LED is typically used in conjunction with a turnout control device (e.g. a Circuitron Tortoise switch machine), because those devices usually have one lead active when the turnout is in one position and another one active when the turnout is the other position. DPDT mechanical switches can also be used flip the LED's color.
Infrared LEDs
Again, also available in the same physical sizes as the single-colored ones mentioned above, these LEDs do not appear to shine a light that humans can see. The infrared spectrum allows light-based communication to take place without it visually disturbing us. These are commonly used in old television sets where a remote control was used to change the channel on a TV, for example. The remote control had to be aimed at a spot on the TV to be able to work. Because LEDs can be turned on and off at incredible speeds, these infrared LEDs can communicate specific information to such devices as TVs. These LEDs are also used in security systems to detect movement.
While today's remote controls use radio-frequency (which does not require the aiming of the remote at the device being controlled), which includes BlueTooth, we can still use infrared LEDs on our model railroads, to help us detect movement or the presence (or lack thereof) of trains on tracks.
You need two components to be able to make practical use out of infrared LEDs. You need the LED that shines the infrared light, and you need a matching sensor that can detect this infrared light. The LED looks like a regular clear LED. The second component is called a "photo transistor", which may or may not look like an LED. The latter's housing varies from manufacturer to manufacturer, but the basic operation is the same. The photo shows such a pair that I used in an N-scale ten-level helix that I had built back in 2002. The LED on the top is the infrared LED (a 5mm one), while the one at the bottom is the receiving photo transistor (which is housed in an LED-style, 3mm housing).
A pair of these were positioned next to each level in the helix (the indents in the wooden supports), so that I could have a small circuit that would light up a regular LED when a train passed through a pair of the infrared components. It allowed me to visually see the progress a train was making through the helix. I had put a set of these on the front edge of the helix and one at the back edge. The helix was covered by Masonite hardboard, so I couldn't see the trains on the track, hence the need for this external collection of indicators. You can see the 20 regular LEDs at the front of the helix. One nice feature was that I could monitor multiple trains in the helix, e.g. one going up and one going down, just by watching the progression of the LEDs lighting up and turning off.
This photo shows a top-down view of that helix, with the infrared LEDs installed in the front (not visible) and the back of the helix (where you see all of the blue connectors).
Note that the current needed to run an infrared LED is usually higher than that of a regular LED (e.g. 50mA, a value that would destroy a regular LED). An infrared LED has a lower "forward voltage" value, typically. So, you will want to consult the documentation that comes with the LED or from the manufacturer's web site when calculating the appropriate current-limiting resistor.
Hooking up an infrared LED to a circuit is identical to that of a regular LED. The only difference is its physical installation. It has to be aimed, as accurately as possible, at the receiving photo transistor for it to work reliably. This, of course, is highly dependent on how you plan to use the pair. For example, if you are wanting to detect the presence of a train on a hidden staging track, you will want to put this pair of components aimed at each other, but at an angle across the track (or tracks) that you want to monitor. The angle is important, as you do not want the pair to shine at each other when the train happens to stop such that the infrared beam is aiming between two coupled cars. By placing them at an angle across the track, you avoid that possibility.
Second, if you installed the infrared LED/photo transistor pair in a lit room, you have to be very accurate in the aim, as the room's lighting will have a reducing effect on the infrared beam. If at all possible, make the space immediately surrounding the two components as dark as possible. You could hide them inside of a structure, or in a bush, or some other trackside feature.