Link Search Menu Expand Document

Practice Exercise: Combining Sensors and Inputs

Table of contents

  1. Brief
  2. Create an if statement to trigger actions.

This practice exercise will ask you to get familiar with the sensors in your kits. It will ask you to

  • learn how to integrate sensing into your circuits and read information from the environment around you;
  • become familiar with how to write code to translate sensors into outputs (in this case, cloud connectivity and an LED)
  • explore components that could be used later in your creative projects.


Imagine a simple three component sensing device (an LED, a sensor and one additional sensor or input). Prototype it as follows:

  1. Start by reviewing the introductory materials for the inputs and sensor section, e.g. what are inputs, what are sensors, reading from sensors, etc.

  2. Identify a simple sensor that you’ll use as part of your creative project (e.g. the photocell/photoresistor is probably a good one to start with). Develop a working circuit with that component and write code to read and make use of information from that sensor (e.g. share the value online, translate it into an output using, for example, an LED.)

  3. Modify the circuit to add Particle.variable code and stream your data from your device to the cloud.

  4. Modify the circuit and add a second component (this can be sensor or an input). For this step, you’ll need to remix two posted tutorials examples Ensure both components are working correctly and are streaming live data to the web.

  5. Use an if statement to evaluate the sensor/input readings and trigger sensible actions specific to that component (e.g. turn on the LED when the temperature drops too low). See Details below.

  6. Document your work, describe how you approached this project, and why you prepared the interaction you have.

Create an if statement to trigger actions.

To do this you’ll need to experiment with the values and readings you’re receiving from each sensor. Use the Particle Console to evaluate the sensor readings. Experiment and make a note of the readings that you find. You should find out where the minimum and maximum values are i.e the range of values you will receive from the component. Then establish a good threshold (or cut off) at which you should trigger your responses.

For example, if you were working with the Photocell and saw that you got a minimum value of 700 and a maximum value of 3181, you might decide that you might alert the use if the light drops below 1500.

Modify the code to include an if statement and create three conditions to respond to:

  • In the first condition, both sensors show bad readings at the same time. In this case play a sound alert.
  • In the second condition, only the first sensor has poor readings. In this case, blink the LED to display the led for 1.5 seconds and off for 0.5 seconds.
  • In the third condition, only the second sensor has poor readings. In this case, blink the LED such that it is on for 0.5seconds and off for 1.5 seconds.
  • In the case that no condition is met, turn the piezo off and the led off.

Things to Try

If you want to get fancy, you could only play the piezo’s sound alert if the less than desirable sensor readings have been sustained for a while i.e. its really critical and the user sould know. You should also try not to drive them mad by playing the tones constantly. Try to modify the behavior to only play them every once in a while.

Table of contents