Help with Particle RPM reader data to Google Sheets document

Hello Particle community,

So for a while now I’ve been working on this little project where I’m using 4 separate Photons to pull the RPMs from several different label printing press unwind shafts. Now with standard arduino code and using their Yun board and such, this would generally be a breeze. However, obviously the Build-IDE is a little different in its syntax and library addition, so making a working, accurate tachometer has been an extremely frustrating experience. On the other end of the spectrum, I am using Google Sheets to display the data from these Photons (I have only interfaced one so far), and used code from a project I found on google that displays basic data from a temperature-humidity sensor. Now, I am using a Keyes Magnet-Ring sensor for each of these particle boards and it is the only thing attached to each board, so no other sensors or anything are being read or controlled by the board. I am currently at my wits end with both the particle code and the Google Sheets script editor code, and any help or input would be so greatly appreciated.

Here’s the Build-IDE code:

#include "FreqPeriodCounter.h"

const int simulated_fan_output_pin = A0;
const int counter_interrupt_pin = D4; 
char resultstr[64];

FreqPeriodCounter counter(counter_interrupt_pin, micros, 0);

const int k_seconds_per_minute = 60;
const int k_pulses_per_revolution = 2;
const int k_hertz_to_RPM_conversion_factor = k_seconds_per_minute / k_pulses_per_revolution;

void setup(void) 
    Serial.println("Computer fan RPM Demo");

    pinMode(simulated_fan_output_pin, OUTPUT);
    analogWrite(simulated_fan_output_pin, 128); // any number here should create a 500Hz frequency

    pinMode(counter_interrupt_pin, INPUT);
    attachInterrupt(counter_interrupt_pin, counter_interrupt_service_routine, CHANGE);
    Particle.variable("result", resultstr, STRING);


void loop() 
    int per;
    long her;
    int rpm;

    if(counter.ready()) {
        per = counter.period;
        her = counter.hertz();
        rpm = her * k_hertz_to_RPM_conversion_factor;
        rpm = rpm/60000;

    sprintf(resultstr, "{\"per\":%d,\"her\":%d,\"rpm\":%d}", per, her, rpm);


void counter_interrupt_service_routine()

Here is the Google Sheets Script Editor code:

  function collectData() {
  var  sheet = SpreadsheetApp.getActiveSheet();

  var response = UrlFetchApp.fetch("");

  try {
    var response = JSON.parse(response.getContentText());
    var result = unescape(response.result);

    try {
      var p = JSON.parse(result); 
      var d = new Date(); 
      sheet.appendRow([d, p.per, p.her, p.rpm]); 
    } catch(e)
      Logger.log("Unable to do second parse");
  } catch(e)
    Logger.log("Unable to returned JSON");

I can’t help with the Tachometer code but I have used Google Sheets and then Ubidots for sending temp and humidity data from the Photon to Ubidots for graphing, logging, and email notifications.

Ubidots is a breeze to get setup and I think it would make your life easier. There is a Ubidots library in the Online IDE which gives examples on how to send your data over. Their account is free so no reason not to give it a try.

Also once you get the RPM code working please share. I want to read the RPM of some 80mm fans I used that have a pulse output wire to measure RPM. I want to use the Photon to control the speed of the fan.

It wold be helpful to know what is wrong with the Particle code you posted. Saying you’re frustrated, but not telling us what’s wrong, what specific problems you’re having, doesn’t give us the information we need to help you.

1 Like

Well, for starters, it will return some absurd value like 18674739 or something like that. Im thinking it might be the fact that the sensor values are read differently than they are on an Arduino, but I wouldn’t know how to fix that being a beginner to Particle.

Can you post some sort of diagram or picture of how you have it all wired up to the Photon?

Has the library, FreqPeriodCounter, even been ported to Particle? You usually can’t just use an Arduino library without making some (often minor) changes to account for the differences in the two platforms.

Looks pretty simple.

What kind of RPM speeds are the machines running at?

Do you have a link to that black adapter board your hooking up to the Photon? Is it a reed switch?

That black board is the Magnet-ring sensor module, and the rpms that it typically runs at are roughly 400-500 rpm

I’m not sure why your current code does not work but there is what looks like a working pulse counting library abaliable in the online IDE. See below. I would check it out and see if you can get it working with your sensor which shouldn’t be to hard to do.

Lets us know if you do get it working.

Looks like @peekay123 adapted this libarary so he may be able to help out if you have questions.

@RWB, that library is for a human (heart) pulse sensor :wink:

@milesc37, can you please provide a link to the freqperiodccounter library. One thing with using CHANGE interrupts is that an external pull-up or pull-down resistor is required to prevent the input pin from floating. Where is that sensor board from?

1 Like

IMHO that’s a rather (excuse the term) “dumb” lib that doesn’t take into account the extra delay between loop() iterations caused by the cloud housekeeping on Particles.

Single triggers might me lost completely if frequency goes up to 1kHz or the WiFi connection drops.