# LED connection tutorial

## Description

Light Emitting Diodes can be used for various applications, annunciator panels, instrument lighting, gear indicator lights, and more. With the hw_led_set API function you can control the brightness of an LED, from off (0) all the way to on (1).

LED's are semiconductor (diodes), which mean that they conduct in one direction. So what is the correct direction? LED's usually have one lead that is a little bit longer, this is the positive (anode) side, the other lead is the negative (cathode). They are usually also marked with a flat spot on the housing, this is the negative (cathode) side.

When an LED is conducting, it has close to zero resistance. This means that when the LED is conducting, it creates a shortcircuit. Therefore you will always need to use a resistor in series with the LED, or you'll destroy it. And that is where it gets a little bit tricky... LED's also have something called 'forward voltage'. This can be seen as the voltage that is 'lost' on the LED. A usual forward voltage is 1.7 volts, but this varies between LED's and I advice you to look it up in the specifications document of your LED. But lets say that your LED has a forward voltage of 1.7 volts, a typical (forward) current of 20 milliamps (mA), and we drive it with 5 volt from an Arduino or Raspberry Pi. To find out what resistor to use, you first subtract the 1.7 volts from the 5 volts:

5 - 1.7 = 3.3

Now we can use the formula R = U / I (resistance is voltage divided by current), and this makes:

3.3 / 0.02 = 165

We would have to use a resistor of 165Ω (Ohm). But since a 165Ω resistor does not exist, we round it up to the closest resistor value available, which is 180Ω when following the E24 series (E-series).

### Placing LED's in series

Now the next explanation is optional, but it is certainly interesting for you to know. You can also place LED's in series, this can save you a lot of components when you have to drive multiple LED's together. With 5 volts you cannot do much, but with for example 12 volt, you can have up to 7 LED's in series (1.7 volt forward voltage). Let's use the 5 volt from the previous example. If we divide 5 by 1.7 we get 2.9. This means we can place two LED's in series (always round to the lowest number). With two LED's this creates a forward voltage of 3.4 volt (1.7 x 2), and if we do the same calculation:

5 - 3.4 = 1.6

We use the same R = U / I formula:

1.6 / 0.02 = 80

Now we will have to use a resistor of 80Ω. This one also does not exist, so we use 82Ω.

This schematic overview shows both the single LED and the LED in series example.

## Examples

Now that we have that out of the way, we can start with some examples. We're going to look at two examples: LED's linked to the panel brightness, and an annunciator panel.

### Panel brightness

In this example we will control the brightness of the LED with the panel brightness data coming from the simulator, we do this for both FSX/Prepar3D and X-Plane. However, FSX and Prepar3D by default do not have a variable that sets the panel brightness, therefore we use an LVar from A2A in this example.

``````-- Create a new LED, and have it off by default

function new_data_xpl(brightness)
-- X-Plane returns a value from 0 to 1, so we can directly use this value. Note that X-Plane returns an array, so we pick the first value out the array.
hw_led_set(led_panel, brightness)
end

function new_data_fsx(brightness)
-- FSX and Prepar3D by default have no value that controls the panel brightness. Most good quality add-on aircraft will do, like A2A. Therefore in this example we use the A2A LVar for the glareshield.
-- The LVar value ranges from 0 to 32, so first we convert it to a value from 0 to 1. Then we use this value to set the LED brightness.
brt_val = (1 / 32) * brightness
hw_led_set(led_panel, brt_val)
end

xpl_dataref_subscribe("sim/cockpit2/electrical/panel_brightness_ratio", "FLOAT", new_data_xpl)
fsx_variable_subscribe("L:GlareshieldLightKnob", "Number",``````

### Annunciator panel

We will create a simple annunciator panel that shows a warning light when the oil pressure is too low and when the bus voltage gets too low. We want these LED's only to turn on when there's actually bus power, so we take the bus voltage into account as well.

``````-- Create two LED's, and have them off by default
led_oilp = hw_led_add("Oil pressure warning", 0.0)
led_altv = hw_led_add("Alternator voltage warning", 0.0)

function new_data_xpl(bus_volts, oil_pressure)
-- We'll say if there's more than 10 volts, there's sufficient bus power. We turn this into a boolean for easier use.
bus_power = bus_volts >= 10

-- Now we can set the LED's
-- We say that a the light should turn on when the oil pressure drops below 20 PSI
if bus_power and oil_pressure < 20 then
hw_led_set(led_oilp, 1)
else
hw_led_set(led_oilp, 0)
end

-- The voltage annunciator should turn on when the voltage drops below 20 volts
if bus_power and bus_volts < 20 then
hw_led_set(led_altv, 1)
else
hw_led_set(led_altv, 0)
end
end

function new_data_fsx(bus_volts, oil_pressure)
-- We'll say if there's more than 10 volts, there's sufficient bus power. We turn this into a boolean for easier use.
bus_power = bus_volts >= 10

-- Now we can set the LED's
-- We say that a the light should turn on when the oil pressure drops below 20 PSI
if bus_power and oil_pressure < 20 then
hw_led_set(led_oilp, 1)
else
hw_led_set(led_oilp, 0)
end

-- The voltage annunciator should turn on when the voltage drops below 20 volts
if bus_power and bus_volts < 20 then
hw_led_set(led_altv, 1)
else
hw_led_set(led_altv, 0)
end
end

xpl_dataref_subscribe("sim/cockpit2/electrical/bus_volts", "FLOAT",
"sim/cockpit2/engine/indicators/oil_pressure_psi", "FLOAT", new_data_xpl)
fsx_variable_subscribe("ELECTRICAL MAIN BUS VOLTAGE", "Volts",
"GENERAL ENG OIL PRESSURE:1", "PSI", new_data_fsx)``````

You can easily extend this script to your liking. Low fuel, vacuum, etc... Just add more datarefs or variables, and add more LED's.