BME280 and TSL2561 I2C connected write out correct measurements only when restarting the code

Dear all,

I finally get Adafruit BME280 and TSL2561 connected with Electron via the I2C wiring. If I use System.sleep(WKP, RISING, 5 * 60, SLEEP_NETWORK_STANDBY), I notice that sensors only write out correct measurements when 1st time starting the device or resting the device. The sensor can write out correct measurements by using System.sleep(SLEEP_MODE_DEEP, 5 * 60).

I prefer the SLEEP_NETWORK_STANDBY method, because sensors write out dataset frequently.

Any ideas to solve this issue?

Thanks a lot.

Here is my code:

#include "Adafruit_TSL2561_U/Adafruit_TSL2561_U.h"
#include "Adafruit_BME280/Adafruit_Sensor.h"
#include "Adafruit_BME280/Adafruit_BME280.h"

Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C

FuelGauge fuel; // battery

// dvices on analog pins
#define power_stepup5V D7



const String key = "2IQR9SDN4Y1X9QPR"; // Change this to your Thingspeak api write key


void setup() {

// Set Pin Modes
    pinMode(power_stepup5V,OUTPUT);

// Turn on power source    
    digitalWrite(power_stepup5V,HIGH);

// Wait for stabilize sensors
    delay(10000);

  if (!bme.begin()) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1) Particle.process();
  }

// Setup the sensor auto-gain and integration time (fast, low resolution)
  tsl.enableAutoRange(true);            
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);

}

void loop() {

// Turn on power source
    digitalWrite(power_stepup5V,HIGH);

// Wait for stabilize sensors
    delay(10000);

// BME280
    float Ta = bme.readTemperature();
    float Rh = bme.readHumidity();
    float Pa = bme.readPressure() / 100.0F;

    
// TSL2561
    float Lx = 0;
    sensors_event_t event;
    tsl.getEvent(&event);
    Lx = event.light;

// LiPo
    float Vol = fuel.getVCell();

// Publish data    
    Particle.publish("thingSpeakWrite_All", "{ \"1\": \"" + String(Ta) + "\"," +
       "\"2\": \"" + String(Rh) + "\"," +
       "\"3\": \"" + String(Pa) + "\"," +
       "\"4\": \"" + String(Lx) + "\"," +
       "\"5\": \"" + String(Vol) + "\"," +
       "\"k\": \"" + key + "\" }", 60, PRIVATE);
       
// Give time for the message to reach ThingSpeak
    for(uint ms=millis(); millis() - ms < 5000; Particle.process());
    
// Turn off power source
    digitalWrite(power_stepup5V,LOW);

// Sleep for 5 minutes to save battery    
    System.sleep(WKP, RISING, 1 * 60, SLEEP_NETWORK_STANDBY);
//    System.sleep(SLEEP_MODE_DEEP, 5 * 60);

}
1 Like

What system version are you testing with?
Can you try 0.6.0-rc.1?

The difference in behaviour is due to the fact that deep sleep will do the full sensor setup again while stop mode sleep relies on everything still being the same way as was before (which might not be even if it’s only for a delay to let things settle).

One thing that got fixed is that interrupts had to be reestablished after stop - which might play a role here.

1 Like

I was using firmware 0.51. After the 0.6 upgrade, it (SLEEP_NETWORK_STANDBY) works fine when sensors are powered via 3.3V pin on electron.

In my project, I prefer to use D7 to power these two sensors. Adafruit BME280 and TSL2561 use very little power, so they can be powered by GPIO pins. Are there anything wrong in the loop?

Thanks ScruffR.

Your loop looks OK, although I personally prefer snprintf() or String::format() for string building and I guess 10sec could be cut down a bit for the sensors to power (but that’s just a guess).

1 Like

I found a simple solution is that I move the piece of code from the setup() to the loop(). This will solve the issue. The delay() can be as short as 2000ms, because the sensor response time is <1000ms.