CxD Archive
Code: Internet of Things 2.0
Code: Internet of Things 2.0
  • Code Introduction
  • Prerequisite Knowledge
  • Tutorials
    • A. Meet Your IoT Kit
      • A-1 Circuit Board
      • A-2 Other Components
      • A-3 Electronic Circuits
    • B. Hello World Test
      • B-1 Start IoT Device
      • B-2 Login to Web IDE
      • B-3 New App Template
      • B-4 Global Variable
      • B-5 Setup Function
      • B-6 Loop Function
      • B-7 Flash App to Device
      • B-8 Modify App
    • C. Smart Light Device
      • C-1 Connect LED
      • C-2 Copy Hello World App
      • C-3 Connect Button
      • C-4 Add Button Code
      • C-5 Modify Button Code
      • C-6 Particle Cloud Code
      • C-7 Web App HTML
      • C-8 Web App CSS
      • C-9 Web App JS
    • D. Smart Security Device
      • D-1 Connect Motion Sensor
      • D-2 Connect Speaker
      • D-3 LED and Button Code
      • D-4 Motion Sensor Code
      • D-5 Speaker Code
      • D-6 Particle Cloud Code
      • D-7 Web App HTML
      • D-8 Web App CSS
      • D-9 Web App JS
  • References
    • Particle Build
    • Photon Device App
    • Web App - Single Screen
    • Web App - Multiple Screens
    • Particle Cloud
      • Web App Prep Steps
      • Get Device Variable
      • Call Device Function
      • Get Device Events
    • Physical Inputs
      • Push Buttons
      • Trimpot Dial
      • Motion Sensor
      • Magnetic Switch
      • Light Sensor
      • Temperature Sensor
      • Soil Moisture Sensor
      • Accelerometer
      • Ultrasonic Sensor *
    • Physical Outputs
      • LED Lights
      • Speaker
      • Servo Motor
      • Micro OLED Display
  • Links
    • IoT Project Guidebook
    • Particle Build (Web IDE)
    • Wiring Language
    • Photon Firmware
    • Particle API JS
    • W3Schools
    • Photon Kit Experiments
Powered by GitBook
On this page
  • Set Pin Mode for LED
  • Verify Your App Code
Export as PDF
  1. Tutorials
  2. B. Hello World Test

B-5 Setup Function

Next you'll add a command in your app code to designate the LED pin as an output.

Set Pin Mode for LED

A variety of inputs and outputs can be connected to the I/O pins on your Photon circuit board. Your app code needs to identify which I/O pins are being used and whether each of these pins will be used for an input or output. This is referred to as setting the pin modes.

You'll need to set the pin mode for the built-in LED that you'll be using. Add this code to your app by inserting it within the setup() function (between the curly braces):

pinMode(LED, OUTPUT);

The pinMode() method requires two parameters inside its parentheses (in this order):

  1. The I/O pin number, which can be the actual pin number (such as: D7, etc.) or a variable that stores a pin number. In this case, the variable LED is listed (which has a value equal to D7).

  2. The mode value, which can be INPUT, INPUT_PULLUP, or OUTPUT. Your Photon uses this value to change the electrical behavior of the pin, so the pin can either receive signals from an input or send signals to an output. In this case, the mode was set to OUTPUT because your app will be sending "on" and "off" signals to the LED light.

At this point, your app code should look similar to this:

int LED = D7;

void setup() {
    pinMode(LED, OUTPUT);
}

void loop() {

}

Notice that the pinMode() statement shown in the app code above is indented (using the tab key). This is a useful practice when adding code statements within curly braces because it helps make the code easier to read and understand – especially if your app contains many code statements nested within each other.

Verify Your App Code

The Particle Build code editor does NOT check your code syntax as you type, so be sure to periodically verify your code to check for errors.

Verify your app code by clicking the Verify icon in the left navigation bar. (Particle Build will first save your code before verifying it.)

While Particle Build is verifying your code, it will display the message "Compiling code" in the status bar at the bottom of the code editor panel. When the verification process is complete, the status bar will indicate the results:

  • If your code compiled without any errors, the status bar will display a success message.

  • If your code contains an error, the status bar will display an error message with a description of the error and a link to the specific line number in your code where the error was detected (though sometimes the root cause of the error occurs on a previous line). You'll want to fix the error and then try verifying your code again.

MULTIPLE ERRORS: Sometimes your app code might contain multiple errors. However, Particle Build will stop verifying the code at the first error that is detected. Once you fix that error and verify the code again, you might see a new error message for another error that occurs later in the code.

PreviousB-4 Global VariableNextB-6 Loop Function

Last updated 2 years ago

Verify Icon