Analog Inputs - No reporting or excessive reporting

I’m a bit new to hardware and am having a a few issues reading some analog inputs.

Ideally, I’d like to be able to swap out inputs at anytime and have them report back to a web application. The code below loops over all pins, checks for update, and publishes a change if needed.

The digital portion works great, but only if I comment out the analog part. The analog never works :frowning: What’s wrong with the below code? Am I overwhelming the stream? Is there some kind of interference that continually gets published? If I change the loop interval from 100 to 1000 milliseconds I start to see analog reporting, but I’m not manipulating anything :confused: ? Any ideas as to what might be wrong here?



int analogPins[] = {A0, A1, A2, A3, A4, A5, A6, A7};
int digitalPins[] = {D0, D1, D2, D3, D4, D5, D6, D7};
int digitalStates[] = {0, 0, 0, 0, 0, 0, 0};
int analogStates[] = {0, 0, 0, 0, 0, 0, 0};

int analogChangeThreshold = 5;

unsigned long lastTime = 0UL;
void loop()
  unsigned long now = millis();

    lastTime = now;

    // Loop over the analog inputs and publish the read
    // states if they've changed "enough" since the
    // previous event loop
    for(int a=0; a<8; a++){
      int currentState = analogRead(analogPins[a]);

      if(abs(analogStates[a] - currentState) > analogChangeThreshold){
        analogStates[a] = currentState;

        char publishString[64];
        sprintf(publishString, "{ \"pinId\": \"A%u\", \"state\": %u }", a, currentState);
        Spark.publish("input-update", publishString);

    // Loop over the digital inputs and publish the read
    // states if they've changed since the previous
    // event loop
    for(int d=0; d<8; d++){
      int currentState = digitalRead(digitalPins[d]);

      if(currentState != digitalStates[d]){
        digitalStates[d] = currentState;

        char publishString[64];
        sprintf(publishString, "{ \"pinId\": \"D%u\", \"state\": %u }", d, currentState);
        Spark.publish("input-update", publishString);

The Spark.publish() command is rate limited–I don’t know what the exact limit is but I would guess around once a few per second. Could that be cause the behavior you see?

Also I hope you are driving all the pins–a floating input pin, particularly an analog input pin, can go to any value.

1 Like

I’m thinking that the threshold of 5 is kinda tight for the analog?

Digital is pretty clear cut. 0V or 3.3V anything inbetween is still ok… well…maybe before 3.3 you get a HIGH but the tolerance is huge…

Once you unplug the analog input, the pin is left floating any the value can be rather random.

Oh @bko mentioned the same thing :wink:

Maybe…you can have a pulldown resistor from the analog inputs to ground…something like 10k should be fine!

1 Like

Thanks, I think you’re right about both things.

I’m pretty sure I’m being rate limited when I turn analog inputs on - probably because it’s publishing non stop.

I didn’t realize that not driving a pin means it could go to any value. I’ve fixed these issues and it seems to be behaving just fine now. Thanks again!

1 Like

Thanks for the advice on the pulldown resistors. I was hoping I would be able to make components more plug and play, but that doesn’t seem realistic given the amount of variance I’d like to have in analog inputs. Thanks again, Joe

If you have some resistor arrays around it’s going to take up minimal space. Google it and you know what i’m saying :wink:

1 Like

Hey Guys,

Yes! Spark.publish is currently rate-limited to an average of 1 publish per second (60 per minute), with an allowed burst of up to 4 per second.

To stream more quickly, a direct TCP or UDP socket will let you connect directly to your server or application and let you stream large amounts of data data much more quickly. :smile:


Thanks David. I’m not familiar with either of those protocols, kind of a noob in this field. Can you point to any learning resources or tutorials around connecting a spark or other arduino hardware via tcp?

1 Like

I would normally point you to our local communication example, but that feels a bit dated ( ). I’ll try to find some examples and post them here :slight_smile:

1 Like