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
  • Global Variable for Button Pin
  • Set Pin Mode for Button
  • Turn On LED If Button Pressed
  • Flash App to Device
  • Troubleshooting Issues
Export as PDF
  1. Tutorials
  2. C. Smart Light Device

C-4 Add Button Code

Next, you'll add code in your Smart Light device app to control the LED using the push button.

The basic steps to use a push button in your app code are:

  1. Declare a global variable to store the I/O pin number for the button.

  2. Set the pin mode for the button pin in the setup() function.

  3. Use a digitalRead() statement to check whether the button is currently pressed, and add code statements that should be performed if the button is pressed (or not pressed).

Global Variable for Button Pin

Declare a global variable to store the I/O pin number for the button by adding this code statement before the setup() function:

int button = D2;

If you connected your button to a different I/O pin other than D2, then modify this code statement to list the correct pin number for your button.

Set Pin Mode for Button

Set the pin mode for your button pin by adding this code statement within the setup() function (between the curly braces):

pinMode(button, INPUT_PULLUP);

Turn On LED If Button Pressed

The digitalRead() method is used to check whether a button is currently pressed.

The digitalRead() method will return a value of either LOW or HIGH:

  • LOW indicates that the button is currently pressed.

  • HIGH indicates that the button is NOT currently pressed.

A variable named buttonState will store the value returned by the digitalRead() method.

Replace all the existing code within the loop() function (everything inside its curly braces) with this code instead:

    int buttonState = digitalRead(button);
    
    if(buttonState == LOW) {
        digitalWrite(LED, HIGH);
    }
    else {
        digitalWrite(LED, LOW);
    }

COPY CODE: Remember that you can copy a code block in this guidebook simply by clicking the copy icon in the upper right of the code block.

Flash App to Device

Flash your app code to your Photon device by clicking the Flash icon in the left navigation bar.

Once your Photon has downloaded the app and restarted, the updated app will start running:

  • Confirm that the LED light that you connected to your Photon circuit board turns on when you press (and hold) the button – and turns off when the button is released.

Of course, nobody would want to continually press a button to keep a light turned on – however, this is a good way to verify that your button is connected correctly. In the next step, you'll modify the app code so the LED light toggles on or off with each button press.

Troubleshooting Issues

If the LED does not turn on when the button is pressed, then there is a mistake in your app code – or a mistake in your wiring connection – or a mistake in both.

If the LED light is turned off and pressing the button has no effect, then verify that one of the button's legs is connected (via a jumper wire) to the same I/O pin number that is assigned to the button variable in your app code. If necessary, change either your wiring or your code, so the pin numbers match. If you revised your code, flash the updated app to your device.

If that still doesn't solve the issue, then double-check all your button wiring by referring back to the connection instructions.

PreviousC-3 Connect ButtonNextC-5 Modify Button Code

Last updated 2 years ago

An will be used to turn on the LED if the button is being pressed (if buttonState has a value equivalent to LOW).

An will be included to turn off the LED if the button is not being pressed.

if statement
else statement