CxD Archive
Internet of Things Project
Internet of Things Project
  • IoT Project Background
  • IoT Project Challenge
  • IoT Project Outline
    • 1-1 Smartphone Research
    • 1-2 Smart Thermostat Research
    • 1-3 Smart Device Research
    • 1-4 Introduction to Photon Kit
    • 1-5 Build Smart Lightbulb
    • 1-6 Generate Project Ideas
    • 1-7 Build Smart Thermostat
    • 2-1 Evaluate Project Ideas
    • 2-2 Plan User Research
    • 2-3 Compile Research Data
    • 2-4 Construct Personas
    • 2-5 Construct Journey Map
    • 2-6 Define Value Proposition
    • 2-7 Define Design Requirements
    • 3-1 Generate Solution Ideas
    • 3-2 Evaluate and Refine Solution Ideas
    • 3-3 Create System Model of Solution
    • 3-4 Create Wireflow for Web App
    • 3-5 Create Storyboard for Solution
    • 3-6 Evaluate Solution Design
    • 3-7 Present Project Proposal
    • 4-1 Track Progress and Issues
    • 4-2 Build Smart Device
    • 4-3 Create Photon App
    • 4-4 Create Web App
    • 4-5 Conduct Integration Testing
    • 4-6 Create Product Demo Video
    • 4-7 Create Product Marketing Website
    • 5-1 Evaluate Smart Device and Web App
    • 5-2 Evaluate Product Marketing Website
    • 5-3 Improve Solution Based on Evaluations
    • 6-1 Create Project Poster
    • 6-2 Present Project to Public
    • 6-3 Write Personal Reflection
  • References for Wiring & Coding
    • Creating Circuits
    • Wiring Programming Language
    • Particle Cloud
    • Hello World App for Photon
    • LED Light
    • Push Button
    • Micro OLED Display
    • Humidity and Temp Sensor
    • Speaker
    • Servo Motor
    • Motion Sensor
    • Magnetic Switch
    • Potentiometer
    • Photocell
    • Moisture Sensor
    • Accelerometer
    • RFID Reader
    • Fingerprint Scanner
    • GPS Receiver
    • Camera
    • Microphone
    • Force Sensitive Resistor
    • Keypad
    • Continuous Rotation Servo Motor
  • Glossary
  • Updated Project Guidebook
  • Updated Code Guidebok
Powered by GitBook
On this page
  • Resistor
  • Code for LED Light
  • Library
  • Global Variables
  • setup( ) function
  • Turn LED On or Off
  • Adjust Brightness of LED (requires PWM pin)
Export as PDF
  1. References for Wiring & Coding

LED Light

PreviousHello World App for PhotonNextPush Button

Last updated 6 years ago

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

An LED light is a 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 (+).

Resistor

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

LED Light

Photon Pin

Positive leg (long) = Power

any I/O pin

Negative leg (short) = Ground

GND using resistor

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

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

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

An LED can be easily burned out if it receives too much power. Therefore, a must be used to help limit the amount of current flowing through the LED.

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

PWM stands for , 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).

resistor
Experiment 1
pulse-width modulation
polarized