Practice Exercise: Sensors to Action

Table of contents

  1. 1. Add a Piezo
  2. 2. Create an if statement to trigger actions.
  3. 3. Link your code to IFTTT
  4. Going Further

Using the circuit you prepared in the last practice exercise, we’re going to modify the circuit as follows:

  • Add a piezo element and trigger a sound alert
  • Use an if statement to evaluate the sensor readings and trigger actions
  • Use IFTTT to map actions from your sensors onto actions in the cloud.

1. Add a Piezo

Start by adding a piezo element to your circuit. Test that you can play a sound using the piezo in your kits.

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

Create an account on IFTTT and add the Particle Channel. You might also want to add channels for Google Drive/Sheets, for SMS messaging and push notifications (this will require you to add the IFTTT app to your smartphone)

Once you’ve set up IFTTT, create an applet to link your Particle project to online services.

Log to Spreadsheet

In your applet on IFTTT:

  1. set your if to be New event published from the Particle channel.
    • Choose the If (event name) to be something like log_to_spreadsheet
    • Choose the Device Name that matches your Particle Argon from the dropdown list.
  2. Set it to Trigger the Add Row to Spreadsheet on the Google Sheets channel
    • Add the formatted row as |||||| |||
  3. Save and create your applet.

In your Particle project, add the following line to your code

// store the time when you last published
int last_published = -1;

void log_to_spreadsheet(  ){

  // check if 1 minute has elapsed
	if( last_published + 60000 < millis() ){
		Particle.publish( "log_to_spreadsheet", String( sensorValue  ) );
		last_published = millis();


In your loop(), call the log_to_spreadsheet() function above.

Trigger an Action

Modify the example above to trigger an SMS or Push Notification through IFTTT

Going Further

Once you’ve achieved the outcome, you can try some of the below and experiment to improve your coding skills

Exercise 1

Experiment with other IFTTT triggers and actions you can call.

Exercise 2

Modify the log to spreadsheet example to log multiple values to the spreadsheet. Gather some data and graph it to visualize your readings.

Exercise 3

Refine your barebones implementation into a working sensor platform that alerts you only when you need to know in-situ feedback and through push notifications/sms alerts.

Table of contents