> For the complete documentation index, see [llms.txt](https://docs.launchboard.app/cxd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.launchboard.app/cxd/internet-of-things-project/references-for-wiring-and-coding/led-light.md).

# LED Light

LEDs (light-emitting diodes) are small, bright, power-efficient lights commonly used in electronic products.

An LED light is a [polarized](https://learn.sparkfun.com/tutorials/polarity) part, meaning it has to be connected to a circuit in a certain way to work properly. Specifically, each LED has a positive leg and a negative leg. These can be identified visually by length: the negative leg has been made shorter.

![](https://576155698-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LDgX-RdB-DPZBPuXzJ1%2F-LDgX75Qrx69yS4tZ2_Z%2F-LDgXG_DxoQHVAXVJKo8%2Fled-polarity.png?generation=1527608057660946\&alt=media)

To make it easier to insert the LED into a breadboard, you can carefully bend the positive leg as shown, so both legs become the same length. You can still identify them visually: the straight leg is negative, and the bent leg is positive.

![](https://576155698-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LDgX-RdB-DPZBPuXzJ1%2F-LDgX75Qrx69yS4tZ2_Z%2F-LDgXG_FTApauvWzXwV5%2Fled-bent.png?generation=1527608057591861\&alt=media)

The negative leg of the LED has to be connected to GND (-). The positive leg is connected to any I/O pin, which will serve as the voltage source (+).

### Resistor

An LED can be easily burned out if it receives too much power. Therefore, a [resistor](https://learn.sparkfun.com/tutorials/resistors) **must** be used to help limit the amount of current flowing through the LED.

In order to use a resistor, you will need to bend both ends into 90° angles, so the resistor can be inserted into the holes on a breadboard.

![](https://576155698-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LDgX-RdB-DPZBPuXzJ1%2F-LDgX75Qrx69yS4tZ2_Z%2F-LDgXG_H3SFBCfQiCtK7%2Fresistor-bend.png?generation=1527608057572061\&alt=media)

The resistor is typically used in place of a jumper wire to connect the negative leg of the LED to GND (-).

| LED Light                     | Photon Pin         |
| ----------------------------- | ------------------ |
| Positive leg (long) = Power   | any I/O pin        |
| Negative leg (short) = Ground | GND using resistor |

[Experiment 1](https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-for-photon-experiment-guide/experiment-1-hello-world-blink-an-led) of the online SparkFun Photon Experiment Guide shows how to connect an LED. Here is the connection diagram for Experiment 1:

![](https://576155698-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LDgX-RdB-DPZBPuXzJ1%2F-LDgX75Qrx69yS4tZ2_Z%2F-LDgXG_JONumB7VdrpZE%2Fexperiment-1.jpg?generation=1527608057686334\&alt=media)

## Code for LED Light

### Library

The LED does **not** require any special code library.

### Global Variables

In the global variables, you should declare a variable to represent the specific pin on the Photon board that the LED is connected to. This will make it easier to understand your code (and easier to modify the code if you change which pin the LED is connected to). The example below declares a variable called "led" (but you could use a different variable name).

```java
// if necessary, change pin number to match your wiring
int led = D0;
```

If you are using **multiple** LED lights, then be sure to give each LED a unique variable name. Use variable names that will make sense when someone reads your code.

```java
// global variables
int redLed = D0;
int blueLed = D1;
```

### setup( ) function

Within the `setup()` function, you have to include a statement to set the pin mode for the LED pin variable:

```java
pinMode(led, OUTPUT);
```

If you are using multiple LED lights, be sure to set the pin mode for each LED's pin variable.

**IMPORTANT:** If you want an LED light to be **off** when your device first starts, then be sure to include code to do this within the `setup()` function (*after setting the pin mode*). Otherwise, the LED light might be on (at a low brightness) when the device starts, depending on which I/O pin the LED is connected to.

### Turn LED On or Off

An LED can be turned on or off by using a `digitalWrite()` statement. This can be included within the `setup()` function (only runs once), within the `loop()` function, or within a custom function.

To **turn on** an LED, set its LED pin variable to `HIGH`:

```java
// turn on led
digitalWrite(led, HIGH);
```

To **turn off** an LED, set its LED pin variable to `LOW`:

```java
// turn off led
digitalWrite(led, LOW);
```

### Adjust Brightness of LED (requires PWM pin)

An LED can be turned on to a specific brightness by using a `analogWrite()` statement, **instead** of using `digitalWrite()`. However, the LED must be connected to a pin that is capable of **PWM output**.

PWM stands for [pulse-width modulation](https://learn.sparkfun.com/tutorials/pulse-width-modulation), which is how a digital output signal (which has only two values: HIGH or LOW) can act like an analog output signal (which has a range of values).

Only the following pins on the Photon RedBoard can act as PWM outputs: D0, D1, D2, D3, A4, A5, WKP(A6), RX, TX.

To adjust the LED brightness, set the LED pin variable to any integer value between 0-255:

```java
// set brightness to number between 0-255

// set led to 100% brightness
// this is same as digitalWrite(led, HIGH)
analogWrite(led, 255);

// set led to 50% brightness
analogWrite(led, 128);

// set led to 25% brightness
analogWrite(led, 64);

// set led to 0% brightness (off)
// this is same as digitalWrite(led, LOW)
analogWrite(led, 0);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.launchboard.app/cxd/internet-of-things-project/references-for-wiring-and-coding/led-light.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
