LED Light

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

An LED light is a polarized 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.

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.

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 (+).


An LED can be easily burned out if it receives too much power. Therefore, a resistor 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.

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

Experiment 1 of the online SparkFun Photon Experiment Guide shows how to connect an LED. Here is the connection diagram for Experiment 1:

Code for LED Light


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).

// 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.

// 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:

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:

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

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

// 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, 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:

// 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);

Last updated