Boron Sleep & Digital Pin not reliable

Hello,

I am using a battery-powered boron lte device, putting the boron into “ultra low power” sleep mode, and I am trying to sense a float switch changing state and send an alarm based on the state change from 1 to 0.

The problem I am running into is that it does not trigger reliably. My float switch is wired to be NO and then after rotation, it is NC. My code holds the input high in sleep mode, but when I rotate my switch to NC it does not pull to ground, it remains at 3.3v.

Also, in my setup, I am using the adafruit feather breakout and have a 10K resistor between the EN and BAT pin. Please help.


@chrizzychriz31,

There is no reason why this type of interrupt can’t be implemented reliably. Couple things that might help us see what the issue is:

  1. When you share code (and that is the right thing to do), try pasting into a code block that you start and stop with three eclipses (under the tilde), it will make it easier to see your code. The block will look like this:
Code Block
  1. Can you say a bit more about the electrical circuity for your switch? Is it being “debounced”, does it have a pull-down resistor or is it floating?

Thanks,

Chip

1 Like

How is your switch wired? It would normally be wired something like this for a normally open switch falling.

2 Likes

It is wired just like your diagram. I believe the problem is in my sleep code. I have the same exact setup working reliably without letting the boron sleep.

#include <application.h>
#include <spark_wiring_i2c.h>

unsigned long sleptMS = 0;
unsigned long maxSleepInterval = 1000 * 60 * 60 * 24 * 30;

/*
 * Get the remaining milliseconds until the next 30 day interval
 */
unsigned long get30DayInterval() {
    unsigned long result = maxSleepInterval;
    if(sleptMS > 0) {
        result -= sleptMS;
    }
    
    if(result > maxSleepInterval) {
        result = 1000 * 60;
    }
    
    return result;
}


void setup()
{
    pinMode(D4, INPUT);
    Serial.begin(9600);
    delay(1000);
}

void loop()
{
    unsigned long sleepInterval = get30DayInterval();
    unsigned long sleepTimestamp = Time.now();

    SystemSleepConfiguration sleepConfig;
    sleepConfig.mode(SystemSleepMode::ULTRA_LOW_POWER)
          //Alarm on (1 -> 0)
          .gpio(D4, FALLING)
          .duration(sleepInterval);
    SystemSleepResult result = System.sleep(sleepConfig);

    if (result.wakeupReason() == SystemSleepWakeupReason::BY_GPIO) {
        Particle.publish("GPIO");
        sendAlarm(result.wakeupPin());
        calculateSleptMS(sleepTimestamp, Time.now());
    } else {
        Particle.publish("Duration");
        //Send battery level
        sleptMS = 0;
        getBatteryVoltage();
    }
    
  delay(5000);
}

/*
 * Send the appropriate alarm based on the triggered GPIO pin
*/
void sendAlarm(pin_t pin) {
    int value = digitalRead(pin);

    char jsonOut[100];
    sprintf(jsonOut, "{ \"metadata\": \"{ \\\"value\\\": %i}\"}", value);
    Particle.publish("highFloatSwitchAlarm", jsonOut, PRIVATE);
    delay(300);
}

void calculateSleptMS(long sleepTimestamp, long wakeTimestamp) {
    sleptMS += (wakeTimestamp * 1000) - (sleepTimestamp * 1000);
}

void getBatteryVoltage() {
    FuelGauge fuel;
    float voltage = fuel.getVCell();

    int lowBattery = 0;
    if(voltage < 3.5) {
        lowBattery = 1;
    }

    char jsonOut[100];
    sprintf(jsonOut, "{ \"telemetry\": \"{ \\\"batteryVoltage\\\": %.2f, \\\"lowBattery\\\": %i}\"}", voltage, lowBattery);
    Particle.publish("highFloatSwitchTelemetry", jsonOut, PRIVATE);
    delay(300);
}```

I am utilizing the pullup resistor on the boron. It is wired just like rickkas7 diagram.

I’m not sure what happens when you exceed the limit, but the maximum sleep duration on Gen 3 including the Boron is around 24 days. You’ll need to shorten the maximum sleep duration.

After cold boot, it’s possible that the time will not be synchronized yet. The delay(1000) in setup() is close to providing enough time, but it’s not guaranteed. You should wait for Time.isValid() before using Time.now().

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.