Readings on Analog Inputs on Photon

In playing with a Photon through the Tinker application I have noticed that if a do a several analog reads on the same input, the first reading is correct and the other reading are in the “300 range”. If I do an analog reading from another input and then switch back to the original, the first new reading is correct and the following readings are in the “300 range” again.

Has anyone else seen that? What is the problem?

Thanks,

Tom

I’m having the same issue with my Electron. I connected the photo resistor using the 220ohm resistor that came with the Electron Kit according to the Annotated Particle example. I get a reading of between 40 to 100 depending on the ambient brightness, and when I cover the sensor with my hand, it reads zero. But after about 1 minute, the value will shoot up to between 240 to 260 and won’t respond to any change in the light intensity. Even if I unplug the photo resistor, and disconnect all connections to the Analog input, the value doesn’t change.

But if I switch to another Analog input, it will work fine for about a minutes and the issue will start with the new pin. What could be causing this strange behaviour?

@Donwhale without some idea of what your code looks like it is very difficult to help troubleshoot the problem.

1 Like

It’s also good to provide a link to the samples you reference to save people the need to search - we don’t have each and every sample and diagram at the top of our heads :wink:

I was referring to this https://docs.particle.io/guide/getting-started/examples/electron/#setup-2

I was using the Blynk apps to read the Pin A0 which seems to freeze the pin after a few seconds, but when I wrote a code to use Particle Cloud Variable, it was working fine.

This is working fine:

STARTUP(cellular_credentials_set("internet.ng.airtel.com", "", "", NULL));


int photoresistor = A0; // 

int power = A5; // This is the other end of your photoresistor. 

int analogvalue; // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor.

FuelGauge fuel;

int battery;


void setup() {

    pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
    pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)
    digitalWrite(power,HIGH);

    // We are going to declare a Particle.variable() here so that we can access the value of the photoresistor from the cloud.
    Particle.variable("analogvalue", &analogvalue, INT); // Particle variable for cloud
    Particle.keepAlive(55);    // send a ping every 55s to keep connection on 3rd party SIM
    
  
    Particle.variable ("battery %", &battery, INT);
    
}


void loop() {

    // check to see what the value of the photoresistor is and store it in the int variable analogvalue
    analogvalue = analogRead(photoresistor);
    battery = fuel.getSoC();
    delay(500);

}

But the Blynk apps works for some seconds, and seems to freeze the pin:

//#define BLYNK_DEBUG // Uncomment this to see debug prints
#define BLYNK_PRINT Serial
#include "blynk/blynk.h"

STARTUP(cellular_credentials_set("internet.ng.airtel.com", "", "", NULL));

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Attach a Button widget (mode: Switch) to the Digital pin 7 - and control the built-in blue led.
// Attach a Graph widget to Analog pin 1
// Attach a Gauge widget to Analog pin 2

// No coding required for direct pin operations!

void setup()
{
    Serial.begin(9600);
    delay(5000); // Allow board to settle
    Blynk.begin(auth);
    Blynk.begin("authcode", IPAddress(45,55,130,102), 8442);
    Particle.keepAlive(55);    // send a ping every 55s to keep connection on 3rd party SIM
}

// Attach a Button widget (mode: Push) to the Virtual pin 1 - and send sweet tweets!
BLYNK_WRITE(V1) {
    if (param.asInt() == 1) { // On button down...
        // Tweeting!
        // Note:
        //   We allow 1 tweet per minute for now.
        //   Twitter doesn't allow identical subsequent messages.
        Blynk.tweet("My Particle project is tweeting using @blynk_app and it’s awesome!\n @Particle #IoT #blynk");
        
        // Pushing notification to the app!
        // Note:
        //   We allow 1 notification per minute for now.
        Blynk.notify("You pressed the button and I know it ;)");
    }
}

// Attach a ZeRGBa widget (mode: Merge) to the Virtual pin 2 - and control the built-in RGB led!
BLYNK_WRITE(V2) {
    int r = param[0].asInt();
    int g = param[1].asInt();
    int b = param[2].asInt();
    if (r > 0 || g > 0 || b > 0) {
        RGB.control(true);
        RGB.color(r, g, b);
    } else {
        RGB.control(false);
    }
}

void loop()
{
    Blynk.run();
    
    if (ModeBtnPressed()) {
        Blynk.notify("Mode button was pressed");
    }
}

// *** Utility functions

bool ModeBtnPressed() {
    if(millis() > 5000) {
        if(BUTTON_GetDebouncedTime(BUTTON1) >= 50) {
            BUTTON_ResetDebouncedState(BUTTON1);
            return 1;
        }
    }
    return 0;
}

@Donwhale, for Blynk, are you using the latest version of the library? You may want to detach the library from your app then re-attach the library again. Also, what version of system firmware are you running on the Electron? It is not A0 that is freezing but Blynk I suspect. Using the latest library and system firmware 0.5.3 or later should work.

Please note the following from the docs:

Before 0.5.3 Note: do not set the pinMode() with analogRead(). The pinMode() is automatically set to AN_INPUT the first time analogRead() is called for a particular analog pin. If you explicitly set a pin to INPUT or OUTPUT after that first use of analogRead(), it will not attempt to switch it back to AN_INPUT the next time you call analogRead() for the same analog pin. This will create incorrect analog readings.

Since 0.5.3 Note: you do not need to set the pinMode() with analogRead(). The pinMode() is automatically set to AN_INPUT any time analogRead() is called for a particular analog pin, if that pin is set to a pinMode other than AN_INPUT. If you explicitly set a pin to INPUT, INPUT_PULLUP, INPUT_PULLDOWN or OUTPUT before using analogRead(), it will switch it back to AN_INPUT before taking the reading. If you use digitalRead() afterwards, it will automatically switch the pinMode back to whatever you originally explicitly set it to.

You have a pinMode(photoresistor,INPUT); you may want to remove. Also, the syntax for Particle.variable() is not up to date. You no longer need to declare the pointer or the type of variable. This suggests that you may be compiling to system firmware older than 0.4.7! Since 0.4.7, the syntax is now:

Particle.variable("analogvalue", analogvalue);

In review. Make sure you update your Electron to system firmware 0.5.3 or later. Reload the latest Blynk library and make sure you are compiling to the system firmware you selected when compiling your app. :smiley:

1 Like

@peekay123, I was running on 0.5.2. I just upgraded the device firmware to 0.6.0 and also updated the apps firmware to 0.5.3 and it’s now working fine.

2 Likes