Photon requires delay when waking from deep sleep?

I’m using a Photon in SLEEP_MODE_DEEP to conserve power. I have two sensors connected via diodes to the WKP pin so either one will wake the Photon. Each sensor is also connected to an analog input. The idea is that when either sensor is activated it wakes the Photon, then I read the analog inputs to determine which sensor was activated. Then one of two events is published to the cloud, depending on which sensor activated.

The problem is that I don’t see any events in the Console unless I add a delay of about 3 seconds using delay(3000); after waking up. With a 2 second delay it successfully publishes about half the time; with any delay less than that I do not see any events published. Is it normal to need such a delay when waking from deep sleep? In operation the sensors might not be activated as long as 3 seconds. If a delay is required I would like to get it down to about one second if possible. Otherwise I will have to add hardware such as a sample and hold circuit or maybe just a capacitor.

I also tried using waitUntil(Particle.connected); instead of an actual delay (see commented line in code below) but again I do not see any events published.

Here’s the code:

int mailSensor = A0;
int newsSensor = A4;
int senseMail = 0;
int senseNews = 0;

void setup() {
 pinMode(mailSensor, INPUT);
 pinMode(newsSensor, INPUT);
}

void loop() {
  delay(3000);
//  waitUntil(Particle.connected);    //Use this line OR the delay line, not both
  senseMail = analogRead(mailSensor);
  senseNews = analogRead(newsSensor);
  if (senseNews > 1000) {
    Particle.publish("News", NULL, PRIVATE, WITH_ACK);
  }
  if (senseMail > 2000) {
  Particle.publish("Mail", NULL, PRIVATE, WITH_ACK);
  }
  System.sleep(SLEEP_MODE_DEEP);
}

What system version are you trageting? Try 0.7.0-rc.4

This is a known issue but should be taken care of with a more recent system version.

Here is the related and now closed issue on GitHub
https://github.com/spark/firmware/issues/1165

1 Like

I was using version 0.6.3. But I tried version 0.7.0-rc.4 and my events are still not being published unless I include the 3 second delay. I also tried moving the delay to after the Particle.publish statements, as in the other thread you linked, but that also had no effect. Do you have any other ideas?

What if you put the delay after your analogRead() calls?
Also add another publish for the case neither of your other conditions hits.

Yes, I tried that before. Tried it again now, in both 0.6.3 and 0.7.0-rc.4 to be sure. No luck. I also tried using SYSTEM_MODE(SEMI_AUTOMATIC) and placing Particle.connect() after the analogRead() calls (code below). In that version I could not get it to publish at all, even with a delay.

I'm not sure I understand. If neither sensor activates, then the Photon would not wake up. Well, it would be awake the very first time through loop{}, when it is first powered up. But in that case I would be standing there next to it :slight_smile:

Code for Semi-Automatic mode:

int mailSensor = A0;
int newsSensor = A4;
int senseMail = 0;
int senseNews = 0;
SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {
 pinMode(mailSensor, INPUT);
 pinMode(newsSensor, INPUT);
}

void loop() {
  delay(3000);
  senseMail = analogRead(mailSensor);
  senseNews = analogRead(newsSensor);
  Particle.connect();
  waitUntil(Particle.connected);
  if (senseNews > 1000) {
    Particle.publish("News", NULL, PRIVATE, WITH_ACK);
  }
  if (senseMail > 2000) {
  Particle.publish("Mail", NULL, PRIVATE, WITH_ACK);
  }
  System.sleep(SLEEP_MODE_DEEP);
}

In that case the problem is maybe not the Particle.publish() but the analogRead() needing some spin-up time. And to test exactly that theory the extra publish would tell.

Yes, I see what you mean now. I will try that, but can’t do it tonight. I’ll update this thread when I can.

Thanks for you help so far. It’s much appreciated.

1 Like

I simplified the code for troubleshooting. Now upon waking it just reads the two sensors, publishes two events with the sensor readings as data, and goes back to sleep. To isolate what needs a spin-up time (I like that name) I tried putting the delay in all three possible locations: before reading the sensors, between reading and publishing, and after publishing. It successfully publishes data with the delay in any of those locations, but not if there is no delay at all. So for example if the delay is in the last position (after publishing), that seems to indicate that neither the analogRead nor the Particle.publish individually require a spinup time, because they both work without being preceded by a delay, as long as there is a delay somewhere in the loop, even after. Could there be some other housekeeping activities behind the scenes that my short loop is interfering with?

int mailSensor = A0;
int newsSensor = A4;
int senseMail = 0;
int senseNews = 0;

void setup() {
 pinMode(mailSensor, INPUT);
 pinMode(newsSensor, INPUT);
}

void loop() {
//  delay(3000);
  senseMail = analogRead(mailSensor);
  senseNews = analogRead(newsSensor);
//  delay(3000);
  Particle.publish("Mail Level",String(senseMail),PRIVATE,WITH_ACK);
  Particle.publish("News Level",String(senseNews),PRIVATE,WITH_ACK);
  delay(3000);
  System.sleep(SLEEP_MODE_DEEP);
}

@SteveR, the pinMode() commands are not required prior to analogRead(). Also, I would move your code to setup() since it will only run once anyway. Give that a shot to see if things change.

1 Like

I'm glad you pointed that out. The documentation says "it will reset and run all user code from the beginning" and I wasn't sure if that included setup() or only loop().

I removed the pinMode() commands and moved the code to setup() and nothing changed.

I have noticed something else odd. The sensor connected to pin mailSensor is a light-dependent resistor in a voltage divider with a fixed resistor, and will be placed inside my mailbox. When the mailbox door opens, the voltage at the middle of the voltage divider will increase, causing an event to be published and indicate that mail was delivered. The point of this thread was that a 3 second delay may be longer than it takes the mail carrier to open and close the box. Now that I have been experimenting (based on @ScruffR's suggestions) it occurred to me that if I put the delay after the publish, my project should work if I just grab a quick reading immediately when the box is opened. Then I can publish it, and a delay after publishing would not hurt me. But now that I am publishing the actual data so I can see it I have noticed something odd. The longer I open my simulated mailbox for a test, the higher the voltage I read at the analogRead(). It's as if there is a significant capacitance at the analog input, causing the voltage to increase slowly over a period of several seconds, rather than extremely quickly as I expected.

The ADC capacitance is in the pF range, but if your sensor impedance is too high the time constant of that RC combo might still cause some delay. Additionally the photoresistor itself has some responsetime which may also vary with temperature.

Have you considered using a photodiode/phototransistor or even a reed switch instead?

1 Like

I did consider a reed switch, but I prefer to trigger it optically to keep the installation simpler. I have been using the photoresistor just because I have some on hand, but I will look into using a photodiode or phototransistor if this doesn’t work out.

It seems that I have a good workaround for the delay issue. Now I need to work out the details of the hardware. Thank you for your help.

1 Like